如何使用带数组的while循环?

时间:2016-04-14 17:25:05

标签: c++ arrays while-loop

我创建了一个允许用户输入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;

}

4 个答案:

答案 0 :(得分:1)

以下是您的代码存在的两个问题。

<强>首先

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

上面的代码将尝试将“完成”字读入整数变量,产生0.不是你想要做的!

<强>第二

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

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

答案 1 :(得分:1)

这里要解开几件事。

基本上,您要检索的输入是 char *和&gt;&gt; operator将其转换为int以适合您的成绩数组。

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

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

这不能在while循环的谓词中发生,因为你的成绩数组只存储 int

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

以下是修订后的循环:

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()的另一个好处是它忽略了不是数字的输入,这将防止有人输入无效输入“土豆”。这就是我立即将输入转换为字符串的原因。

答案 2 :(得分:0)

使用另一个变量来存储用户输入的等级数量。您也无法在整数数组中存储字符串:

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++)

答案 3 :(得分:0)

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

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

```

// 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 ,即计算“最低等级”,“最高等级”和“平均等级”的循环”。

注意:如果您使用第一个选项,您也必须在顶部#include <string>

相关问题