wut do i use to check if cin equals a word and not a number like
Code:#include <iostream.h> int main() { int name; cin >> name; if (name == "bob") { cout << "hello john\n" } else { cout << "hello bob\n"; } return 0; }
wut do i use to check if cin equals a word and not a number like
Code:#include <iostream.h> int main() { int name; cin >> name; if (name == "bob") { cout << "hello john\n" } else { cout << "hello bob\n"; } return 0; }
You might try including <cstring> and declaring name as a string instead of an int. Or, since it looks like you don't want to use namespaces, include <string.h>.
If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein
im a beginner so wut do you mean by that
try this handy little function strcmp
Code:#include <iostream> #include <string> using namespace std; int main() { char name[30];//make it a char instead of a int cin.getline(name,30,'\n');//getline does not terminate at white spaces like cin>> if(strcmp(name,"bob")==0)//Very nice function :) { cout<<"Hi bob"<<endl; } else { cout<<"Hi"<<endl; } return 0; }
Woop?
Oh and to explain why you need
is because strcmp returns 0 on it being true and non zero on the comprason being falseCode:if(strcmp(name,"bob")==0)
Woop?
i think you mean how to check if you typed a number or a word?
well if this is the case you can use :
if not what you want just forget it ;-)Code:int main() { int num; if(cin >> num) cout << "a number"; else cout << "not a number"; cout << endl; return 0; }
thx prog but wut is the 30 after te char for???
its called an array basically its declaring name to be able to hold 30 char's try this on for size
Code:char name[30]="Brian";//declare a 30 element char array for(int i = 0,i<30,i++)//loop though to print out whats at which element { cout<<name[i]<<endl;//print out the current value at this element }
Woop?
prog-bman - In your example above you used strcmp which is in <cstring>, but you included <string> instead. That code won't work until you fix the header include.
killdragon - <cstring> holds c style string functions like strcmp. You might also recognize this as <string.h>, they are basically the same except <cstring> is the newer, preferred version.
If you want to learn strings to get words and stuff, I would suggest learning the string class. It might be easier to learn, and it is probably harder to mess up, than the C style character array strings used in prog-bman's example. The standard string class is also a more C++ way of doing things (as opposed to C). The header for the string class is <string>, and is completely different than <cstring> and <string.h>.
And just for more info, here is the sample program using the string class:Code:#include <iostream> #include <string> using namespace std; int main() { string name; // make it a string instead of a char array or int getline(cin, name); // this version of getline takes cin as an argument. if (name == "bob") { cout<<"Hi bob"<<endl; } else { cout<<"Hi"<<endl; } return 0; }
Last edited by jlou; 07-27-2004 at 12:10 AM.
Yes the string class is safer to work with sorryOriginally Posted by jlou
Woop?
I'm sure all of everyone's methods who replied work, but I (out of a bit of a challenge) created my own method. Basically it tests to see if 0-9 are included in the string.
Code:#include <iostream.h> #include <string.h> int main() { char name[30]; int i; int error_val; do { cout<<"Enter your name: "; cin.getline(name, 30, '\n'); cout<<"\nYour name is: "; for(i=0; i<=strlen(name); i++) { if(name[i]!='0' && name[i]!='1' && name[i]!='2' && name[i]!='3' && name[i]!='4' && name[i]!='5' && name[i]!='6' && name[i]!='7' && name[i]!='8' && name[i]!='9') { cout<<name[i]; error_val=0; } else { cout<<"\n\nPlease do not use numbers.\n\n"; error_val=1; break; } } }while(error_val!=0); return 0; }
Also, progbman, it's shorter than:
if you just use:Originally Posted by prog-bman
Code:if(!strcmp(name,"bob"))
Last edited by CodeNinja; 07-27-2004 at 09:16 AM.
hey i used the code from jlou cause that looked easier and when i compile it i get 8 erros
i get that when i compile the headers (#include <string.h>) with te .h part, when i take out the .h part of both of them i get two errors like thisCode:C:\Program Files\DevStudio\MyProjects\test\main.cpp(3) : error C2871: 'std' : does not exist or is not a namespace C:\Program Files\DevStudio\MyProjects\test\main.cpp(7) : error C2065: 'string' : undeclared identifier C:\Program Files\DevStudio\MyProjects\test\main.cpp(7) : error C2146: syntax error : missing ';' before identifier 'name' C:\Program Files\DevStudio\MyProjects\test\main.cpp(7) : error C2065: 'name' : undeclared identifier C:\Program Files\DevStudio\MyProjects\test\main.cpp(8) : error C2065: 'getline' : undeclared identifier C:\Program Files\DevStudio\MyProjects\test\main.cpp(9) : error C2446: '==' : no conversion from 'char *' to 'int' This conversion requires a reinterpret_cast, a C-style cast or function-style cast C:\Program Files\DevStudio\MyProjects\tes\main.cpp(32) : error C2143: syntax error : missing ';' before 'return' C:\Program Files\DevStudio\MyProjects\test\main.cpp(33) : error C2143: syntax error : missing ';' before '}' Error executing cl.exe. test.exe - 8 error(s), 0 warning(s)
i am using microsoft visual c++ 5.0 if that mattrersCode:C:\Program Files\DevStudio\MyProjects\test\main.cpp(32) : error C2143: syntax error : missing ';' before 'return' C:\Program Files\DevStudio\MyProjects\test\main.cpp(33) : error C2143: syntax error : missing ';' before '}' Error executing cl.exe. test.exe - 2 error(s), 0 warning(s)
It compiles to exactly the same machine code, so it doesn't matter.Also, progbman, it's shorter than:
Quote:
Originally Posted by prog-bman
Code:
if(strcmp(name,"bob")==0)
if you just use:
Code:
if(!strcmp(name,"bob"))
[email protected]
Microsoft Visual Studio .NET 2003 Enterprise Architect
Windows XP Pro
Code Tags
Programming FAQ
Tutorials
Well usually when the compiler tell you that you have errors and that you are missing the ; it usually means that you are missing them somewhere.....if you look closely enough I bet you can find them . If not, the error messages even tell you what line they are on. Good Luck!Originally Posted by killdragon
Knowledge is power and I want it all
-0RealityFusion0-
omg i found out that the problem was that i had two
return 0;
} and the second one was far down cause i erased other code tthat was there but forgot that one...hhaha,
can ne1 help me with this though,a run a program i have to clodse visual cpp cause it performed an illegal operation, its getting very irratating...ne know how to fix that??