Restart the program after users input. | Sololearn: Learn to code for FREE!


Restart the program after users input.

Hello I am stuck in the last part of this program. When the user press y, the program would have to run from beginning. It actually does but gives me some crazy number. Try running the code below. Thanks. #include <iostream> using namespace std; int midpoint; int y = 100; int x = 1; char check; char tryagain; int main(){ do{ cout<<"Think about a number between 1 and 100."; midpoint = ( x + y ) / 2; while(x != y){ cout<<endl <<"Is it"<< " "<< midpoint << " "<< "?"; cin>>check; if(check=='l' || check=='L') { y = midpoint; midpoint = ( x + y ) / 2; } else if(check=='h' || check=='H') { x = midpoint; midpoint = ( x + y ) /2; } else if( check == 'c' || check == 'C') { break; } else{ cout<<"Incorrect choice."<<endl; } } cout<< "Great! Do you want to try again? (y/n)"; cin >> tryagain; }while (tryagain == 'y'); return 0; }

2/20/2017 8:51:30 PM

Safal Basnet

4 Answers

New Answer


On the first view I think you have to reset x and y.


Just like @TashiN said, you need to reset your x and y. You can do this by declaring and defining x and y inside the do loop section: do { int x = 1; int y = 100; } while...


so you should display the valid options that the user could enter as the choices. if you were to test this via blackbox, it would be impossible to determine what is going on. prompts are important. as for the random number thing, not quite sure what you are referring to. when i ran it on here it just ran for 20 times or whatever because of the needed input via soloLearn. you have it set up so that if its not their number they press y? should be think of a number display possible number ask input(higher/lower/correct) IF it is not correct recalculate repeat until correct IF correct ask if they want to play agian reset all values. hope this helps


You need to reset your variables declared at the top