Stack and C ++的新功能。 我想终止一个循环,回转用字符反馈给它的数字。说,Q退出。以下程序功能齐全,没有语法错误。如何在不编辑输入参数的情况下终止此循环?
#include <iostream>
#include <string>
using namespace std;
int main()
{
bool run = true;
while(run)
{
cout<<"Enter your two favorite numbers."<<endl;
int num1;
int num2;
cin>>num1>>num2;
cout<<"You entered "<<num1<<" and "<<num2<<"."<<endl;
}
return 0;
}
答案 0 :(得分:0)
你想要break
statement。但是,您还需要为第一个输入读取字符串,而不是整数。
while(run)
{
cout<<"Enter your two favorite numbers, or 'Q' to exit."<<endl;
string input;
int num1;
int num2;
cin>>input1;
if (input == "Q")
{
break;
}
else
{
num1 = stoi(input);
}
cin>>num2;
cout<<"You entered "<<num1<<" and "<<num2<<"."<<endl;
}