Thread: checking cin for words

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    25

    Exclamation checking cin for words

    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;
    }

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    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

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    25
    im a beginner so wut do you mean by that

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    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?

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Oh and to explain why you need
    Code:
    if(strcmp(name,"bob")==0)
    is because strcmp returns 0 on it being true and non zero on the comprason being false
    Woop?

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    i think you mean how to check if you typed a number or a word?

    well if this is the case you can use :

    Code:
    int main()
    {
      int num;
      
      if(cin >> num)
        cout << "a number";
      else
        cout << "not a number";  
      
      cout << endl;
      	
      return 0;
    }
    if not what you want just forget it ;-)

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    25
    thx prog but wut is the 30 after te char for???

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    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?

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    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.

  10. #10
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Quote Originally Posted by jlou
    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;
    }
    Yes the string class is safer to work with sorry
    Woop?

  11. #11
    01000011 00100000 0010000
    Join Date
    Jul 2004
    Posts
    38
    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:

    Quote Originally Posted by prog-bman
    Code:
    if(strcmp(name,"bob")==0)
    if you just use:

    Code:
    if(!strcmp(name,"bob"))
    Last edited by CodeNinja; 07-27-2004 at 09:16 AM.

  12. #12
    Registered User
    Join Date
    Jul 2004
    Posts
    25
    hey i used the code from jlou cause that looked easier and when i compile it i get 8 erros
    Code:
    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 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 this
    Code:
    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)
    i am using microsoft visual c++ 5.0 if that mattrers

  13. #13
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    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"))
    It compiles to exactly the same machine code, so it doesn't matter.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  14. #14
    1479
    Join Date
    Aug 2003
    Posts
    253
    Quote Originally Posted by killdragon
    Code:
    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)
    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!
    Knowledge is power and I want it all

    -0RealityFusion0-

  15. #15
    Registered User
    Join Date
    Jul 2004
    Posts
    25
    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??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. getline(function)??
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 12:42 PM
  4. cin not allowing input after first use of function
    By Peter5897 in forum C++ Programming
    Replies: 5
    Last Post: 01-31-2006, 06:29 PM
  5. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM