Hello Tipper1997,
Forgot to mention this last time>
PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
Since I put all the comments in the program, I will post this to help you along:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
//using namespace std; // <--- Best not to use.
int main()
{ // <--- Best way to use {}s.
// declare variables
int number, guess, answer;
// get the system time
unsigned seed = time(0);
// seed the random number generator
srand(seed);
do
{ // <--- Channged.
// generate random number between 1 and 100
number = (rand() % (100 - 1 + 1)) + 1; // <--- All you need is rand() % 100 + 1;
//std::cout << "\n The computer's number is: " << number << '\n'; // <--- Used for testing.
// ask the user to guess the randomly generated number
std::cout << "\nGuess a number between 1 and 100 ( -9 to quit): "; // <--- Notice the "\n". This makes the output more readable.
std::cin >> guess;
if (guess = -9) // <--- To end the program.
break; // <--- Breaks out of the do/while loop.
for (int count = 0; count < 10; count++)
{
// validate the users input
if ((guess < 1) || (guess > 100))
{
// <--- The extra spaces after the "\n" are there to make this line different from the rest.
std::cout << "\n You must guess a number between 1 and 100. Guess again: ";
count--;
std::cin >> guess;
}
// <--- In all my testing these next line appear to work.
// the user has less than 5 guesses
else if ((guess == number) && (count < 5))
{
std::cout << "\nEither you know the secret or you got lucky!" << std::endl;
break;
}
// user has between 5 and 7 guesses
else if ((guess == number) && (count >= 5) && (count <= 7))
{
std::cout << "\nYou're pretty good at this!" << std::endl;
break;
}
// user has between 8 and 10 guess
else if ((guess == number) && (count >= 8) && (count <= 10))
{
std::cout << "\nYou'll do better next time.";
break;
}
// user has guessed more than 10 times
else if (count > 10)
{
std::cout << "\nSorry. You have taken too many guesses." << std::endl;
break;
}
// <--- This should be "user had guessed above the computer number. Not what your comment says.
// user has guessed above 100
else if (guess > number)
{
std::cout << "Too high, try again.: ";
std::cin >> guess;
//break;
}
// <--- Same idea as above.
// user has guessed less than 1
else if (guess < number)
{
std::cout << "Too low, try again.: ";
std::cin >> guess;
//break;
}
}
} while (1); // <--- The while needs a condition. 1 or "true" creates an endless loop.
// <--- These next lines I needed. Use them if you want or delete is you do not need.
// <--- Used mostly for testing in Debug mode. Removed if compiled for release.
// <--- Used to keep the console window open in Visual Studio Debug mode.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();
return 0;
}
|
Your "pch.h file sounds like the "stdafx.h" file needed in Visual Studio (VS). Eventually I figured out how to create an empty project where I did not need the "stdafx.h" file. You should have the same ability. In the mean time start your highlight one line lower. This will avoid the comments and questions about the "pch.h" file.
In the program noticed how I used the "\n"s to break up the output so that is is not all run together and becomes more readable.
I also added some code to break out of the do/while loop to end the program.
Hope this helps,
Andy