So I have learned a few things about c++ in the past week and have decided to try an exercise with what I've learned and make a number guessing game.
At times the game runs fine and gives me my Win message but at other times it'll just stop letting me input numbers even if i havent guessed correctly or ran out of tries. Also sometimes if i do guess correctly it won't count it as correct. Please help debug this!! I want to learn from my mistakes!!!
then that indicates you could probably refactor this to be in one loop.
I also think that this: while(t<=10 && y < 10 && x!= 1+(rand()%10)){
is over complicated. Surely you just want to loop ten times or until the user gets the right number?
okay so I need to find a way to contain it all into one loop that'll allow the first guess to be the same as any other guess and randomly created number. I'm not sure how I'm honestly not that advanced at all yet. I've been learning for a few days now. I need to know how to fix it so I can learn from that!
edit: one thing about rand: you should call it once per loop and then store this value in a variable. So if you need to test the number again you know you're using the same number.
I'll just mention how brand spanking new I am to this. I've been teaching my self for the past 6 days or so hahaha. Go ahead and MOCK! So I understand what is needed but I can't figure out how to go about it. Like I don't know what "bool" is yet or "const". I'm like level 1 noob. Which is why I kinda just want my code straight up fixed in the simplicity that it's in so I can learn what and how to go about certain things before moving on.
Edit: I want to master the little bit of knowledge I have in practice like this mini game before using stuff I haven't seen yet ^_^
i'm not mocking. i've given you an example and basically you need to plug in your random stuff.
if you don't know data types like bool yet i suggest you look here: http://www.cplusplus.com/doc/tutorial/variables/
in fact, you might want to skim through sections "Basics of C++" and "Program structure".
(const is just a way of telling the compiler you never intend to change the value of a variable, which stops a programmer accidentally changing it in the code as you'll get a compiler error).
#include <iostream>
int main()
{
constint maxNumberOfTries {10};
constint answer {4}; // this the user should guess
bool guessed {false}; // by default, the user has guessed wrong
// simpler condition on the loop
for ( int i = 0; i < maxNumberOfTries; ++i ) {
int number {0};
std::cout << "input: ";
std::cin >> number;
if ( answer == number ) {
// no output here, just store that guess was correct
guessed = true;
// terminate loop prematurely
break;
}
elseif ( 1 + i < maxNumberOfTries ) {
// on all but the last loop iteration
std::cout << "No, " << answer << " != " << number << ", try again\n";
}
}
// Now we know whether a guess was right or all times failed.
// Print appropriate message
if ( guessed ) {
// won, congratulate
}
else {
// lost, tell that game is over
}
return 0;
}
This also hints that one can do things in many ways. Some of the syntax requires C++11 support from the compiler. Depends on your compiler whether it has support and has it enabled by default.