Hi, I need help with my C++ code. The steps says that I need to allow uppercase
ID: 3885520 • Letter: H
Question
Hi, I need help with my C++ code. The steps says that I need to allow uppercase or lowercase Y and N as the answers. I need to decide how to handle input validation -- that is, what to do if the user enters something other than Y or N. So, I need to add something that blocks something other than Y or N in the code when the question of would you like to play again in the code appears wihout using return 0 since it is not allowed. Thanks
#include <iostream>
using std::cout; //allows the program to perform input and output
using std::cin;
using std::endl;
#include<cstdlib>
//Library to tell time
#include <ctime>
using std::time;
//Function templates
int getRandomInt();
int printMessage(int, int);
int main()
{
//Print my name and this assignment's title
cout << "LAB 4c: Tabular Formatting [ Guess.cpp ] ";
cout << "Programmer: Juan Gomez ";
cout << "Editor(s) used: Sublime Text 3 ";
cout << "Compiler(s) used: Visual Studio 2015";
cout << "File: " << __FILE__ << endl;
cout << "Compiled: " << __DATE__ << " at " << __TIME__ << endl << endl;
bool playAgain = true;
while (playAgain) //While playAgain is true, the loop will continue until it is false.
{
//Declaring the variable for the buffer.
//Declaring integer variable for 'userGuess' so that the user can input the number guessed.
//Declaring integer variable 'randomInt' so that the random machine choosen by the machine
//in the function 'getRandomInt' is passed to the integer variable 'randomInt'.
char buf[100];
int userGuess;
int randomInt = getRandomInt();
//Prompt user for input to choose a number between 1 and 1000
//and guess the number from the machine.
//The user will enter the number guessed and save it in the buffer
//using the 'string buffer method' and converted to integer by 'atoi'.
cout << "I have a number between 1 and 1000" << endl;
cout << "Can you guess my number?" << endl;
cout << "Please type your first guess: ";
cin >> buf; userGuess = atoi(buf);
cin.ignore(1000, 10);
while (true) //While the loop is true,the loop for 'userGuess' inside of the function 'printMessage'
{ //will continue until it is false and the user does not want to play anymore.
//Calling the function 'printMessage' so that the logic for repeating guesses can work.
int returnVal = printMessage(userGuess, randomInt);
if (returnVal == 1) //if the user chooses 'y' the loop for repeating guesses will be
//called again.
{
//Loop to display the user if he/she wants to play again the game.
//'Buffer method' to allow the user to input 'y' or 'Y' for yes
//and 'n' or 'N' for no.
//If the user chooses 'n' the loop will return false and the loop will break
//If the user chooses 'y' the loop will break but the program will start again with
//the guessing game.
cout << "Would you like to play again (y or n)?: ";
cin >> buf;
cin.ignore(1000, 10);
if (buf[0] == 'y' || buf[0] == 'Y')
{
break;
}
if (buf[0] == 'n' || buf[0] == 'N')
{
playAgain = false; //If the user hits 'n' it will convert 'playAgain' false.
break;
}
if (buf[0] != 'y' && buf[0] != 'Y'&& buf[0] != 'n' && buf[0] != 'N')
{
cout << "Please enter correct input.. either 'y' or 'n'" << endl;
}
}
else
{
cin >> buf; userGuess = atoi(buf); //'Buffer method' to allow the user to input the numbers until
cin.ignore(1000, 10); //the guess is correct.
}
}
if (playAgain == false) //The program will check if 'playAgain' is false.
{ //If it is false 'playAgain' will return false and will break the loop.
break;
}
}
}
ompiled: Sep 16 2017 at 22:43:40 I have a number between 1 and 1000 an you guess my number? Please type your first guess: 300 oo high. Try again: 406e oo high. Try again: 206e oo low. Try again: 250 oo low. Try again: 27e oo low. Try again: 286e oo high. Try again: 277 oo high. Try again: 276 oo high. Try again: 275 oo high. Try again: 274 oo high. Try again: 273 oo high. Try again: 272 oo high. Try again: 271 Excellent! You guessed the number! ould you like to play again (y or n)?: ds Please enter correct input .. either 'y' or 'n' Excellent! You guessed the number! ould you like to play again (y or n)?:Explanation / Answer
//Please see the code below
#include <iostream>
#include <cstdlib>
#include <ctime>
using std::cout; //allows the program to perform input and output
using std::cin;
using std::endl;
int main(void) {
srand(time(NULL)); // To not have the same numbers over and over again.
while(true) {
char answer;
int number = rand() % 1000; // System number is stored in here.
int guess; // User guess is stored in here.
int tries = 0; // Number of tries is stored here.
//std::cout << number << " "; // Was used for debug...
while(true) { // Get user number loop.
// Get number.
std::cout << "I have a number between 1 and 1000 " <<endl;
cout << "Can you guess my number?" << endl;
cout << "Please type your first guess: ";
label_01:
std::cin >> guess;
std::cin.ignore();
// Check number.
if(guess > number) {
std::cout << "Too high! Try again. : ";
goto label_01;
} else if(guess < number) {
std::cout << "Too low! Try again. : ";
goto label_01;
} else {
break;
}
// If not number, increment tries.
tries++;
}
std::cout<<"Excellent! " << std::endl;
std::cout<<"You guessed the number" << endl;
while(true) { // Loop to ask user is he/she would like to play again.
// Get user response.
std::cout << "Would you like to play again (Y/N)? ";
std::cin >> answer;
std::cin.ignore();
// Check if proper response.
if(answer == 'n' || answer == 'N' || answer == 'y' || answer == 'Y') {
break;
} else {
std::cout << "Please enter 'Y' or 'N'... ";
}
}
// Check user's input and run again or exit;
if(answer == 'n' || answer == 'N') {
std::cout << "Thank you for playing!";
break;
} else {
std::cout << " ";
}
}
// Safely exit.
std::cout << " Enter anything to exit. . . ";
std::cin.ignore();
return 0;
}
OUTPUT:
I have a number between 1 and 1000
Can you guess my number?
Please type your first guess: 300
Too high! Try again. :200
Too low! Try again. :250
Too low! Try again. :280
Too high! Try again. :275
Excellent!
You guessed the number
Would you like to play again (Y/N)? sa
Please enter 'Y' or 'N'...
Would you like to play again (Y/N)?
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.