Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C++ declaration error
#1
Hello (C++) programmers of southperry. I'm having a really simple problem with this code that I can't figure out.

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

do {
    if (playersRespond == '1') {
      //do something;
    } else if (playersRespond == '2') {
      //do something;
    } else {
    cout << "Please type in 1, 2 or 3" << endl;
    }
  } while (playersRespond != 3);

char c;
cin >> c;
return 0;
}

My compiler says I have a "declaration error" at the line:

} while (playersRespond != 3);

I have no idea what I'm doing wrong.

edit: does it have something to do with me saying "while (playersRespond != 3)" when i already have an "else" statement within the "do"?
Reply
#2
You're missing the ctime and iostream headers and the std namespace. Plus you've got yourself a nice infinite loop. Smile

But back to your problem, might it be that you've missed the '' around the 3?
Reply
#3
shouldn't playersRespond be an integer?

Also, move the cin to inside the do-while loop so you don't run into an infinite loop.
Reply
#4
@Tempus
#include <cstdlib>
#include <iostream>
#include <ctime>

I have those ^. The "(playersRespond != '3')" might be the problem. It's a huge facepalm if it is. I don't have an IDE I can access until tomorrow so I can't check it (not to mention the actual code is in my lab's computer lol. This is one of the earlier saved code.)

Anyway if that isn't the problem, can it be something else?

@Fiel, I originally didn't have playersRespond as numbers. I want it to loop through 1 and 2 if the players types in anything that isn't 3.


edit: revised code based on Fiel's and Tempus' help

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

using namespace std;

int 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') {
      //do something;
    } else if (playersRespond == '2') {
      //do something;
    } else {
    cout << "Please type in 1, 2 or 3" << endl;
    }
  char c;
  cin >> c;
  } while (playersRespond != '3');

return 0;
}
Reply
#5
If you're defining playersRespond to be an integer, you need to remove the ' ' from around the 1, 2 and 3 (your if, else if and your while lines).

*Edit* What are you trying to do? Because if someone enters in a 1 or 2 you do something, otherwise you get the "Please enter 1, 2 or 3" message. You also get this message if playersRespond == 3 first time round. And why the char c bit? c has no impact, if playersRespond isn't 3 the 1st time around, you'll get an infinite loop. It makes no sense. Are you trying to do something like this?:
Code:
#include <ctime>
#include <iostream>

using namespace std;

void main() {
    srand(time(NULL));
    int guess = rand() % 99 + 1, playersRespond;
    cin >> playersRespond;
    do {
        if (playersRespond == 1) {
            // do something
        } else if (playersRespond == 2) {
            // do something else
        } else if (playersRespond == 3) {
            // response is already 3, break out of this?
            break;
        } else {
            cout << "Please type in 1, 2 or 3\n";
            cin >> playersRespond;
        }
    } while (playersRespond != 3);
}
Reply
#6
No need to break out of the loop. At the end of the do-while loop the condition will be false and the loop will terminate normally.

Code:
void main() {
    srand(time(NULL));
    int guess = rand() % 99 + 1;
        int playersRespond;

    do {
                cout << "Please enter 1 or 2. Enter 3 to terminate.\n";
                cin >> playersRespond;
        if (playersRespond == 1) {
            // do something
        } else if (playersRespond == 2) {
            // do something else
        }
    } while (playersRespond != 3);
}
Reply
#7
I think there was an include for if you use randoms, but i can't for the life of me remember what it was called.

By the way, i hate do whiles. Never used one since i learned about it. (aside from the assignment that required i used one)

Edit: What's 'c' doing and why is it there. What's this whole program doing actually. Goggleemoticon
Reply
#8
Edit: Don't mind me.

Edit2: And technically, there was nothing wrong with using a char. You just had to be consistent with using '1', '2', and '3' rather than 1, 2, and 3.
Reply
#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
#10
Well first you want the cin >> c to be cin >> playersRespond, since that's your variable.

Second, you can move the first cin >> playersRespond into the do-while loop and get rid of the second one. Conceptually, it does the same thing.

Here's a pseudocode because I'm too lazy to think in C++:

