Hello theforgottenone4,
As mentioned:
PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.
Along with the proper indenting 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.
|
Its not difficult to see that you are using the "Windows" operating system, but it quite often helps to mention the IDE that you are using. As different IDEs use different compilers and the header files are most often different.
Here is your code with the changes that are mentioned earlier. I also took the liberty to adjust the output to make it easier to read. This is more of a suggestion than something that you need to do.
I also rearranged your code with a few blank lines. This not only makes it easier for someone not familiar with your code to read. And that is what is most important.
Your use of {}s is totally your choice, but, IMHO, I find the "Allman" style the easiest to read and work with.
https://en.wikipedia.org/wiki/Indentation_style
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
//Setting up the program//
#include <iostream>
#include <limits> // <--- Added.
#include <string>
#include <ctime>
#include <cstdlib>
//using namespace std; // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/
//This code starts the random number generator that is used for the game//
//void genNumber()
//{
// srand(time(NULL));
//}
int main()
{
//Sets up variables and starts the random number generator that generates a random number between 1-20//
constexpr int MAXTURN{ 6 };
constexpr int MAXNUM{ 20 };
char again{};
int guess{}, num{}, turn{}; // <--- "turn" works better starting at 1.
std::string name;
//genNumber();
//turn = 0; // <--- Not needed here if you initialize the variable when defined.
srand(static_cast<size_t>(time(nullptr))); // <--- Only needs done once.
//Asking what the user's name is and starts the loop for the random number generator//
std::cout << "\n Hi, what is your name? ";
std::getline(std::cin, name); // <--- Changed.
std::cout << "\n Hi " << name << ", Welcome to the Random Number Guessing Game!\n Try to guess a random number between 1-"<<MAXNUM<< " within six tries.\n Good Luck!" << std::endl;
while (true)
{
num = (rand() % MAXNUM) + 1;
std::cout << "\n " << num << '\n'; // <--- used for testing. Comment out when done.
//Outputs the instructions for the game and starts the first input option for the game//
while (true)
{
std::cout << "\n Enter a number between 1 and 20 (" << 6 - turn << " tries left): ";
std::cin >> guess;
std::cin.ignore(); // <--- Removes the "\n", but not needed.
//Check is tries are taken up.
if (turn > MAXTURN) // <--- Changed.
{
break;
}
//Checks and sees if the user's guess is too high or too low and outputs which it is//
if (guess > num)
{
std::cout << "\n you guessed to high!" << std::endl;
}
else if (guess < num)
{
std::cout << "\n you guessed to low" << std::endl;
}
else
{
break;
}
//If the guess was not correct, the user loses a turn//
turn++;
}
//Checks for tries and if the user is out of tries you lose the game//
if (turn > MAXTURN) // <--- Changed.
{
std::cout << "\n Sorry, you're out of tries." << std::endl;
}
else
{
//Or, user won.
//std::cout << "\n\n You guessed the number, congratulations! You got it in " << turn + 1 << " tries" << std::endl;
//std::cout << "\n\n Congratulations " << name << "! You got it in " << turn + 1 << " trie(s)." << std::endl;
std::cout << "\n\n Congratulations " << name << "! You got it in " << turn + 1 << (turn + 1 == 1 ? " try." : " tries.") << std::endl;
}
//Resets the turn count after first try ends//
turn = 0;
//Asks the user if they would like to play again and starts the loop again based on the user's input///
while (true)
{
std::cout << "\n\n Want to keep playing? (Y/N)? ";
std::cin >> again;
std::cin.ignore(); // <--- Removes the "\n", but not needed.
//Sets up the possible answers to if the user would like to play again//
if (again == 'n' || again == 'N' || again == 'y' || again == 'Y')
{
system("cls");
break;
}
//else
//{
//}
}
//If the user decides to play again the game runs once more and if not then the game stops and outputs instructions to leave the game//
if (again == 'n' || again == 'N')
{
std::cout << "\n Thank you for playing! ";
break;
}
//else
//{
//}
}
std::cout << "\n\n Close the window to exit" << std::endl; // <--- Not really needed. Just looks bad in the output.
//Ends the code//
// The next line may not be needid. 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;
//system("pause"); // <--- Never reached. "return" leaves the program first.
}
|
For the header files I have found a couple of things that help. The first group are the header files that most any program will need and the ones that you use most often. I also found, and quite be accident, that putting these header files in alphabetical order helps when you forget one. The second group would be any header file that is specific to the program. Usually this would be header files like; "fstream", "algorithm", "sstream" and others. This could also include any header)file starting with "c" (<cstdlib>).
The last group would be any #include in double quotes. I the previous header files order does not make any difference, but in the case of the files in double quotes order does make a difference. That is why they would be last , so that the previous header files can cover anything that may be in your header file like; a "std::cout" or "std::string".
The comments on lines 9 and 10 should explain that.
In "main" the two constants are a better way of using these numbers. This way you only have one place to change these numbers and they will change everywhere they are used in the program.
The other variables, and this is more a personal choice, but I like to put them in the order of smallest amount of storage space to the largest. Actually the order here makes no difference.
You will notice the empty {}s after the variable names. From C++11 on these will initialize the "char"s to "\0", "int"s to (0) zero and "doubles" to "0.0". This may not always be needed, but I like to know that they have a value that is not the garbage that an uninitialized variable would have. Mostly it is for peace of mind and it also eliminates any compiler warnings about using an uninitialized variable.
The function call is not needed because of line 31. Over time I have found this to be the best way to use "srand". If you look it up "srand" take an "unsigned int" for the seed number and "time" does not return an "unsigned int" hence the type cast. Although calling "time" with (0) zero or "NULL" will work I read somewhere that "nullptr" is the best choice here.
On line 35 using "getline" is the better choice because you never know what a name will be.
Moving the call to "rand" will generate a new random number for each game.
The places where you have "cin.ignore();" will remove the "\n" from the input buffer, but the program will work fine without it.
At the end of your outer while loop you have two if statements with empty else statements. Just because have an if statement does not mean that you have to have an else statement. The program works just fine with out them.
I think the last lines before the "return" will work better for you.
Hope that helps,
Andy