C++ 函数,考试成绩为 0-100
c++ function with test scores 0-100
my if 语句不是 100% 有效,我不确定我做错了什么。任何帮助将不胜感激,谢谢!这是我的函数
void DisplayStudentsInRange(const StudentType student[], int numStudents, int lownum, int highnum)
{
cout <<"Enter two values from 0-100 for the range of test scores you want to view: ";
cin >> lownum >> highnum;
if ( lownum < 0 || highnum < 0 || lownum > highnum || lownum > 100 || highnum > 100)
{
cout <<"Please re-enter 2 values of test scores that you want to view from within the range 0-100";
cin >> lownum >> highnum;
}
cout << endl << "List of students with scores in the range " << lownum << " to " << highnum << endl << endl;
FormatNameScoreGrade(cout);
for(int i = 0; i < numStudents; i++)
{
if(student[i].testScore >= lownum && student[i].testScore <= highnum)
{
cout << setw(20) << student[i].studentName << setw(10) << student[i].testScore << setw(10) << student[i].grade << endl;
}
}
cout << endl;
}
不确定你在追求什么,但关于第一个if
语句,你可能想将输入和验证放入一个循环中:
do {
values = get_input();
} while (!values_are_valid());
我会在你的 if 语句中使用括号
if ( (lownum < 0) || (highnum < 0) || (lownum > highnum) || (lownum > 100) || (highnum > 100))
过去我没有这样做时,我遇到了问题。
请具体说明什么如果不是合适的也只是一个注释如果第二次输入的数据不正确,则第一个 if 将只工作一次您不会要求用户重新输入它,如果您想获得正确的输入你应该用一段时间替换你的第一个。
但总的来说,使用会更好
Do {
cout <<"Enter two values from 0-100 for the range of test scores you want to view: ";
cin >> lownum >> highnum;
}while( (lownum < 0) || (highnum < 0) || (lownum > highnum) || (lownum > 100) || (highnum > 100)); //statement
do 将按原样首次运行,然后如果输入不正确,while 语句将在输入正确后触发 do 部分。您可以编辑 couts 以类似于"输入/重新输入"等。
相关文章:
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 有哪些有效的方法可以消除一组 100 万个字符串>重复数据?
- 介于 1 和 100 之间的质数列表
- 检查nullptr是否100%保护内存布局不受segfault影响
- C ++ pcl_ros:如何使用最新的 100 条消息生成点云
- 我的期末考试遇到了麻烦.代码不断循环回到我的订购菜单,不会显示我的总价
- 将一副牌循环100次(一副牌)
- 为什么 someNumber = rand() & 100 + 1;不产生错误?
- 如何在没有同步的情况下使用多个线程(2、4,8、16 个线程)在循环(10,100、1000 个周期)中打印字符串?
- OpenMP 比串行代码慢 100 倍
- 如何使此函数适用于 100 个对象 (c++)?
- cudaMemcpyToSymbol只是挂起,永远不会返回.GPU 处理速度为 100%.代码在 K40 上工作正常,但
- 如何将系统音量级别作为标量从 0 到 100?
- 同时分叉 100 个进程,有时有些进程会变成僵尸
- 当我读取一个大小为 17 mb 的 100 万个 url 文件时,我的程序占用大小为 163 MB
- 我正在使用 std::bitset 并尝试创建两个大小为 100,000,000,000 的数组 std::bitset
- fprintf stdout 没有 100% 准确转储数据
- 需要生成从 100 到 500 的随机数
- Cpp 什么是字符 a 的声明 [10][100]
- C++ 函数,考试成绩为 0-100