简体   繁体   中英

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. 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. 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

Second way

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

To break the loop press ctrl+D

You can do this:

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

the program will stop taking user input when user presses "ctrl+Z"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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