welp.... there it is in all it's ?glory? lol. Well, like many on this forum I am very very new to programming and thought I would try my hand in making one of these infamous number guessing games. Some of you skilled programmers may be able to look at this and say it's crap or can spot the bugs quickly. Well, if not I will try and explain what is going on here.
1) I wanted there to be different difficulty levels
2) I wanted each difficulty level to have a certain amount of attempts
3) I want it to count the number of tries for each one
4) I would like for it to say different things depending on how far away from the number you are, example if the number is 50 and the guess is 49 I would like for it to say "You are very close" but if they choose something like 2 I would like it to say "You are way too low" <-----This one really has me stumped.
5) I'm not sure whats going on with this program but it will only let me choose option #1 for the difficulty level????
6) The program completely ignores the loop at the end asking if the player would like to play again......?
1.) You can have multiple difficulty levels with only one variable if you want. You can use a switch case or an if else if structure to set the maximum value based on what level they want.
2.) Same as above but with attempts rather than maximum value.
3.) You can keep track of tries by subracting the remaining attempts from maximum number of attempts.
4.) Set some variables that are the range for very close and far away, and if their guess is less than or equal to that number, you output a message based on that range's meaning.
5.) Your while loop on line 39 has an ending brace in the wrong place.
6.) Not sure what your problem is right now. Your code is very long. Try answer = tolower(answer); //if answer is 'Y' it's now 'y'
Try these fixes and put in your code. If you have any questions, feel free to ask.
I finished writing it. If you want any help, then I have a working version that mostly follows what you want.
Some of what you wanted I was too lazy to do, but I have essentially the same thing you want.
@giblit
I did the same thing, but it was a little more variable, and I used abs(num - guess); where num is the set number and guess is what the user is guessing.
It's no problem I didn't feel called out. Also I just realized OP you have if 1...if 2... if 3... Have you considered a switch then a default answer/guess?
What do you mean messed up code::blocks? Also @xism why do you have lines 7 , 10 , 11 , 12, 13( in that function ), line 14 , and arguments if you're not using them? It seems you made it a lot harder than it needed to be.
Wow guys!!!! Thank you so much for all of this information. I thought about using the switch loops but heard they can be a bit buggy at times??? I know this must be frustrating, but some of this stuff is going over my head a little bit..... I still have so much to learn, and this program may be way above my skill level for now.
@xismn
- I honestly have no clue on the libraries you are using, as I am only using
#include <iostream>
using namespace std;
@GRex2595
-I will work with the switch loops and try and run them, but as stated above, I am not very familiar with them but seems easy enough.
I would just like to mention switches aren't loops they are pretty similar to if statements and they aren't buggy people just prefer if/else if besides for situations where you have a bunch of different options like 1-99 all do different things. It would be a lot more code to do a bunch of if/else vs case 1: case 2: case 3:...
Well, the program somehow opened all the #include files like iostream and they got messed up. I want to know how to restore the #include stuff to default settings. I already tried re-installing it. Thank you for your time.
@giblit
- LOL wow just shows how new I am to programming. I really do enjoy the challenge that it brings and I am always up for problem solving. I just hope that I will be able to make a career out of this one day....
Well Like I mentioned I was going to work on this code some more, well I have not messed with switches yet but I did write a code that works pretty well. The only thing with it is that once you run out of lives it does not ask you if you would like to play again....... has me stumped (BIG SURPRISE HERE!!! lol)
{
bool playagain = true;
while (playagain)
{
srand(time(0));
int number = rand()%100+1;;
int guess;
int numberOFguesses = 0;
int numberOFlives = 10;
char answer;
cout << "Im thinking of a number between 1-100.\n";
cout << "Take a guess, but be careful, you only have " << numberOFlives << " lives\n";
cout << "Your guess is: ";
cin >> guess;
// numberOFlives = 9;
while(guess != number )
{
if(guess < number && numberOFlives != 0)
{
numberOFlives--;
cout << "That cost you a life point, you only have " << numberOFlives << " lives left.\n";
cout << "Your choice is too low, guess again: ";
cin >> guess;
numberOFguesses++;
if(numberOFlives == 0)
{
cout << "-------------" << endl;
cout << "| GAME OVER |" << endl;
cout << "-------------" << endl;
}
}
if(guess > number && numberOFlives != 0)
{
numberOFlives--;
cout << "That cost you a life point, you only have " << numberOFlives << " lives left.\n";
cout << "Your choice is too high, guess again: ";
cin >> guess;
numberOFguesses++;
if(numberOFlives == 0)
{
cout << "-------------" << endl;
cout << "| GAME OVER |" << endl;
cout << "-------------" << endl;
}
}
if(guess == number && numberOFlives != 0)
{
numberOFguesses++;
cout << "Congrats!! You got it in " << numberOFguesses << " tries!!" << endl;
}
}
cout << "Would you like to play again? (y or n)" << endl;
cin >> answer;
if(answer != 'y')
{
playagain = false;
cout << "Hope you had fun playing!\n";
}
}
}
It looks like it should. One thing I can mention though instead of checking the number of "lives" left in your if statements why not loop only when there are lives left? So you could remove that check from all the if statements ( they should be if/else if related also ) then on line 19 change to while( guess != answer && lives > 0 )
Then on the next line you want to decrement the lives so put --lives;
Edit also can you properly format it it is kind of hard to read. Basically inside each curly brace you want to indent each line.
So when you have a curly in a curly you indent twice
WOW MAN!!!!! AHHH thank you so much!! you have been a huge help, I can't thank you enough. Here is my final (for now) project and it works beautifully!!!!
#include <iostream>
usingnamespace std;
int main()
{
bool playagain = true;
while (playagain)
{
srand(time(0));
int number = rand()%100+1;;
int guess;
int numberOFguesses = 0;
int numberOFlives = 10;
char answer;
cout << "Im thinking of a number between 1-100.\n";
cout << "Take a guess, but be careful, you only have " << numberOFlives << " lives\n";
cout << "Your guess is: ";
cin >> guess;
while(guess != number && numberOFlives > 0)
{
if(guess < number && numberOFlives != 0)
{
numberOFlives--;
cout << "That cost you a life point, you only have " << numberOFlives << " lives left.\n";
cout << "Your choice is too low, guess again: ";
cin >> guess;
numberOFguesses++;
if(numberOFlives == 0)
{
cout << "-------------" << endl;
cout << "| GAME OVER |" << endl;
cout << "-------------" << endl;
}
}
if(guess > number && numberOFlives != 0)
{
numberOFlives--;
cout << "That cost you a life point, you only have " << numberOFlives << " lives left.\n";
cout << "Your choice is too high, guess again: ";
cin >> guess;
numberOFguesses++;
if(numberOFlives == 0)
{
cout << "-------------" << endl;
cout << "| GAME OVER |" << endl;
cout << "-------------" << endl;
}
}
if(guess == number && numberOFlives != 0)
{
numberOFguesses++;
cout << "Congrats!! You got it in " << numberOFguesses << " tries!!" << endl;
}
}
cout << "Would you like to play again? (y or n)" << endl;
cin >> answer;
if(answer != 'y')
{
playagain = false;
cout << "Hope you had fun playing!\n";
}
}
}
I tried cleaning it up as much as I could. But again THANK YOU so much. Now I will start working on how to get different answers like if you are 5 numbers away it should say "You are very close" and if you're 30 away it will say "You are so far off" and things like that. I have already tried this
1 2 3 4 5 6 7 8 9 10
if(guess <= number - 10)
{
cout << "You are a little far off on the low side, guess again: ";
cin >> guess;
}
if(guess >= number + 10)
{
cout << "You are a little far off on the high side, guess again: ";
cin >> guess;
}
I would calculate the difference then depending on the difference output different things like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int difference = 0;
//loop
difference = guess - number; //if guess is greater its even
if( difference < 0 && difference > -11 )
{
std::cout << "You are a little too low" << std::endl;
}
elseif( difference > 0 && difference < 11 )
{
std::cout << "You are a little too high" << std::endl;
}