Hello. Currently, I am creating a game called Hangman. Whenever the program asks the user to pick a number, if he/she enters a character more than one letter, the program outputs multiple times. How do I make it check if the length of the character equals one?
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
usingnamespace std;
int main()
{
char input;
cout << "Please enter a character" << endl;
cin >> input; // I would like to get the size of the character
return 0;
}
if you just need 1 character from the stream you can just throw away the rest if you don't need them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
usingnamespace std;
int main()
{
char input;
cout << "Please enter a character" << endl;
cin >> input; //get one character only
cin.ignore(1000,'\n');//throw away the rest.
cout<<input;//display the character.
return 0;
}
if your input can at times require more than one character, then char might not be the best type, you might consider std::string or something else