I mean at runtime? Or does that still count?So it was the problem of the compiler not the language instruction
This is a discussion on Making a random number guessing game (exit failure? wtf) within the C++ Programming forums, part of the General Programming Boards category; So it was the problem of the compiler not the language instruction I mean at runtime? Or does that still ...
I mean at runtime? Or does that still count?So it was the problem of the compiler not the language instruction
Thought for the day:FLTK:The most fun you can have with your clothes on.You got big stones calling out GCC! (anduril462)
"If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"
goto statements are fine except they are evil. People are pressuring C++ standard boards ( IEEE 'n' stuff ) to remove it from the language as it is considered an illogical statement as it causes an 'illogical' execution flow. Simply put; if you need a goto statement, your code is in need of some TLC. Semantically goto statements can be viewed as sub-functions which breaks the semantics of a function which is a linear code flow.
The truth is goto staments are used internally ( not goto staments exactly but jumps to instructions ) i.e. loops.
This one works (thanks to help from my programming class teacher and some tweaking from myself), but I was wondering if there was any way to implement a hint system that displays one of the digits (i.e. the tens or ones place).Code:#include <iostream> #include <stdio.h> #include <stdlib.h> #include <time.h> using namespace std; int rnd(int range); void seedrnd(void); int main() { int myguess, compguess, atry=101, gcount=0, rang=100, r=1, i; while (r ==1) { gcount=0; seedrnd(); cout << "**************************************" << endl; cout << "Welcome to my number-guessing program!" << endl; cout << "**************************************" << endl; cout << "[press enter to continue] \n\n"; getchar(); cout << "I have picked a number between 1 and " << rang << endl; cout << "How many guesses do you think you'll need? \n\n"; cin >> myguess; compguess=rnd(rang) + 1; cout << "\nOK, here we go... \n"; while (atry != compguess) { retry: gcount++; cout << "Enter guess " << gcount << ": " << endl; cin >> atry; if (atry > compguess) { cout << "Sorry, too high. Guess again. " << endl; goto retry; } if (atry < compguess) { cout << "Sorry, too low. Guess again. " << endl; goto retry; } cout << "FINALLY! \n\n"; cout << "You needed " << gcount << " guesses, \n and estimated you would need "; cout << myguess; cout << ". Thanks for playing. " << endl; cout << "Press 1 to play the game again. " << endl; cin >> r; } cout << "\n"; cout << i << " "; } return (0); } int rnd(int range) { int r; r=rand()%range; return (r); } void seedrnd(void) { srand((unsigned)time(NULL)); }
Before anything else,
a) Get rid of goto.
b) Indent properly.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
OK, but I tried it without goto and it loops when you guess.
How do I work around this?
[edit- indent like this?
?]Code:return (0) } int rnd(int range) { int r; r=rand()%range; return (r); } void seedrnd(void) { srand((unsigned)time(NULL)); }
[edit #2- this is handy for indenting knowledge Wikipedia ftw]
Last edited by muffinman8641; 03-03-2011 at 08:07 AM.
You do it logically! Use loops such as while.
Formulate the proper logic in your mind, ie what is the invariant for which we loop?
That is, what conditions must we satisfy to keep asking?
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
Hint? I can't think of anything! ReplacingwithCode:if (atry > compguess)and removing the gotos did, as I predicted, loop it.Code:while (atry> compguess)
(by the way I accidentally lied. I meant to say thatmakes it go to "Finally" as if the guess was wrong AND correct!Code:if (atry > compguess) { cout << "Sorry, too high. Guess again. " << endl; }
You got MS Visual Studio???
No, Dev C++. Imma get a new one though, because I've been informed it's out of date. This makes me sad because I'm used to it. :[
Hint:
What do we want to do?
We want to ask the user to input a number until the user answers correct.
So I ask again: what is our condition for looping? When do we want to keep repeating the question?
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
We want to keep looping it until the user's answer is equal to compguess.
While creates an uncontrollable loop.
If takes us right to the end of the program.
If/Else does the same as if.
I think you might be referencing one I'm not familiar with. For? Not real good at those...
No, while is perfectly fine for these things.
So basically you said it yourself:
Loop until input guess == compguess.
While loops take inverted conditions, since they loop until the condition is false.
That is, we want to loop so long as our condition guess == compguess is not met.
Two ways to do this:
while (!guess == compguess))
or inverting the logic within and removing the not condition
while (guess != compguess)
Now put it into place and watch the magic.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
What does !guess do? I know != is not equal to, so something related to that?
It's like reverse psychology! You're a wizard!
/thanks
Oh God, I feel like a moron.
I have no idea what to do with this.Code:while (atry != compguess) { retry: gcount++; cout << "Enter guess " << gcount << ": " << endl; cin >> atry; if ((atry > compguess) || (atry < 1)) { cout << "Please enter a guess between 1 and " << rang << ". " << endl; goto retry; } while (atry == compguess) { cout << "Sorry, too high. Guess again. " << endl; goto retry; } while (atry == compguess) { cout << "Sorry, too low. Guess again. " << endl; goto retry; } cout << "FINALLY! \n\n";
What about the if ((atry > compguess) || (atry < 1)) { section?
Sorry for being annoying, I'm just a little confused.