String problem
Hey, this is a really simple question. I am making a text game and I was wondering how I would compare two strings in an if-then format.
Example:
int main()
{
char buffer[10];
cout <<"Please enter your class.Fighter,Mage,Theif"<<endl;
cin.get(buffer.10);
if(??? == "Fighter")
{
Fighter();
}
return(0);
}
I know its really simple, but im a newbie.(Man I hate it when people say that)Thanks.
"Good,evil,I got the gun"
Ash- Evil Dead
"Good,evil,I got the gun"Ash- Evil Dead
This seems to be the subject that confuses everyone when they first begin C/C++.
A string is actually an array of values that represent letters. Every string is ended with a zero so you know when the string ends.
char String[4];
String[0]=''b'';
String[1]=''i'';
String[2]=''g'';
String[3]=0; //if you forget the zero, cout or whatever will display a whole bunch of characters until it reaches a zero in memory.
cout << String;
//the output should be
big
If you want to find the value of a char, just cast it to an int or short like this;
cout << (short)String[0];
Casting forces one data type to be displayed as another. In this case since all text functions treat all char variables as letters, you have to cast it to another variable to view it''s value.
''char'' contains a value just like ''int'', ''long'' and so on. Actually it can contain a value of a range 0-255 unsigned, and -128-127 signed.
Hope this helps. I really wish they would have called ''char'' ''byte''.
Oh yeah. The best way to check if two strings match is by using the function ''strcmp(FirstString, SecondString)''. If it returns a zero, then they match.
A string is actually an array of values that represent letters. Every string is ended with a zero so you know when the string ends.
char String[4];
String[0]=''b'';
String[1]=''i'';
String[2]=''g'';
String[3]=0; //if you forget the zero, cout or whatever will display a whole bunch of characters until it reaches a zero in memory.
cout << String;
//the output should be
big
If you want to find the value of a char, just cast it to an int or short like this;
cout << (short)String[0];
Casting forces one data type to be displayed as another. In this case since all text functions treat all char variables as letters, you have to cast it to another variable to view it''s value.
''char'' contains a value just like ''int'', ''long'' and so on. Actually it can contain a value of a range 0-255 unsigned, and -128-127 signed.
Hope this helps. I really wish they would have called ''char'' ''byte''.
Oh yeah. The best way to check if two strings match is by using the function ''strcmp(FirstString, SecondString)''. If it returns a zero, then they match.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement