views:

47

answers:

2

Trying to code a guess how many gumballs in the gumballs jar, kinda thing. There are two problems I keep getting:

  1. It's supposed to say if the guess is to high or to low but that only happens when i enter a number over 1000 and it says: Enter your guess: 1001 Too High!

Too Low!

Enter your guess:

  1. If you type a letter or phrase it goes haywire saying:

Enter your guess: Too High!

Too Low!

but it keeps saying that at about a couple hundred maybe even a thousand times a minute -.-

#include<iostream>
#include<ctime>
using namespace std;

int main(void)
{
     int iGumballs;
     int iUserguess;
     int iGuesses = 0;

    while(true)
    {
           system("CLS");
           cin.clear();
           iGuesses = 0;

    srand(static_cast<unsigned int>(time(0)));
    iGumballs = rand()%1000+1;
    cout << "How many gumballs are in the gumball jar, you guess!" << endl;
    do
    {
        cout << "Enter your guess: ";
    cin>> iUserguess;
    if(iUserguess > iGumballs)
    {
                  cout << "Too High!" << endl << endl;
    }
    if(iUserguess > iGumballs)
    {
                  cout << "Too Low!" << endl << endl;
    }
    iGuesses ++;
}while(iUserguess > iGumballs || iUserguess < iGumballs);
cout << "You guessed the right amount of gumballs! High Five!" << endl << endl;
cout << "You took" << iGuesses << " guesses" << endl << endl;
system("PAUSE");
}
return 0;
}
+1  A: 

Why don't you post the parts of the code you're having trouble with, instead of just giving us a dump of everything and asking us to figure it out?

Based solely on your description of the problem, I would recommend you:

  1. Add input validation (which will solve your second issue)
  2. Have another look over the code that checks whether the number is too high/low
  3. If you still can't figure it out, post the section of the code that's giving you trouble and explain what you've tried and what went wrong.
Anon.
Well I can't really post the part of the code for the second problem because I didn't know what was causing that problem. As you can probably tell by the program type I'm pretty new to C++, so can you explain how input validation works?
Cra2y
+3  A: 

Let me hint at one problem. Let's take a look at the two checks you have for printing the message "too high" and "too low":

    if(iUserguess > iGumballs)
    if(iUserguess > iGumballs)

Notice anything wrong when I put them together?

R Samuel Klatchko
Ok, thanks. That problems fixed now :)
Cra2y