Code:
while true:
  have the computer guess according to the current range
  read in playersRespond
  case playersRespond:
    1: adjust the range
    2: adjust the range
    3: lucky guess. read in a char
      if char = 'y', reset the range
      otherwise, break the loop
    default: print instructions

Using break on an infinite loop because it's easier than figuring out what the terminating condition would be.
Reply
#11
Code:
#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

int main(void)
{
int upperBound;
int lowerBound;
int range;
int guess;
int playersRespond;

srand(time(NULL));

cout << "Upper Bound?\n";
cin >> upperBound;

cout << "Lower Bound?\n";
cin >> lowerBound;

if(upperBound < lowerBound)
{
    cout << "Improper ranges.\n";
    return 0;
  }

cout << "Enter 1 if the number I guess is too low.\n";
cout << "Enter 2 if the number I guess is correct.\n";
cout << "Enter 3 if the number I guess is too high.\n";

do {
    guess = lowerBound + (rand() % upperBound);
    cout << "Is your number" << guess << "?" << endl;
    cin >> playersRespond;
    switch(playersRespond)
    {
    case 1:
      lowerBound = guess;
      break;
    case 2:
      cout << "Damn I'm good!" << endl;
      break;
    case 3:
      upperBound = guess;
      break;
    default:
      cout << "Enter 1 if the number I guess is too low.\n";
      cout << "Enter 2 if the number I guess is correct.\n";
      cout << "Enter 3 if the number I guess is too high.\n";
    }
    if(lowerBound == upperBound)
    {
      cout << "You lying cheat! You switched numbers midgame!\n";
      return 0;
    }
  } while (playersRespond != 2);

  return 0;
}
Reply
#12
Technically, you can do lowerBound = guess + 1 and upperBound = guess - 1. But you'd have to adjust your anti-cheat bit.
Reply
#13
Thank you Fiel. Unfortunately, I don't have access to a compiler until tomorrow but from I'm reading, the program starts out asking the user for an upperBound and lowerBound, then the game doesn't actually start until the player enter a number?

I'm also looking at these lines

cout << "Upper Bound?\n";
cin >> upperBound;

If the user were to enter in 0, wouldn't there would be a division by 0 (osh-) when the program reaches the line "guess = lowerBound + (rand() % upperBound);"?
Reply
#14
Shouldn't it be (rand() % (upperBound - lowerBound + 1))?

In that case the only way there'd be a division by 0 is if upperBound < lowerBound, which is tested against.

And it asks for an upperBound and lowerBound, then displays the instructions (what 1, 2, and 3 mean), then displays a guess before asking a number.
Reply
#15
Russt Wrote:Shouldn't it be (rand() % (upperBound - lowerBound + 1))?

In that case the only way there'd be a division by 0 is if upperBound < lowerBound, which is tested against.

And it asks for an upperBound and lowerBound, then displays the instructions (what 1, 2, and 3 mean), then displays a guess before asking a number.

lowerBound = 25
upperBound = 75

rand() % (75 - 25 + 1)
rand() % (51)

So it will generate a number between 0 and 50 - not really what you want.
Reply
#16
guess = lowerBound + (rand() % (upperBound - lowerBound));

Is the magic formula that I was looking for. Fiel's code was generating some huge numbers beyond 100 Goggleemoticon

But thanks guys.
Reply
#17
Fiel Wrote:lowerBound = 25
upperBound = 75

rand() % (75 - 25 + 1)
rand() % (51)

So it will generate a number between 0 and 50 - not really what you want.
Yes it is. That expression is added to lowerBound, making the range exactly what it needs to be.

The +1 is needed, if you want to be accurate. Unless upperBound isn't supposed to be inclusive.
Reply
#18
Russt Wrote:Yes it is. That expression is added to lowerBound, making the range exactly what it needs to be.

The +1 is needed, if you want to be accurate. Unless upperBound isn't supposed to be inclusive.

Oh, I thought you meant that formula to replace the entire expression, not just that part of the formula. That's what had me confused.
Reply


Forum Jump:


Users browsing this thread: