Break a loop when the user just input an enter in visual c++ or code blocks -
i want know how make stop while loop when user input enter without asking continue or , here code:
int main() { bool flag = true; int userinput; while(flag){ cout<<"give integer: "; if(!(cin >> userinput) ){ flag = false; break;} foo(userinput); } }
thanks in advance.
use getline. break if string empty. convert string int.
for(std::string line;;) { std::cout << "give integer: "; std::getline(std::cin, line); if (line.empty()) break; int userinput = std::stoi(line); foo(userinput); }
std::stoi
throw exception on failure, handle want.
Comments
Post a Comment