[英]Keep running program until user input exit
My main function 我的主要功能
int main(){
Postfix post;
char size=100;
char infix[size];
string get_input=" ";
cout<<"Input Infix";
cin>>infix;
int size=strlen(infix);
char postfix[size];
post.infix_to_postfix(infix, postfix, size);
cout<<"\nInfix Expression is:"<<" "<<infix;
cout<<"\nPostfix Expression is:"<<" "<<postfix;
cout<<endl;
The program converts infix to postfix notation with stacks. 该程序将带栈的infix转换为postfix表示法。 My question is, is there a way to keep looping until the user does not want to.
我的问题是,有没有一种方法可以使循环一直持续到用户不想这样做为止。 similar to this
与此类似
int n;
cin>>n;
while (n!=0){
// keep doing whatever
}
Below are the two ways you can do this.. first of all is to would suggest to use std::string
it would make your life easy. 下面是您可以执行此操作的两种方法。.首先,建议使用
std::string
它将使您的生活变得轻松。 Try to include it in your coding habit.. 尝试将其包含在您的编码习惯中。
while (std::getline(std::cin, line) && !line.empty())
{
//write your logic here
}
To break the loop user has to press enter
要中断循环,用户必须按
enter
Second way 第二种方式
std::string str;
while(std::cin>>str)
{
//write your logic here
}
To break the loop press ctrl+D
要中断循环,请按
ctrl+D
You can do this: 你可以这样做:
while(cin >> infix){
// your code here....
}
the program will stop taking user input when user presses "ctrl+Z" 当用户按下“ ctrl + Z”时,程序将停止接受用户输入
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.