Answered You can hire a professional tutor to get the answer.

QUESTION

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

/*

* 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;

}

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question