int n, k; cin>>n>>k; //read the number of stories and eggs string response; while(response != "exit") { cout << "query " << x << "\n"; //can also use endl for newline cout.flush(); cin >> response; if(response == "broke") k--; if(!sureAboutAnswer) { //perhaps you need more information cout << "query " << x << "\n"; cout.flush(); cin >> response; if(response == "broke") k--; } else { //in case you've found the answer cout << "answer " << x << "\n"; cout.flush(); cin >> response; //the response is going to be "exit" } }