![]() |
|
C++ declaration error - Printable Version +- Southperry.net (https://www.southperry.net) +-- Forum: Social (https://www.southperry.net/forumdisplay.php?fid=14) +--- Forum: Rubik's Cube (https://www.southperry.net/forumdisplay.php?fid=58) +--- Thread: C++ declaration error (/showthread.php?tid=16368) |
C++ declaration error - sky54264 - 2009-09-15 Hello (C++) programmers of southperry. I'm having a really simple problem with this code that I can't figure out. Code: int main()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"? C++ declaration error - Tempus - 2009-09-15 You're missing the ctime and iostream headers and the std namespace. Plus you've got yourself a nice infinite loop. ![]() But back to your problem, might it be that you've missed the '' around the 3? C++ declaration error - Fiel - 2009-09-15 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. C++ declaration error - sky54264 - 2009-09-15 @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>C++ declaration error - Tempus - 2009-09-15 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>C++ declaration error - Fiel - 2009-09-15 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() {C++ declaration error - Takebacker - 2009-09-15 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. C++ declaration error - Russt - 2009-09-15 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. C++ declaration error - sky54264 - 2009-09-15 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>C++ declaration error - Russt - 2009-09-15 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:Using break on an infinite loop because it's easier than figuring out what the terminating condition would be. C++ declaration error - Fiel - 2009-09-15 Code: #include <cstdlib>C++ declaration error - Russt - 2009-09-15 Technically, you can do lowerBound = guess + 1 and upperBound = guess - 1. But you'd have to adjust your anti-cheat bit. C++ declaration error - sky54264 - 2009-09-15 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);"? C++ declaration error - Russt - 2009-09-15 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. C++ declaration error - Fiel - 2009-09-16 Russt Wrote:Shouldn't it be (rand() % (upperBound - lowerBound + 1))? 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. C++ declaration error - sky54264 - 2009-09-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 But thanks guys. C++ declaration error - Russt - 2009-09-18 Fiel Wrote:lowerBound = 25Yes 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. C++ declaration error - Fiel - 2009-09-19 Russt Wrote:Yes it is. That expression is added to lowerBound, making the range exactly what it needs to be. 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. |