Thread: If command practice

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    15

    If command practice

    This isnt much of a first post, but im learning and wanted to put together something off the top of my head a "small game"
    The problem is that it allways gives me the command like if i pick the number 3, it gives me the words "Your too low, Congrats you win!!"
    Am I doing else wrong, why does it display the win command when I didnt win. Also can anyone point me to a way to recycle it so if u dont get the number right it doesnt terminate.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
        int number;
        
        cout<<"Please input a number: \n";
        cin>> number;
        cin.ignore();
        cout<<"You inputted: "<< number << "\n";
        cin.ignore();
    
    
        if ( number < 5 ) 
             cout<< " You are too low\n";
             
             else if ( number > 5 ) 
                  cout<< " You are too high\n";
                  
                  
             else ( number == 5 ); 
                  cout<< " Congrats you win!!\n";
         
    cin.get();
    }
    Last edited by Syllable; 08-31-2006 at 05:27 PM.

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    the problem is here
    Code:
             else ( number == 5 ); 
                  cout<< " Congrats you win!!\n";
    change this to
    Code:
             else
                  cout<< " Congrats you win!!\n";
    You don't need to specify a condition in an else statement, because else essentially means "do this if none of the previous conditions have been met". also, a semicolon at the end of an if or else statement will signal the end of that statement.
    Last edited by Bench82; 08-31-2006 at 05:48 PM.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Also can anyone point me to a way to recycle it so if u dont get the number right it doesnt terminate.
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int number;
    
       do {
          cout<<"Please input a number: " << endl;
          cin>> number;
          cin.ignore();
          cout<<"You inputted: "<< number << "\n";
          cin.ignore();
    
          if ( number < 5 ) 
             cout<< " You are too low\n";
          else if ( number > 5 ) 
             cout<< " You are too high\n";
       } while (number != 5);
                  
       cout<< " Congrats you win!!\n";
         
       cin.get();
    }
    Then you can add another loop to ask the player if they want to play again after they win.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    15
    thanks a bunch i got it

    Edit: What I forgot to ask, is how I can make the number random instead of 5, and if anyone has any recommendations of game programing type tutorial sites or e-books to get started with.
    Last edited by Syllable; 08-31-2006 at 09:03 PM.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >What I forgot to ask, is how I can make the number random instead of 5
    You can use the rand function. Maybe something like:
    Code:
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int number;
    
       //Seed the pseudo random number generator.  Do this only once.
       srand(time(NULL));
    
       //Generate a number between 1 and 100
       int actual = 1 + (double) rand() * 100 / (RAND_MAX + 1);
    
       do {
          cout<<"Please input a number: " << endl;
          cin>> number;
          cin.ignore();
          cout<<"You inputted: "<< number << "\n";
          cin.ignore();
    
          if ( number < actual ) 
             cout<< " You are too low\n";
          else if ( number > actual ) 
             cout<< " You are too high\n";
       } while (number != actual);
                  
       cout<< " Congrats you win!!\n";
         
       cin.get();
    }
    See the FAQ for more information on using rand
    Last edited by swoopy; 08-31-2006 at 11:39 PM.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's the FAQ entry (FAQ > How do I... (Level 1) > Generate random numbers?):
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    And here's Prelude's random number tutorial (FAQ > Prelude's Corner > Random Numbers):
    http://faq.cprogramming.com/cgi-bin/...&id=1073086407

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >And here's Prelude's random number tutorial (FAQ > Prelude's Corner > Random Numbers):
    And here's the updated and vastly improved versions:
    http://eternallyconfuzzled.com/tuts/random.html
    http://eternallyconfuzzled.com/articles/rand.html
    http://eternallyconfuzzled.com/libs/jsw_rand.html
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Main returns an int
    Perhaps you are referring to the lack of a return 0? That is not necessary for the main function. Zero is returned automatically if the function ends without a return statement.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Perhaps you are referring to the lack of a return 0?
    I hadn't thought of that, I bet that's to what Ideswa indeed was referrring. I normally add the return 0; also just for consistency.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Perhaps you are referring to the lack of a return 0?
    I was trying to get him to say it explicitly. Vague good advice is no better than bad advice.
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Aug 2006
    Posts
    15
    Yea thanks for the catch, i forget to return, but i should just start doing it and remembering it. Also anyone have any sites or tutorials on small games that are used in examples. Most books like my textbook C++ learning by deitel, gives me some stupid gradebook example which I hate.

Popular pages Recent additions subscribe to a feed