2016-04-14 140 views
0

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

循环结束后,它会计算并显示。列表中的最高年级,最低年级和平均成绩

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

你能帮我找出如何停止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; 

} 
+0

看代码'cin >>等级[count];'。你打算如何将“完成”一词放在一个用于等级的数组中?也许像'-1'这样的数字可以用来表示“完成”,然后检查输入的数字是否为“-1”? – PaulMcKenzie

回答

1

这是您的代码的两个问题。

第一:

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

上面的代码将attepmpt读字“做”成整型变量,产生0不是你想要做什么!

:以上

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

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

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

有几件事情在这里解压。

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

下一步你正在用grade[count] != done检查是否如果“等级”在ID“count”的整数不等于bool false。在这种情况下,这将始终返回true。

为了您的使用情况,你要被检查的是如果输入等于字符 *“做”

这不能在while循环的谓词发生,因为你的等级阵列只存储int

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

这里是修改后的循环:

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旅馆(是,它忽略输入不是一个号码,这将屏蔽某人输入“马铃薯”等无效输入。这就是我立即将输入转换为字符串的原因。

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>