# is this the best way...

• 06-04-2006
Hugo716
is this the best way...
IS this the best way to write this piece of code. or is there an easier way.

also could someone just post the small function that would work with it cuz i couldnt get it to work even though it looked fine.

Code:

```// Guessing Game #include <iostream> #include <ctime> using namespace std; int main() { int rNumber;  // Random number variable int Guess;    // Your input variable cout << "Welcome to Reece's, Guess the number game...\n\n\n\n\n"; // random number srand(time(NULL)); rNumber = rand() % 15 + 1; cout <<"Enter your answer: \n"; cin >> Guess; if (Guess > 15 || Guess < 1) cout << "ERROR. Enter number above 0 and below 15 please: \n"; cin >> Guess; if(Guess == rNumber) cout << "Congrats You Win!\n"; else cout << "You lose!\n"; cout << "The Number is " << rNumber << endl; system("PAUSE"); return 0; }```
cheers.
• 06-04-2006
whiteflags
If you wanted too, you could let the user guess the number until he gets it right, by carefully crafting a loop, but you did a good job regardless.

The reason your random number isn't working is because you forgot to #include <cstdlib>, where the srand and rand functions are declared.
• 06-05-2006
Hugo716
thanks mate.

hmm, ill try a loop for it.

would this work

while(Guess != rNumber)
cout << "Wrong, guess again!: \n";
cin >> Guess;
• 06-05-2006
Perspective
>would this work?

There's only one way to find out!
• 06-05-2006
Hugo716
Hmm ok.. lil problem.

if i type over 15 or below 0, it does what i told it to and replies with error, enter number below 15 and over 0, but when i do it again, it just misses this part out and says nope, wrong answer.
i want it to keep saying enter number below 15 and over 0 every time i type a number over 15 and under 0 lol.

Code:

```// Guessing Game #include <iostream> #include <ctime> using namespace std; int main() { int rNumber;  // Random number variable int Guess;    // Your input variable cout << "Welcome to Reece's, Guess the number game...\n\n\n\n\n"; // random number srand(time(NULL)); rNumber = rand() % 15 + 1; cout <<"Enter your answer: \n"; cin >> Guess; do{ cout <<"ERROR! Enter a number above 0 but below 15.\n"; break; } while(Guess > 15 || Guess < 0); do { cout << "Nope, wrong answer... Guess again: \n"; cin >> Guess; } while(Guess != rNumber); cout << "Congrats You Win!\n"; cout << "The Number is " << rNumber << endl; system("PAUSE"); return 0; }```
can someone please point out what im doing wrong with the first do.. while loop plz.
• 06-05-2006
ZuK
You have to think again about the logic.
Your first do while loop actually is no loop.
it is allwais entered and it will hit the break statment every time. That means not looping at all.
BTW the two loops have to be nested in some way.
Kurt
• 06-05-2006
Dave_Sinkula
I would do it something like this instead.
Code:

```  for ( ;; )   {       cout <<"Enter your answer: ";       cin >> Guess;       if ( Guess == rNumber )       {         break;       }       if ( Guess > 0 && Guess < 15 )       {         cout << "Nope, wrong answer...\n";       }       else       {         cout <<"ERROR! Enter a number above 0 but below 15.\n";       }   }```
• 06-05-2006
Hugo716
Cheers bud that done it!

may i just ask, what does for
Code:

`(;;)`
mean?

seen it loads but dont know what the ;; means, and not sure on FOR too. im gonna look into for in a min.

hugo.
• 06-05-2006
enderbean719
Infinite loop
Code:

`for (;;)`
This is just a way to do an infinite loop.

It has no variables, conditions, or increments.
The reason he made it an infinite loop was because he had an embedded condition with "break" in it.

A normal for loop would be

Code:

`for (var = __  ; var < __  ; var++)`
-if you take out everything, you still must have the 2 ";" left.

I think another way to do an infinite loop would be
Code:

```while (1) { }```
• 06-05-2006
Hugo716
okie dokie.

cheers bud.