【问题标题】:How to use a while loop with an array?如何使用带有数组的while循环?
【发布时间】:2016-08-06 09:52:43
【问题描述】:

我创建了一个允许用户输入 10 个等级的程序。我使用了一个 while 循环将成绩存储在数组中,但如果用户只有 5 个成绩要输入,他可以键入 done 退出程序。

循环结束后,会计算并显示。数组内的最高分、最低分和平均分

不幸的是,当用户输入完成后,程序将显示其余未输入的成绩行。

您能帮我找出如何阻止 while 循环显示该循环的其余未输入成绩吗?

#include <iostream>
using namespace std;

int main()
{
const int SIZE = 10;
int grade[SIZE];
int count = 0;
int lowestGrade;
int highestGrade;
bool done = false;


cout << "This program is limited to entering up to 10 grades." << endl;

while ( grade[count] != done && count < SIZE)
{

    cout << "Enter a grade #" << count + 1 << " or done to quit: ";
    cin >> grade[count];
    count++;

}

//LOWEST GRADE
lowestGrade = grade[0];
for (count = 0; count < SIZE; count++)
    if (grade[count] < lowestGrade)
    {
        lowestGrade = grade[count];
    }

//HIGHEST GRADE
highestGrade = grade[0];
for (count = 0; count < SIZE; count++)
{
    if (grade[count] > highestGrade)
    {
        highestGrade = grade[count];
    }
}


//AVERAGE GRADE
double total = 0;
double average;
for (int count = 0; count < SIZE; count++)
    total += grade[count];
average = (total / SIZE);

cout << endl;

cout << "Your highest grade is: " << highestGrade << endl;
cout << "Your lowest grade is: " << lowestGrade << endl;
cout << "Your average grade is: " << average << endl;



system("pause");
return 0;

}

【问题讨论】:

  • 看代码cin &gt;&gt; grade[count];。你如何将“完成”这个词放在一个用于成绩的数组中?也许-1这样的数字可以用来表示“完成”,然后检查输入的数字是否为-1?

标签: c++ arrays while-loop


【解决方案1】:

您的代码有两个问题。

第一:

....
cout << "Enter a grade #" << count + 1 << " or done to quit: ";
cin >> grade[count];
count++;
....

上面的代码将尝试将单词“done”读入整数变量,产生 0。不是你想做的!

第二:

...
for (count = 0; count < SIZE; count++)
... 

上面的代码将尝试遍历所有可能的元素 (SIZE)。但是,您可能比这更少!您需要使用在上一个循环中计算的count 作为您的边界(当然,循环中的控制变量使用不同的名称)。

【讨论】:

    【解决方案2】:

    这里有几件事要解压。

    基本上,您要检索的输入是 char * 并且 >> 运算符将其转换为 int 以适合您的成绩数组。

    接下来你用grade[count] != done检查的是id为“count”的“grade”中的整数是否不等于bool false。在这种情况下,这将始终返回 true。

    对于您的用例,您要检查的是您的输入是否等于 char * "done"

    这不会发生在 while 循环的谓词中,因为您的成绩数组仅存储 int。

    因此,我认为最简单的解决方案是检查输入是否等于“完成”。 如果您想将 done 布尔值设置为 true 否则我们可以尝试将其转换为 int 并将其存储在 Grades 数组中。

    这是修改后的循环:

    while (!done && count < SIZE)
    {
        cout << "Enter a grade #" << count + 1 << " or done to quit: ";
    
        string input = "";
        cin >> input;
    
        if (input == "done")
        {
            done = true;
        }
        else
        {
            grade[count] = stoi(input);
        }
    
        count++;
    }
    

    以下内容有点超出了问题的范围,但使用 stoi() 的另一个好处是它会忽略不是数字的输入,这将防止有人输入无效输入,例如“土豆”。这就是为什么我立即将输入转换为字符串。

    【讨论】:

      【解决方案3】:

      使用另一个变量来存储用户输入的成绩数量。您也不能在整数数组中存储字符串:

      std::string input = "";
      while(count < SIZE)
      {   
          cout << "Enter a grade #" << count + 1 << " or done to quit: ";
          getline(cin, input);
          if(input == "done")
              break;        
          try
          {
              grade[count] = std::stoi(input);
              count++;
          }
          catch(std::invalid_argument)
          {
              cout << "not a valid number\n";
          }
      }
      int actualsize = count;
      

      然后使用这个变量中止你的 for 循环:

      for (int i = 0; i < actualsize; i++)
      

      【讨论】:

        【解决方案4】:

        有两种简单的方法可以解决您的问题:

        • 您可以读取字符串而不是整数,如果读取的字符串“完成”,则中断循环,否则,将读取的字符串转换为整数,如下所示:

        ```

        // rest of the code
        
        int total_count = 0;
        
        while (count < SIZE) {
          cout << "Enter a grade #" << count + 1 << " or done to quit: ";
          string temp;
          cin >> temp;
          if(temp == "done") {
            break;
          } else {
            grade[count] = stoi(temp);
            count++;
            total_count = count;
          }
        }
        
        // rest of the code
        

        ```

        • 如果您不想使用字符串,那么假设成绩为非负数,您可以在用户键入负数时停止读取输入,例如“-1”。因此,您需要执行以下操作:

        ```

        // rest of the code
        
        int total_count = 0;
        
        while (count < SIZE) {
          cout << "Enter a grade #" << count + 1 << " or -1 to quit: ";
          int temp;
          cin >> temp;
          if(temp == -1) {
            break;
          } else {
            grade[count] = temp;
            count++;
            total_count = count;
          }
        }
        
        // rest of the code
        

        ```

        另外,不要忘记在其余循环中将 SIZE 替换为 total_count,即计算“LOWEST GRADE”、“HIGHEST GRADE”和“AVERAGE GRADE”的循环'。

        注意:如果您使用第一个选项,您还必须在顶部输入 #include &lt;string&gt;。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-19
          • 1970-01-01
          • 2015-06-25
          • 2020-08-29
          • 2021-12-28
          相关资源
          最近更新 更多