By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,245 Members | 884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,245 IT Pros & Developers. It's quick & easy.

Guess Program

P: 41
Hi,

I'm starting to learn C++. And I have this assignment and I can't compile it for some reason..
Here is the problem:

-> Write a number guessing name program named guess. The program will generate a pseudo random number in the range 0 to 99. The program will then enter an infinite loop and will:
1 - prompt the user to enter a guess between 0 and 99.
2 - read the guess
3 - if the guess and the random number are equal
a - the program will print a success message
b - exit the program
4 - if the guess is less than the random number, the program will print Low and return to 1
5 - if the guess is greater than the random number, the program will print high and return to 1
6- if the guess is less than 0, the program will terminate

Here is my source code.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     srand ((unsigned)time(0));
  10.     int random = rand() %100;
  11.     int numbers = 0;
  12.  
  13.     do
  14.     {
  15.         cout << "Please enter a number between 0 and 99: ";
  16.         cin >> numbers;
  17.  
  18.         if ( numbers == random )
  19.         {
  20.             cout << "Congratulations, you got the right number.";
  21.             exit(0);
  22.         }
  23.  
  24.  
  25.          else if ( numbers > random )
  26.         {
  27.             cout << "High";
  28.             continue;
  29.         }
  30.  
  31.         else if ( numbers < random )
  32.         {
  33.             cout << "Low";
  34.             continue;
  35.         }
  36.  
  37.                                 else if ( numbers < 0 )
  38.                                 {
  39.                                     exit(0);
  40.                                 }
  41.     }
  42.  
  43.     return 0;
  44.  
  45. }
  46.  
Thank you,

Doug
Jan 26 '07 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,510
try replacing
Expand|Select|Wrap|Line Numbers
  1.     do
  2.     {
  3.  
with
Expand|Select|Wrap|Line Numbers
  1.     while(1)   // ** loop forever
  2.     {
  3.  
if will then compile
Jan 26 '07 #2

P: 41
Thank you so much,

Doug
Jan 26 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.