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; Oh... Thanx....

  1. #16
    Registered User
    Join Date
    Sep 2005
    Posts
    11
    Oh... Thanx.

  2. #17
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Read my previous post's edit.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  3. #18
    Registered User
    Join Date
    Sep 2005
    Posts
    11
    Ok, it works pretty well, but there's one thing thats bugging me. After every definition, it says the cout in my else statement.

  4. #19
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by pandapwnage
    Ok, it works pretty well, but there's one thing thats bugging me. After every definition, it says the cout in my else statement.
    Make sure you have all your definitions in else if statements

    if()
    else if()
    else if()
    else if()
    else

    jmd15, I wasnt refering to your coding abilities by saying "what kind of C++ Programmer are you". I said that because you posted C when your title says C++ What I said after that wasn't meant to continue from that as criticism, just pointing out so the OP knows, yah know.

    Just a note that the way this program is setup it wont be able to handle more than this simple program, and just so you know for later projects.. using a while statement would also work here, instead of goto, like so:

    Code:
    int main()                            
    {
      char name[256];
      
      while(1) {
      cout<<"Enter a name: ";
      cin>> name;
      cin.ignore();
      if(  strcmp(name,"exit")==0)
        break;
      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();
      }
    }
    Thats usually how its done.
    Last edited by Dae; 09-30-2005 at 09:51 PM.
    Warning: Have doubt in anything I post.

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

  5. #20
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Yes, that is also a good way to do it. Probably even a better way. Just a note, in your if statements(this includes else if, and else) you use brackets around the code to be executed if that condition is true. UNLESS you only have one line of code to be executed then you don't need brackets, but maybe is a good idea just to get into the habit so you don't forget them.

    @ Dae:
    Sorry for getting angry, I interpreted your comment in the wrong way.
    Last edited by jmd15; 10-01-2005 at 07:54 AM.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  6. #21
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    strcmp is in <cstring>, not <string>.

    You should use Dae's first version that uses the C++ string class (with one change: add #include <string>). It is easier and safer to use than the C-style character arrays in jmd15's examples.

Page 2 of 2 FirstFirst 12
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