I'm a C newbie and I can't figure out how to have the user be able to type in a long word instead of a limit to 4 characters; for example:
char a;
cin>>a;
if (a=='alongword')
{
do stuff
}
What's the easiest way to do this?
Printable View
I'm a C newbie and I can't figure out how to have the user be able to type in a long word instead of a limit to 4 characters; for example:
char a;
cin>>a;
if (a=='alongword')
{
do stuff
}
What's the easiest way to do this?
char a;
A 'char' is a single character. As such, your program should only be acceptign a single character. What you need is an array.
char a[NUMBER];
Replace number with the number of characters you want them to be able to enter, then add one to that number.
Quzah.
And read a little more bout the cin. There's also cin.getline :)
char a;
cout << "enter a single char" << endl;
if(a == 'i')
cout << "you entered the character i" << endl;
char buffer[80];
cout << "enter a single word up to 79 char long" << endl;
//if(buffer == 'alongword') won't work because alongword is a string of char and should be so indicated by using "" not ' '. Also can't use the equality operator on strings, have to use strcmp(), or one of it's relatives.
Use <cstring> header file to manipulate strings. It's got a lot of good functions.