Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have been trying to incorporate a check to see if the input from the user is a

ID: 3542660 • Letter: I

Question

I have been trying to incorporate a check to see if the input from the user is a valid input. For example my program wants the user to guess a number between 1-1000. My program works perfectly, except when the user inputs any other character other than a number it goes CRAZY. Anyways, I want it to check and make sure that the user is inputting numbers, not something silly. So I have been going in circles trying to figure this part out. I am sure it is a easy fix, but I am new to programming and this has got me stumped. Any help would be appreciated.



Explanation / Answer

#include "stdafx.h"

#include<iostream>

#include<cstdlib>

#include<ctime>


using namespace std;


int main()

{


bool isGuessed=true;

while(isGuessed)

{

srand(time(0));

int number=rand()%1000+1;

int guess;

char answer;



cout<<"Midterm Exercise 6 ";

cout<<"I have a number between 1 and 1000. ";

cout<<"Can you guess my number? ";

cout<<"Please type your first guess: ";

cin>>guess;

if(guess<=1000&&guess>=1)

{

while(guess!=number)

{

if(guess>number)

{cout<<" Too high. Try again! ";

cin>>guess;

if(guess<=1000&&guess>=1);

else

{cout<<"you have not entered the input appropriately";

break;

}

}

if(guess<number)

{

cout<<" Too low. Try again! ";

cin>>guess;

if(guess<=1000&&guess>=1);

else

{

cout<<"you have not entered the input appropriately";

break;

}

}

}

if(guess==number)

{

cout<<" Excellent! You have guess the number! ";

}

cout<<"Would you like to play again (y or n)? ";

cin>>answer;

cout<<" ";


if(answer!='y')

{

isGuessed=false;

cout<<"Thanks for playing! ";

system ("PAUSE");

return 0;

}

}

}



return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote