Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Desparate Help 1

Status
Not open for further replies.

gamerz

Programmer
Feb 24, 2005
1
US
I'm new user of the tek-tips and I need some
desperate help!!
On a program that chooses the number to be guessed by
selecting an integer at random in the range of 1 to
1000. The program then types:

I have a number between 1 and 1000
Can you guess my number?
Please type your first guess.

The player then types a first guess. The program
responds with one of the following:

1. Excellent! You guessed the number!
Would you like to play again (y or n)?
2. Too low. Try again
3. Too high. Try again

It was done in C but I couldn' understand it. (I'm new
to C++, and have never done C) My program is written
below and always gives me an infinite Could anyone help me?

Thanx!!!




#include <iostream>
#include <cstdlib>

using std::cout;
using std::cin;
using std::endl;

double guessed_num;
char response;
double rand_num;
double i;

int main()
{
rand_num = 1+ rand() % 1000;
do
{
cout << "I have a number between 1 & 1000.";
cout << "\nCan you guess my number?";
cout << "\n\nPlease type your first guess: ";
cin >> guessed_num;


if(guessed_num < 1 || guessed_num > 1000)
{
cout << "\nPlease enter a number between 1 & 1000";
cin >> guessed_num;
}

while (guessed_num != rand_num)
{
if(guessed_num < rand_num)
cout << "\nToo low. Try again.";

else if(guessed_num > rand_num)
cout << "\nToo high. Try again.";
}
cout<< "Excellent You've guessed the correct number";
cout<< "would you like to play again (y or n)?";
cin >> response;

} while(response != 'n')
return 0;
}





 
Code:
      while (guessed_num != rand_num)
     {
         if(guessed_num < rand_num)
               cout << "\nToo low. Try again.";

         else if(guessed_num > rand_num)
               cout << "\nToo high. Try again.";
     }
It's infinite loop: if guessed_num != rand_num is true it's true forever because of you don't change both vars in this loop.
Correct this loop and try again...
 
Oh, read Process TGML link on the posting form and use CODE tag to post your sources, please...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top