保持运行程序,直到用户输入退出

Keep running program until user input exit

本文关键字:用户 输入 退出 运行 程序      更新时间:2023-10-16

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;

程序用堆栈将中缀符号转换为后缀符号。我的问题是,有没有一种方法可以一直循环,直到用户不想循环为止。类似于

   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"时,程序将停止接收用户输入