ScholarMatic | 24/7 Homework Help

ScholarMatic Will Help You Write Your Essays and Term Papers

Answered » You can buy a ready-made answer or pick a professional tutor to order an original one.

/* * Incorrect Buffer Size Calculation A C++ example miscalculating buffer size.

by | Nov 28, 2023 | engineering

/*

* Incorrect Buffer Size Calculation

A C++ example miscalculating buffer size. 

*/

#include <cstdlib>

#include <iostream>

#include <vector>

using namespace std;

string convertInput(string userString){

// i is an iterator, charIndex moves through characters in the buffer

int i, charIndex;

// if string is larger that 10 chars, then do not store this input

const int MAX_SIZE = 15;

if ( MAX_SIZE < userString.length() ){

return “string to large”;

}

// an array to store characters in memory as a buffer

char buffer[(5*sizeof(char) * MAX_SIZE) + 1] = {‘ ‘};

charIndex = 0;

for ( i = 0; i < userString.length(); i++ ){

char theChar = userString[i];

// some secial charcters are changed to html format

if( ‘&’ == theChar ){

buffer[charIndex++] = ‘&’;

buffer[charIndex++] = ‘a’;

buffer[charIndex++] = ‘m’;

buffer[charIndex++] = ‘p’;

buffer[charIndex++] = ‘;’;

} else if (‘<‘ == theChar ){

buffer[charIndex++] = ‘&’;

buffer[charIndex++] = ‘l’;

buffer[charIndex++] = ‘t’;

buffer[charIndex++] = ‘;’;

} else if (‘>’ == theChar ){

buffer[charIndex++] = ‘&’;

buffer[charIndex++] = ‘g’;

buffer[charIndex++] = ‘t’;

buffer[charIndex++] = ‘;’;

} else if (‘”‘ == theChar){

buffer[charIndex++] = ‘&’;

buffer[charIndex++] = ‘q’;

buffer[charIndex++] = ‘u’;

buffer[charIndex++] = ‘o’;

buffer[charIndex++] = ‘t’;

buffer[charIndex++] = ‘;’;

}

else buffer[charIndex++] = theChar;

}

return buffer;

}

// function to show all the stored keywords

void showKeywords(vector<string> keywords) {

int count = keywords.size();

cout << “There are a total of ” << count << ” html keywords.” << endl;

for (int i = 0; i < count; i++) {

cout << keywords[i] << endl;

} 

}

// the program starts here

int main(int argc, char** argv) {

vector<string> keywords;

// put some starting values in the keywords vector

keywords.push_back(“HomeKit”);

keywords.push_back(“Interface”);

keywords.push_back(“Web Forum”);

showKeywords(keywords);

string newKeyword;

// run a loop for user to enter keywords.

while(true) {

cout << “Type a word to include in the html keyword data:” << endl;

getline(cin, newKeyword);

// if user hits enter without any input data, then break the loop

if (newKeyword.empty()) {

cout << “This program would now normally return to a main menu, but”

“since this is just an example, the application will now exit”;

break;

}

// run the convertInput function to check for html special chars

string output = convertInput(newKeyword);

// if the function sees that the string is too large, then inform user

if(output == “string to large”) {

cout << “keyword exceeds maximum length, please try again” << endl;

} else { // otherwise, store the converted string in the keywords vector

cout << endl;

keywords.push_back(output);

showKeywords(keywords);

cout << endl;

} 

}

return 0;

}

ScholarMatic: Explanation & Answer

Your ready answer from a verified tutor is just a click away for as little as $14.99


  

Click Order Now to get 100% Original Answer Customized to your instructions!

HOME TO CERTIFIED WRITERS

Why Place An Order With Us?

  • Certified Editors
  • 24/7 Customer Support
  • Profesional Research
  • Easy to Use System Interface
  • Student Friendly Pricing

Have a similar question?

PLAGIRAISM FREE PAPERS

All papers we provide are well-researched, properly formatted and cited.

TOP QUALITY

All papers we provide are well-researched, properly formatted and cited.

HIGHLY SECURED

All papers we provide are well-researched, properly formatted and cited.

ScholarMatic: Get Started

Assignment Writing Service

Feel safe and secure when placing an order on our portal!
Fruitful cooperation begins with solid guarantees, and we are professional enough to promise perfect results. Let’s get it started!