繁体   English   中英

继续运行程序,直到用户输入退出

[英]Keep running program until user input exit

我的主要功能

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;

该程序将带栈的infix转换为postfix表示法。 我的问题是,有没有一种方法可以使循环一直持续到用户不想这样做为止。 与此类似

   int n;
  cin>>n;

  while (n!=0){
  // keep doing whatever
 }

下面是您可以执行此操作的两种方法。.首先,建议使用std::string它将使您的生活变得轻松。 尝试将其包含在您的编码习惯中。

while (std::getline(std::cin, line) && !line.empty())
{
    //write your logic here
}

要中断循环,用户必须按enter

第二种方式

std::string str;
while(std::cin>>str)
{
//write your logic here
}

要中断循环,请按ctrl+D

你可以这样做:

while(cin >> infix){
    // your code here....
}

当用户按下“ ctrl + Z”时,程序将停止接受用户输入

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM