Thank you codemonkey. I still am a semicolon noob. I finally figured out why it kept quitting, a stupidly simple answer:
Code:
int choice;
cout << "Wrong, guess again? (Y/N): ";
But the input is a character. So I changed to char and it seems to be gold.
Here is the final code, I added some things but it seems to be working perfect now. Thanks for all your help!
Code:
#include <stdlib.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand ( time(NULL) );
int number = (rand() % 15) + 1;
int guess;
int trycount = 0;
char choice;
cout << "Cheat? (Y/N) \n";
cin >> choice;
if (choice == 'Y' || choice == 'y')
cout << "The secret number is " << number << ".\n";
else if (choice == 'N' || choice == 'n')
cout << "Good for you.\n";
cout << "Please enter a guess: ";
cin >> guess;
if (guess < number)
cout << "Too low.\n";
else if (guess > number)
cout << "Too high.\n";
else
cout << "Right!\n";
while (guess != number && trycount < 5)
{
char choice;
cout << "Guess again? (Y/N): ";
cin >> choice;
if (choice == 'Y')
{
cout << "Guess again: ";
cin >> guess;
if (guess < number)
cout << "Too low \n";
else if (guess > number)
cout << "Too high \n";
else
cout << "You guessed the number!\n";
trycount++;
}
}
if (trycount == 5)
cout << "Too many tries! Better luck next time. ";
else
cout << "Bye!";
return 0;
}
Please, if you have any recommendations (even though it works) feel free to offer them.
Also codemonkey, what is the advantage/disadvantage of having "using namespace std" inside or outside the int main function body?