If statments using char variable, need help

This is a discussion on If statments using char variable, need help within the C++ Programming forums, part of the General Programming Boards category; When writing a script using the char variable, using if statments.. how do you write it? I'm trying to make ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    11

    If statments using char variable, need help

    When writing a script using the char variable, using if statments.. how do you write it?

    I'm trying to make so that it checks if a specific name is typed, and if it is, it defines the name.

    Code:
    #include <iostream>	
    
    using namespace std;
    		
    int main()                            
    {
      char name;                           
      
      cout<<"Enter a name";                
      cin>> name;                         
      cin.ignore();                       
      if (  char*name () Shayne )  {                
         cout<<"Shayne is a twenty fifth dinosaur pagent.\n";                    
      }
      else if ( char*name () Jamie ) {           
         cout<<".... and so on and so on for like 10 names..\n";           
      }
      else {
        cout<<"....\n";     
      }
      cin.get();
    }
    That's what I am working with But I am pretty much a noob and can't get it to work. I get errors on the line with Shayne and Jamie.

  2. #2
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Yeah.... How about this:
    Code:
    #include <iostream>
    #include <string>	
    
    using namespace std;
    		
    int main()                            
    {
      char name[256];                           
      
      cout<<"Enter a name";                
      cin>> name;                         
      cin.ignore();                       
      if ( strcmp(name,"Shayne")==0 )  {                
         cout<<"Shayne is a twenty fifth dinosaur pagent.\n";                    
      }
      else if ( strcmp(name,"Jamie")==0 ) {           
         cout<<".... and so on and so on for like 10 names..\n";           
      }
      else {
        cout<<"....\n";     
      }
      cin.get();
    }
    You are trying to compare the variable name to those names right?
    Last edited by jmd15; 09-30-2005 at 08:43 PM.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    11
    Now it turns over even more errors. :S

  4. #4
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    What kind of "C++ Programmer" are you? o.o You forgot to include the header for strcmp, and I dont think that is the right thing to do in this case either...

    Well...
    Code:
    #include <iostream>	
    
    using namespace std;
    		
    int main()                            
    {
      string name;                           
      
      cout<<"Enter a name";                
      cin>> name;                         
      cin.ignore();                       
      if ( name == "Shayne" )  {                
         cout<<"Shayne is a twenty fifth dinosaur pagent.\n";                    
      }
      else if ( name == "Jamie" ) {           
         cout<<".... and so on and so on for like 10 names..\n";           
      }
      else {
        cout<<"....\n";     
      }
      cin.get();
    }
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Last time I checked, programmers are humans too. If I remember correctly humans DO make mistakes as well. What right do you have to say anything of that nature to me anyway?? How do you know, you don't know the slightest thing about me, so why don't you shutup. It's better habit to use strcmp() btw.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    11
    *Still a noob*

    It doesn't open

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    11
    Eeeek, nvm, it works, my thing was messed up.

    But now it closes after you type in the second name *eek*

  8. #8
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Shouldn't. cin.get(); should hold it up until you hit enter. My modified code about works perfectly for me, try it again.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    11
    Yours closes after 2 names as well... I dunno what's wrong..

  10. #10
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    You're trying to enter in two names?? The program isn't written to accept two names into it.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  11. #11
    Registered User
    Join Date
    Sep 2005
    Posts
    11
    Yeah. Well, I want it so that you can type in as many names as you want.. >_<

    And if you make comments on the stuff it might make me less noobish...

  12. #12
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Ok, this keeps taking in names until you enter exit as a name:
    Code:
    #include <iostream>
    #include <string>	
    
    using namespace std;
    		
    int main()                            
    {
      char name[256];
      
      main:
      cout<<"Enter a name: ";
      cin>> name;                         
      cin.ignore();   
      if(  strcmp(name,"exit")==0)
      return 0;                    
      if ( strcmp(name,"Shayne")==0 )               
         cout<<"Shayne is a twenty fifth dinosaur pagent.\n";                    
      else if ( strcmp(name,"Jamie")==0 )      
         cout<<".... and so on and so on for like 10 names..\n";           
      else
        cout<<"....\n";     
    
      cin.get();
      goto main;
    }
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  13. #13
    Registered User
    Join Date
    Sep 2005
    Posts
    11
    Ooohh, I get it.. Thanks!

  14. #14
    Registered User
    Join Date
    Sep 2005
    Posts
    11
    It only works for the first name... for thr rest, it says ,

    Enter a name: ....

    when you type in a second/third/etc. name.

  15. #15
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Don't you want to have the "Enter a name:" come up again after you enter a name already? I don't know what you want anymore, but you should try to alter the code to the way you want it. That makes for a better learning experience. Then, if you have altered the code and need additional help, repost the code you are working with now.

    EDIT:
    Sorry your problem is happening because of the "cin.getline()" function added in there. Just take that out, I left it in so you could read your results before being taken back to the main loop.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  3. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM
  4. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 11:11 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21