Say I'm writing a program that takes the input from the user, which is a sentence of some sort, and outputs what he entered with the first letter capitalized.
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
constshort MAX_CHARACTERS = 100;
int main()
{
usingnamespace std;
char control = 'y';//this variable is used by user to stay in or exit out of do-while loop
char sentence[MAX_CHARACTERS];
do
{
cout << "Enter a sentence: ";
cin.getline(sentence, MAX_CHARACTERS+1);
bool period_encountered = true;
short i(0);
while(sentence[i] != '\0')
{
if((isalpha(sentence[i])) && period_encountered)
{
cout << toupper(sentence[i]);
period_encountered = false;
}/*
else if(sentence[i] == '.')
{
period_encountered = true;
cout << sentence[i];
}*/
cout << sentence[i];
i++;
}
cout << "\n\nWould you like to run the program again? (Y/N): ";
cin >> control;
while ((control != 'y') && (control != 'n'))
{
cout << "Incorrect input. Would you like to run the program again? (Y/N): ";
cin >> control;
control = tolower(control);
}
cin.ignore();
cin.clear();
}while(control == 'y');
cout << "Press key to exit: ";
char exit;
cin >> exit;
return 0;
}
So if I type in "tom" the output should be "Tom". However with the above code I get "84tom" instead. I have to replace line 26 with: