1
int main()
{
    string selection;
    // print out selection menu
    selection = userOption();
    cout << selection << endl;

    //now perform web parser
    webparser(selection);    

    //now perform searchstring
    searchString(selection);
return 0;
}

以上是我的部分代码字符串 userOption() 是一个像这样打印出菜单的函数

货币

  1. 美元/新加坡元
  2. 欧元/美元
  3. 输入您自己的货币对
  4. 退出程序

我如何让 main 不退出,直到从 userOption 选择 4

4

3 回答 3

2

一个简单的do-while:

int main()
{
  string exitstr("4");
  string selection;
  do {
    // print out selection menu
    selection = userOption();
    cout << selection << endl;
    if (selection == exitstr)
      break;
    //now perform web parser
    webparser(selection);    

    //now perform searchstring
    searchString(selection);
  } while (1);
return 0;
}
于 2012-07-25T06:18:12.997 回答
1
int main()
{
    string selection;
    while( (selection = userOption()) != "4")
    {
        cout << selection << endl;
        //now perform web parser
        webparser(selection);    
        //now perform searchstring
        searchString(selection);
    }
    return 0;
}
于 2012-07-25T06:17:22.490 回答
1
int main()
{
    for (;;)
    {
        string selection;
        // print out selection menu
        selection = userOption();
        cout << selection << endl;

        if (selection == "4") break;

        //now perform web parser
        webparser(selection);    

        //now perform searchstring
        searchString(selection);
    }
    return 0;
}

类似于 perreals 的答案。有很多方法可以编写“无限”循环,我认为在循环头(作为 this 或 by while(true))中表达它会更好 - 当你开始阅读循环时,你会立即知道结束条件在里面的某个地方。

于 2012-07-25T06:19:33.390 回答