-
using strings
I made a program where you input a string
then I made and if statement which will display a certain output depending on what was entered in the string but there is something wrong with the if statement can someone please tell me how to use and if statement to read a string I am not sure if I have to pass the whole string and if I do how exactly would I do that
I use visualC++ 6.0
thanx for any help
-
So you are evaluating a string that has been entered by the user?
But you have not said what you are evaluating......length?.....comparing to another string?.....
The if statement is dependant on the evaluation needed.
For instance if you wanted to check one string against another you would use strcmp(), to see how big the string was you could use strlen()................
It depends on what you are trying to do.....
-
Here...
I think this is what you mean:
char name[200]; // name of string
cin >> name;
cout << "\n\n";
if(name=="Hi")
{
cout << "Hello to you, too!";
}
else if(name=="Bye")
{
cout << "See ya later!";
}
Is that what you mean?
-
Woops, you cant compare strings like that.....
Code:
char name[200]; // name of string
cin >> name;
cout << "\n\n";
if(!strcmp("Hi",name))
{
cout << "Hello to you, too!";
}
else if(!strcmp("Bye",name))
{
cout << "See ya later!";
}
-
Both programs do the same thing :p
-
No, Fordy is right, with character arrays you have to use the functions from string.h. Your cannot compare them with the == operator.
-
Thanx
Yea I needed to compare the string to the condition in the if statement
thanx for your help
-
also wut does strcmp do exactly
-
strcmp....
compares 2 strings and returns 0 if they are identical.
Also returns < 0 if string 1 is less than string 2, and > 0 if otherwise....
-
Strings
I use gnu C++ in Emacs, and they allow use of standard operators with string variables. I don't use define strings as char arrays, but I use the string library to define the variable type. I know it works. Is it a question of propriety? I am in a c++ class, and the teahcer tols us to use the standard operators. Is this incorrect? Is it a library issue, or syntax?