c++ - 继续运行程序直到用户输入退出

标签 c++ arrays stack

我的主要功能

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

关于c++ - 继续运行程序直到用户输入退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33249215/

相关文章:

javascript - 如何展平包含数组的对象数组?

java - 使用 PHP 使用 JSONP 返回对象而不是结果

arrays - 如何为每个 CollectionView 部分拥有不同的数据集?

Java 堆栈排序符号平衡

c++ - 堆栈.h :13:3: error: ‘Cell’ does not name a type

c++ - QVector 与 Qthreads

c++ - 将包含 std::thread 的类添加到 vector 中

c++ - 直方图均衡不适用于彩色图像 - OpenCV

c++ - 如何使用 C++ 正确检查注册表项是否存在?

delphi - 在 Delphi TStack<T> 中循环并搜索值