If command practice

This is a discussion on If command practice within the C++ Programming forums, part of the General Programming Boards category; This isnt much of a first post, but im learning and wanted to put together something off the top of ...

  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,046
    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,796
    >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
    Aug 2006
    Posts
    15
    wow thanks! so many replies.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934

    Thumbs up

    >And here's the updated and vastly improved versions:
    I had a feeling you had made some changes to some of your tutorials, but was unable to get anyone here to insert them.

    Also, I'm not sure I got the formula corrent on the code above to generate a number between 1 and 100, but hopefully it's close (didn't have time to test it).
    Last edited by swoopy; 09-01-2006 at 09:45 AM.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Also, I'm not sure I got the formula corrent on the code above to generate a
    >number between 1 and 100, but hopefully it's close
    Well, it works when RAND_MAX is less than INT_MAX. Otherwise you invoke undefined behavior with (RAND_MAX + 1). You can get around that by using 1.0 instead of 1, and that has the added benefit of forcing the entire expression to double so you can avoid the cast.

    Of course, now you have potential overflow with (rand() * 100) when you ditch the cast. I would probably use rand as late in the expression as possible, which means dividing first and then multiplying the result. Then a cast to int to remove the warning couldn't hurt:
    Code:
    int actual = static_cast<int>(1 + rand() / (RAND_MAX + 1.0) * 100);
    My best code is written with the delete key.

  12. #12
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Main returns an int:

    Code:
    int main
    {
        return 0;
    }
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Main returns an int:
    Who posted otherwise?
    My best code is written with the delete key.

  14. #14
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    It wasn't in his source, and nobody posted it.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >It wasn't in his source, and nobody posted it.
    Every piece of code in this thread properly defines main as returning int.
    My best code is written with the delete key.

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

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