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

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;

程序将中缀转换为带有堆栈的后缀表示法。我的问题是,有没有办法一直循环直到用户不想循环。 类似于此

   int n;
  cin>>n;

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

你可以这样做:

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

当用户按下 "ctrl+Z"

时,程序将停止接受用户输入

以下是您可以执行此操作的两种方法.. 首先是建议使用 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