【问题标题】:How to get a word entered by the user to exit a loop?如何让用户输入的单词退出循环?
【发布时间】:2015-07-25 09:00:45
【问题描述】:

我正在开发一个程序,提示用户输入参加比赛的参赛者的姓名和评委给出的分数。所有这些都在一个带有计数器的 for 循环中,以跟踪参赛者的数量。由于参赛者人数未知,用户应该输入“完成”退出循环。

我发现退出循环的所有方法都是使用布尔值,它不适用于“字符串”变量。 我想知道是否有办法通过输入“完成”这个词来退出循环

#include <iostream>
#include <string>   //for name entry of contestants
#include <iomanip>  //rounding and point perceision
#include <fstream>  //write names and averages to a file for storage

using namespace std;

double calcAvgScore(int, int, int, int, int);   //calculates the average after the highest and lowest scores are found 
double findHighest(int, int, int, int, int);    //finds the highest of the 5 scores 
double findLowest(int, int, int, int, int);     //finds the lowest of the 5 scores

int main()
{
    string cName;   //name of the contestant
    int jScore1, jScore2, jScore3, jScore4, jScore5;    //scores of each judge
    double AvgScore;    //average of the scores
    ofstream outFile;

    outFile.open("NamesAndAverage.txt");

    for (int count = 1;; count++)
    {
        if (count = 1)
            cout << "Please enter the name of Contestant #" << count << ". If there are no Contestants enter ""Done""";
        else
            cout << "Please enter the name of Contestant #" << count << ". If there are no more Contestants please enter ""Done""";
        cin >> cName;
    }
}

【问题讨论】:

  • 您在寻找break;吗?

标签: c++ loops for-loop exit break


【解决方案1】:

您可以将条件直接添加到循环中,

for (int count = 1; cName != "Done"; count++)

或在循环内使用ifbreak

if( cName == "Done" )
     break;

您也可以在循环内容中使用三元:

cout << "Please enter the name of Contestant #" << count << ". If there are no" << (count > 1) ? "more" : "" << "Contestants enter ""Done"";
cin >> cName;

【讨论】:

  • “也可以简化” - 哎呀!如果要考虑任何三元运算符“简化”,则应该删除所有冗余:std::cout &lt;&lt; "Please enter the name of Contestant #" &lt;&lt; count &lt;&lt; ". If there are no " &lt;&lt; (count &gt; 1 ? "more" : "") &lt;&lt; "contestants enter \"Done\"";
  • 像这样使用三元运算符很难简化。只需使用if。指导原则是,三元运算符只有在使用其结果时才应使用,而不是丢弃它。
  • @ChristianHackl 以前从未更新过该指南,谢谢!
  • @TonyD 是的,您的简化方法更好。
  • @raymelfrancisco:不客气。事实上,TonyD 的例子是一个很好的例子,因为它使用了运算符的结果。这更容易阅读和理解。
【解决方案2】:

你可以break退出循环:

for (int count = 1;; count++)
{
    // prompt for input, snipped
    cin >> cName;
    if (cName == "Done") 
    {
        break;
    }
}

【讨论】:

    【解决方案3】:

    您可以使用break;

    if (cName == "Done") {
        break;
    }
    

    此外,您的程序中还有另一个错字。与

    if (count = 1)
    

    您实际上每次都将 count 重置为 1。这应该是:

    if (count == 1)
    

    【讨论】:

      【解决方案4】:

      除了处理"Done",您还应该检查您的输入和输出操作是否成功,两者都可以使用if/else 语句完成:

      if (std::ofstream outFile("NamesAndAverage.txt"))
          for (int count = 1;; count++)
          {
              cout << "Please enter the name of Contestant #" << count << "."
                      "If there are no ";
              if (count > 1) std::cout << "more ";
              std::cout << "contestants enter \"Done\"\n";
              if (cin >> cName && cName != "Done")
                  // use cName...
              else
                  break;
           }
      else
           std::cerr << "unable to open output file\n";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-25
        • 2014-04-12
        • 1970-01-01
        • 2021-03-16
        相关资源
        最近更新 更多