0

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; }

20th Feb 2017, 8:51 PM
Safal Basnet
Safal Basnet - avatar
4 Réponses
+ 5
On the first view I think you have to reset x and y.
20th Feb 2017, 9:35 PM
Tashi N
Tashi N - avatar
+ 1
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...
22nd Feb 2017, 3:12 AM
Zeke Williams
Zeke Williams - avatar
0
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
20th Feb 2017, 9:44 PM
Michael Szczepanski
Michael Szczepanski - avatar
- 1
You need to reset your variables declared at the top
20th Feb 2017, 10:29 PM
aklex
aklex - avatar