Thread: simple question asking program

  1. #1
    Me/R10
    Join Date
    Aug 2005
    Posts
    5

    simple question asking program

    im trying to make a simple test program that asks simple questions and such. heres the code i made but it doesnt work.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
        
        char name [20];
        
        char quest [5];
        
        cout<<"Hi whats your name?\n";
        cin>>name;
        cin.ignore();
        
        cout<<"hello "<<name<<" are you tall? (yes or no)\n";
        cin>>quest;
        cin.ignore();
        
        if ( quest=yes ){
             cout<<"thats nice and your tall or so you say anyways\n";
             }
             else if ( quest=no ){
                  cout<<" hah your short and im a short program\n";
                  }
                  else {
                       cout<<" YES OR NO!!!!!?? \n";
                       cin>>quest;
                       cin.ignore();
                       }
                       cout<<"press enter to quit";
        
        cin.get();
        
    }
    i just want to get it to say different sentences based on whether you said yes or no to the question.

  2. #2
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    You should use: if ( strcmp ( quest, "yes" ) ){ instead of if ( quest=yes ){. Also with no.
    Oh and I see it needs to be 'true' so I think it is if ( ! strcmp ( quest, "yes" ) ){.
    Last edited by Yuri; 08-17-2005 at 05:44 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You should use the C++ string class. It is easier and safer. Also be aware that = means assign and == means compare, unless you are talking about C style strings like quest, in which case you must use strcmp as Yuri stated.

  4. #4
    Me/R10
    Join Date
    Aug 2005
    Posts
    5
    thank you! it worked. but when i ddint put the ! it switched around the answer. when i say no it says the answer for yes and vice versa. why is that and whats the ! for? im just curious because im very inexperienced

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Code:
    a == b // is a equal to b?
    a != b // is a not equal to b?
    !(a == b) // same as a != b, but a little harder to read
    a = b // set a equal to b
    And, some shortcuts:
    Code:
    (!a) : evaluates to true iff a is false (as a boolean), 
    0 (as an integer), or NULL (as a pointer)
    
    (a) : evaluates to true iff a is true (as a boolean),
    non-0 (as an integer), non-NULL 
    (as a pointer -- even if it points to garbage)
    strcmp returns 0 iff the two arguments are equal, so !strcmp(str1, str2) evaluates to true if the strings are equal.
    Last edited by Zach L.; 08-17-2005 at 06:37 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    I use '!' like this:
    Code:
        cout<<"Enter the Password: ";
        cin>> Code1;
                
        if ( ! strcmp ( Code1, "Quit" ) || ! strcmp ( Code1, "quit" ) ){
        exit( 0 );
        }
    
    And if you enter "Quit" or "quit" the program quit. Else, he compares the Password.
    It is the same as:
    
        if ( strcmp ( Code1, "Quit" ) == 0 || strcmp ( Code1, "quit" ) == 0 ){
        exit( 0 );
        }
    
    But '!' is smaller then '== 0'.
    And of corse have those two the same effect:
    
        if ( strcmp ( Code1, "Quit" ) != 0 || strcmp ( Code1, "quit" ) != 0 ){
        exit( 0 );
        }
    
        if ( strcmp ( Code1, "Quit" ) || strcmp ( Code1, "quit" ) ){
        exit( 0 );
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Possibly simple question
    By PAragonxd in forum C++ Programming
    Replies: 1
    Last Post: 09-14-2008, 02:43 AM
  2. Replies: 16
    Last Post: 09-30-2007, 11:08 PM
  3. Problem with simple XOR program
    By spike_ in forum C++ Programming
    Replies: 8
    Last Post: 08-17-2005, 12:09 AM
  4. Mystery bug in simple program? - I am beginner
    By archie123 in forum C++ Programming
    Replies: 7
    Last Post: 04-08-2005, 07:23 AM
  5. question about the loop in case conversion program
    By Elhaz in forum C++ Programming
    Replies: 8
    Last Post: 09-20-2004, 04:06 PM