#include <cstdlib>
#include <iostream>
#include <time.h>
usingnamespace std;
int main(int argc, char *argv[])
{
int guess;
srand (time(NULL));
int rNum = rand() % 100 + 1; // rNum is random number
cout << "Guess the number (between 1-100): ";
while (guess != rNum){
cin >> guess;
if (guess < rNum) cout << "That number is too low, guess again!: ";
if (guess > rNum) cout << "That number is too high, guess again!: ";
if (guess == rNum) cout << "You win!\n";
}
system("PAUSE");
return EXIT_SUCCESS;
}
1) % gives the remainder. So rand() % 100 is from 0 to 99, rand() % 100 is from 1 to 100.
2) Check the logic. (guess == rNum) is in the body of the loop with condition (guess != rNum).
You can pull that out of the loop (you may even omit the if statement then :) ).
In the rewritten version it's fine...(because you prompt for input inside the loop)
[EDITED]
But now I see a problem, guess isn't initialized before line 13, it can be anything and can happen to be equal to rNum.
Try using do...while, so the condition is checked at the end of iteration.
[EDITED]
An easy way would be changing line 9 to like int guess = -1;, but, is this pretty? I don't know ;)
3)
1 2 3 4 5 6 7 8 9 10
// else codehere is ok
else
Foo();
// but if you wantneed more than one line for the else "body", add braces
else
{
Foo();
Bar();
}
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <string>
usingnamespace std;
int main(int argc, char *argv[])
{
int myGuess = 50;
string answer;
int myNum; // number that player chooses
int guessTrys = 7;
int previousGuess;
cout << "I am bob the Brilliant!\n";
cout << "I will guess any number between 1-100 in 7 guesses or less!\n";
cout << "ready? lets go!\n";
cout << "Is your number " << myGuess <<"? 'higher', 'lower', 'correct'\n";
while (myGuess != myNum ){
cin >> answer;
if (answer == "lower"){
myGuess = myGuess / 2;
guessTrys--;
cout << "Hmm, let me try again... is your number " << myGuess << "?";
}
elseif (answer == "higher"){
myGuess = myGuess + 25;
if (myGuess > 100){
myGuess = 100;
}
guessTrys--;
cout << "Hmm, let me try again... is your number " << myGuess << "?";
}
if (answer == "correct" && guessTrys > 0){
cout << "AHA, I win!";
}
elseif (answer == "correct" && guessTrys <= 0){
cout << "I failed to guess it in 7 trys, darn you!";
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
However, I don't know the formula for if it needs to go higher.. I can't just times it by 2. if I just add 1 The computer won't guess it in 7. If I add more it may skip over it. Any ideas?
Then I guess it should be removed manually. Anyway system() should be avoided...
So back to the problem, do you want the computer guess the number half-way in the range?
If so, I think of something like myGuess += (MAX - myGuess)/2, where MAX is the upper bound of the range...
well, the lower part works fine, it divides it by two. But lets say for instance my number is.. 11.. it starts at 50, then 25, then 12.. so thats 3 "guesses". But now I just want it to go down by 1. Ok so fine, can't do that, so it goes to 6. But now how do I get it to add the right amount? What if my number was 86.
Ill try to do what you suggested, though it's a little confusing (I'm a pretty new coder). Can you explain what myGuess += (MAX - myGuess)/2 does? If I understand it it takes myGuess and adds to it the MAX number (how do I define that again) minus the original / 2.
Sorry if this is a dumb question. Updated my previous code a little since my last post, its up.
Now, computer should guess the number halfway between 20 and 50, i.e. 20 + (50 - 20)/2 = 35.
If the dividend is an odd number, I believe it's just similar.