你好我是初学者,我在 c ++ 中制作了这个程序,作为我正在研究的那本书的一部分(数组章节)。 它意味着逐一取学生人数,成绩平均,并提供教室平均值等信息,< strong>在该课堂上无法通过该课程的学生人数,最佳平均成绩。 (我知道美国的成绩看起来像A,B,C等,但在我的国家,成绩从1到10(1是最低,10是最高,5或超过5是要求通过测试,课程等) 我更进了一步,在每个命令后实现了 reloop 菜单的选项,还添加了一个选项退出程序,一个选项重启该计划。 到目前为止,我测试了不同的值,一切似乎工作正常,除非我输入一个字母或程序中除了数字之外的任何其他内容,它永远循环。如何让它显示在交换机默认情况下设置的错误消息,就像我输入6,7等时一样?我正在使用 MS Visual C ++ Express 2013 。 谢谢你的时间!
以下是代码:
#include<iostream>
using namespace std;
int main()
{
float medii[35], m, max; short int n, i; int sel, nr; bool reloop, reset;
do
{
reloop = true;
reset = false;
m = 0; max = 0; n = 0; i = 0; sel = 0; nr = 0;
cout << "===============================================================================" << endl;
cout << "Numarul elevilor = "; cin >> n; //input the number of students in the class
cout << "===============================================================================" << endl;
for (i = 0; i < n; i++)
{
cout << "Introduceti media cu numarul " << i + 1 << ": "; cin >> medii[i]; //Insert the average one by one
}
cout << "===============================================================================" << endl;
cout << "Mediile introduse sunt: ";
for (i = 0; i < n; i++) cout << medii[i] << " "; //Display the averages with a space between them
while (reloop)
{
cout << "\n===============================================================================" << endl;
cout << "Selectati optiunea dorita:" << endl; //Select the desired task
cout << "1 - Numarul elevilor nepromovati" << endl; //The number of students who didn't graduate (counts the averages under 5
cout << "2 - Media clasei" << endl; //The average of the classroom
cout << "3 - Cea mai mare medie a clasei" << endl; //The highest average in the class
cout << "4 - Inchideti programul" << endl; //Close the program
cout << "5 - RESET" << endl; //RESET
cout << "===============================================================================" << endl;
cout << "Selector: "; cin >> sel;
cout << "===============================================================================" << endl;
switch (sel)
{
case 1:
{
for (i = 0; i < n; i++)
if (medii[i] < 5)
nr = nr + 1;
cout << nr << " elevi nepromovati.";
nr = 0;
break;
}
case 2:
{
m = 0;
for (i = 0; i < n; i++)
m = m + medii[i];
m = m / n;
cout << "Media clasei este: " << m;
m = 0;
break;
}
case 3:
{
max = medii[0];
for (i = 1; i < n; i++)
if (medii[i]>max)
max = medii[i];
cout << "Media maxima: " << max;
break;
}
case 4:
{
reloop = false;
break;
}
case 5:
{
reloop = false;
reset = true;
cout << "RESET" << endl;
break;
}
default:
cout << "Ati introdus o optiune invalida."; //"You chose an invalid option" (The ERROR message, apparently this works only if a number is received by the program. If a letter is inserted, the program loops forever. What am I doing wrong??)
}
}
} while (reset);
}
答案 0 :(得分:0)
问题是cin
期望0到9范围内的数字字符(因为sel
是一个int)并且它找到一个字母字符。
因此,它不会将sel
更新为新值(它也不会抛出异常)。
您可以将sel
设为char
并检查其值:
int main()
{
float medii[35], m, max; short int n, i; char sel, nr; bool reloop, reset;
do
{
reloop = true;
reset = false;
m = 0; max = 0; n = 0; i = 0; sel = 0; nr = 0;
cout << "===============================================================================" << endl;
cout << "Numarul elevilor = "; cin >> n; //input the number of students in the class
cout << "===============================================================================" << endl;
for (i = 0; i < n; i++)
{
cout << "Introduceti media cu numarul " << i + 1 << ": "; cin >> medii[i]; //Insert the average one by one
}
cout << "===============================================================================" << endl;
cout << "Mediile introduse sunt: ";
for (i = 0; i < n; i++) cout << medii[i] << " "; //Display the averages with a space between them
while (reloop)
{
cout << "\n===============================================================================" << endl;
cout << "Selectati optiunea dorita:" << endl; //Select the desired task
cout << "1 - Numarul elevilor nepromovati" << endl; //The number of students who didn't graduate (counts the averages under 5
cout << "2 - Media clasei" << endl; //The average of the classroom
cout << "3 - Cea mai mare medie a clasei" << endl; //The highest average in the class
cout << "4 - Inchideti programul" << endl; //Close the program
cout << "5 - RESET" << endl; //RESET
cout << "===============================================================================" << endl;
cout << "Selector: "; cin >> sel;
cout << "===============================================================================" << endl;
switch (sel)
{
case '1':
{
for (i = 0; i < n; i++)
if (medii[i] < 5)
nr = nr + 1;
cout << nr << " elevi nepromovati.";
nr = 0;
break;
}
case '2':
{
m = 0;
for (i = 0; i < n; i++)
m = m + medii[i];
m = m / n;
cout << "Media clasei este: " << m;
m = 0;
break;
}
default:
cout << "Ati introdus o optiune invalida."; //"You chose an invalid option" (The ERROR message, apparently this works only if a number is received by the program. If a letter is inserted, the program loops forever. What am I doing wrong??)
}
}
} while (reset);
}
请注意:
char sel
..和
case '1':