Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C++ declaration error
#9
Wow, a few people responded while I was writing this post. What I'm trying to do is a little long to explain but it's like this:

You think of a number between 1-100 and program will guess a random number between 1-100 and will keep guessing until it gets your number.

For example if you think of 50 and the program guesses 61, you tell it the guess is too high and the computer will guess between 1-60. The next ("random") guess it makes is 42 and so you tell it that it's too low. Now the program must generate a number between 43-60. So it loops until it guesses the number you're thinking of.

For playersRespond == 2 and 3, I haven't fully figured out how the code would look yet but so far, I have:


Code:
#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

void main()
{
srand(time(NULL));
int upperBound = 100;
int lowerBound = 1;
int range = upperBound - lowerBound;
int guess = 1 + rand() % range;
int playersRespond;
cin >> playersRespond;

do {
    if (playersRespond == 1) {
      upperBound = guess;
      range = upperBound - 1;
      cout << "Is your number" << guess << "?" << endl;
    } else if (playersRespond == 2) {
      lowerBound = guess;
      range = lowerBound + 1;
      cout << "Is your number" << guess << "?" << endl;
    } else if (playersRespond == 3) {
      cout << "Lucky guess. Would you like to play again? y/n" << endl; //I haven't figured out what to do here yet to loop it back to the beginning
    } else {
      cout << "Please type in 1 if guess is too high, 2 if guess is too low or 3 if guess is correct." << endl;
    }
  cin >> c;
  } while (!((playersRespond = 1) || (playersRespond = 2) || (playersRespond = 3)));
}
Reply


Messages In This Thread
C++ declaration error - by sky54264 - 2009-09-15, 12:35 PM
C++ declaration error - by Tempus - 2009-09-15, 12:56 PM
C++ declaration error - by Fiel - 2009-09-15, 01:02 PM
C++ declaration error - by sky54264 - 2009-09-15, 01:04 PM
C++ declaration error - by Tempus - 2009-09-15, 02:56 PM
C++ declaration error - by Fiel - 2009-09-15, 08:13 PM
C++ declaration error - by Takebacker - 2009-09-15, 08:35 PM
C++ declaration error - by Russt - 2009-09-15, 08:52 PM
C++ declaration error - by sky54264 - 2009-09-15, 08:56 PM
C++ declaration error - by Russt - 2009-09-15, 09:07 PM
C++ declaration error - by Fiel - 2009-09-15, 09:19 PM
C++ declaration error - by Russt - 2009-09-15, 10:15 PM
C++ declaration error - by sky54264 - 2009-09-15, 11:08 PM
C++ declaration error - by Russt - 2009-09-15, 11:35 PM
C++ declaration error - by Fiel - 2009-09-16, 09:30 AM
C++ declaration error - by sky54264 - 2009-09-16, 07:23 PM
C++ declaration error - by Russt - 2009-09-18, 08:22 PM
C++ declaration error - by Fiel - 2009-09-19, 05:05 PM

Forum Jump:


Users browsing this thread: