CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Sep 2009
    Posts
    30

    Please Help me with this program.

    I am trying to ask the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again". The program should also keep a count of the number of guesses that the user makes. The program should use a loop that repeats until the user correctly guesses the random number. The program should display the random number as well as the number of guesses. This is what I have so far but my loop is not working. Help!!!!
    Code:
    //This is a game called "The Guessing Game"
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    int main()
       { 
          int number;
          unsigned seed = time(0);
          srand(seed);
          seed = 1 + rand() % 100;
          cout << "Please try to guess a number from 1-100: "; 
          cin >> number;
       }
       
       while (number < 1 || number > 100)
          cout << "Try again. Please try to guess a number from 1-100: ";    
          
       
       system("pause");
       return 0;
       }

  2. #2
    Join Date
    Sep 2009
    Posts
    57

    Re: Please Help me with this program.

    Code:
    //This is a game called "The Guessing Game"
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    int main()
       { 
          int number;
          unsigned seed = time(0);
          srand(seed);
          seed = 1 + rand() &#37; 100;
          cout << "Please try to guess a number from 1-100: "; 
       }
       
       while(number != seed)
       {
          cin >> number;
          if(numb > seed)
             cout << "Number is too high!\n";
          else if(numb < seed)
             cout << "Number is too low!\n";
        }
    
        cout << "Congratulation! You won the game!\n";
       
       system("pause");
       return 0;
       }
    I'll give you the code, but this is a very easy task that you had to figure on your own.

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Please Help me with this program.

    Quote Originally Posted by salehhamadeh View Post
    Code:
    //This is a game called "The Guessing Game"
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    int main()
       { 
          int number;
          unsigned seed = time(0);
          srand(seed);
          seed = 1 + rand() % 100;
          cout << "Please try to guess a number from 1-100: "; 
       //}
       
       while(number != seed)
       {
          cin >> number;
          if(numb > seed)
             cout << "Number is too high!\n";
          else if(numb < seed)
             cout << "Number is too low!\n";
        }
    
        cout << "Congratulation! You won the game!\n";
       
       system("pause");
       return 0;
       }
    I'll give you the code, but this is a very easy task that you had to figure on your own.
    That and your "{" and "}" were not well balanced. Your "main" was ending at my quote (where it was closed). Once the main closed, the rest of the code could not compile.

    Also,

    Code:
          unsigned seed = time(0);
          srand(seed);
          seed = 1 + rand() % 100;
    It is a very bad idea to use the same variable for things things that have nothing in common. You should have a "seed" variable, and a "numToGuess" variable.
    Last edited by monarch_dodra; October 4th, 2009 at 04:14 PM.

  4. #4
    Join Date
    Sep 2009
    Posts
    30

    Re: Please Help me with this program.

    Quote Originally Posted by salehhamadeh View Post
    Code:
    //This is a game called "The Guessing Game"
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    int main()
       { 
          int number;
          unsigned seed = time(0);
          srand(seed);
          seed = 1 + rand() % 100;
          cout << "Please try to guess a number from 1-100: "; 
       }
       
       while(number != seed)
       {
          cin >> number;
          if(numb > seed)
             cout << "Number is too high!\n";
          else if(numb < seed)
             cout << "Number is too low!\n";
        }
    
        cout << "Congratulation! You won the game!\n";
       
       system("pause");
       return 0;
       }
    I'll give you the code, but this is a very easy task that you had to figure on your own.
    Thank you so much for your help.

    Quote Originally Posted by monarch_dodra View Post
    That and your "{" and "}" were not well balanced. Your "main" was ending at my quote (where it was closed). Once the main closed, the rest of the code could not compile.

    Also,

    Code:
          unsigned seed = time(0);
          srand(seed);
          seed = 1 + rand() % 100;
    It is a very bad idea to use the same variable for things things that have nothing in common. You should have a "seed" variable, and a "numToGuess" variable.
    Can you please explain why this was a bad idea?? Which variables have nothing in common?

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Please Help me with this program.

    You're re-using the seed variable for something totally unrelated (the generated number). That's what he means.

  6. #6
    Join Date
    Sep 2009
    Posts
    30

    Re: Please Help me with this program.

    Thank you all for the input. This is how the code looks
    Code:
    //This is a game called "The Guessing Game"
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    int main()
    {
          int number;
          unsigned seed = time(0);
          srand(seed);
          seed = 1 + rand() &#37; 100;
          cout << "Please try to guess a number from 1-100: "; 
        
       
       while(number != seed)
       
       {
          cin >> number;
          if(number > seed)
             cout << "\nNumber is too high!\n";
          if(number < seed)
             cout << "\nNumber is too low!\n";
      
        else if(number < 1 || number > 100)
             cout << "Number is not between 1-100. Try again.\n";  
        }
                  
            cout << "\nCongratulations! You won the game!\n";
         
       system("pause");
       return 0;
    }
    else if(number < 1 || number > 100)
    cout << "Number is not between 1-100. Try again.\n";

    I am having problems executing this. It couts but it also couts that number is to high ( if(number > seed)
    cout << "\nNumber is too high!\n"; )
    I don't want this I only want to cout that number is not between 1-100. What am I missing? Also which formula or function can I use to cout how many time user tried to guess the number?? cin.get??

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Please Help me with this program.

    Code:
     if(number > seed)
             cout << "\nNumber is too high!\n";
    if(number < seed)
        cout << "\nNumber is too low!\n";
    else if(number < 1 || number > 100)
    This is wrong. The very first thing you should test for is to see if the number is in range (between 1 and 100). Then you test whether it is > seed and then test for < seed.

    Also, take a look at the code I have above closely. You only do a test for out of range if the number is < seed? That doesn't sound right. What happens if the number is too high? First, the number is > seed, so you output "Number is too high", regardless of whether it is in range or not.

    Make it simple -- test for the out of range first and get that out of the way. Once it is established that the number is in range, then you test if the number is < seed or > seed.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Please Help me with this program.

    Quote Originally Posted by Kl2eativ View Post
    Also which formula or function can I use to cout how many time user tried to guess the number?? cin.get??
    You don't need a formula or function. All you need is a counter starting at 0 and increment it each time the loop is executed to do the guess.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Sep 2009
    Posts
    30

    Re: Please Help me with this program.

    Quote Originally Posted by Paul McKenzie View Post
    Code:
     if(number > seed)
             cout << "\nNumber is too high!\n";
    if(number < seed)
        cout << "\nNumber is too low!\n";
    else if(number < 1 || number > 100)
    This is wrong. The very first thing you should test for is to see if the number is in range (between 1 and 100). Then you test whether it is > seed and then test for < seed.

    Also, take a look at the code I have above closely. You only do a test for out of range if the number is < seed? That doesn't sound right. What happens if the number is too high? First, the number is > seed, so you output "Number is too high", regardless of whether it is in range or not.

    Make it simple -- test for the out of range first and get that out of the way. Once it is established that the number is in range, then you test if the number is < seed or > seed.

    Regards,

    Paul McKenzie
    Awsome Thank you... The only thing I need is how do I get how many time the user tried to guess the number?

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Please Help me with this program.

    Quote Originally Posted by Kl2eativ View Post
    Awsome Thank you... The only thing I need is how do I get how many time the user tried to guess the number?
    I mentioned it in my last post.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Sep 2009
    Posts
    30

    Re: Please Help me with this program.

    Quote Originally Posted by Paul McKenzie View Post
    I mentioned it in my last post.

    Regards,

    Paul McKenzie
    I did what you recommended and it worked great. Please take a look.
    Code:
    /This is a game called "The Guessing Game"
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    int main()
    {
          int number;
          unsigned seed = time(0);
          srand(seed);
          seed = 1 + rand() &#37; 100;
          cout << "Please try to guess a number from 1-100: "; 
        
       
       while(number != seed)
         { 
          cin >> number;
          if(number < 1 || number > 100)
             cout << "\nNumber is not between 1-100. Try again: ";  
          
          else if(number > seed)
             cout << "\nNumber is too high! Try again: ";
          
          else if(number < seed)
             cout << "\nNumber is too low! Try again: ";
         }
            cout << "\nThe number was: " << number << endl;
            cout << "\nCongratulations! You won the game!\n\n";
              
       system("pause");
       return 0;
    }
    The only thing I need is at the end after the cout statement The number was, I need it also to Say You have made "x" amount of attempts!!
    Last edited by Kl2eativ; October 4th, 2009 at 11:07 PM.

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Please Help me with this program.

    Quote Originally Posted by Kl2eativ View Post
    The only thing I need is at the end after the cout statement The number was, I need it also to Say You have made "x" amount of attempts!!
    Well, this is going to be the third time I've mentioned it:

    You need another variable to keep count of how many times the loop is executed. Output the value of that counter at the end.

    Regards,

    Paul McKenzie

  13. #13
    Join Date
    Sep 2009
    Posts
    30

    Re: Please Help me with this program.

    Quote Originally Posted by Paul McKenzie View Post
    Well, this is going to be the third time I've mentioned it:

    You need another variable to keep count of how many times the loop is executed. Output the value of that counter at the end.

    Regards,

    Paul McKenzie
    I did not see where u mentioned to create another value for the loop. Or is it me that just can't read?? Thank you for your help and time!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured