循环不工作时执行
Do while Loops not working
我一直在循环使用一项作业,以在课堂上进一步练习和发展我的编程技能,但我在另一个DO WHILE循环中遇到了3个DO WHILE循环的问题。
我试图否认测试1、2和3的测试分数小于1且大于100。我遇到循环没有处理我为Test1/2/3输入的内容。它允许while范围之外的值通过。有没有人能建议或看到我可能做错了什么?提前感谢各位!
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
double computeavg (int a, int b, int c);
char lettergrade (double z);
int main ()
{
double test1, test2, test3, average; //test1/2/3, test scores, Average: average of test scores
double tottest1=0, tottest2=0, tottest3=0, avg1, avg2, avg3; //tottest# - sum of test grades collected for that number test, avg# average for first second or third test
int student=0, avgvar; //average variable, Student number
char grade, ans; // Holds a letter grade, holds a response to a question
do{
student=student+1;
cout<<"Hello Student #"<<student<<endl;
do{
cout<<"Please input test 1 Grade ";
cin>> test1;
}
while(test1>=1||test1<=100);
do{
cout<<"Please input test 2 Grade ";
cin>> test2;
}
while(test2>=1||test2<=100);
do{
cout<<"Please input test 3 Grade ";
cin>> test3;
}
while(test3>=1||test3<=100);
average=computeavg (test1, test2, test3);
cout<<setprecision(0)<<fixed;
cout<<"Your Average is: "<<average<<endl;
tottest1=tottest1+test1;
tottest2=tottest2+test2;
tottest3=tottest3+test3;
grade = lettergrade(average);
cout << "Your grade is " << grade << endl;
cout<<"Do you want to grade another student? (y/n)";
cin>>ans;
cout<<"n";
} while(ans=='y');
我认为您需要更改do while,如下所示:
do{
cout<<"Please input test 1 Grade ";
cin>> test1;
}
while(test1<1||test1>100);
您的条件是落后的。仔细看:while(test3>=1||test3<=100)
所有数字至少满足部分条件。例如,减号;4不大于或等于1,但小于或等于100,并且由于您的条件与||
运算符(即或运算符)连接,因此条件计算结果为true。
你需要改变条件。这是家庭作业,所以我不会确切地告诉你应该使用什么条件。
您可以使用调试器自行发现此问题。你也可以通过仔细地用手浏览你的程序来发现它。拿一支铅笔和一些纸,写下变量的当前值。跟踪你正在使用的指令,然后按照自己的方式完成程序。当一个变量发生变化时,划掉它的旧值,记下新值。最终,你将能够在脑海中做到这一点,但在你学习的同时,把它写下来会有所帮助。
相关文章:
- 从不同目录执行时"symbol lookup error:",否则从构建目录执行时按预期工作
- C++ 在条件未按预期工作时执行
- 工作线程在执行太快后永久休眠
- 无法使用 Visual Studio 启动 DirectX 11 可执行文件,生成工作正常
- 程序按执行方式工作,直到我向其添加析构函数为止
- 等待所有线程完成一项工作,然后执行另一项工作
- 该程序在执行时停止工作.我在代码块中制作了这个程序
- 为什么将可执行文件重命名为临时文件的此代码段不能按预期工作?
- CLION 调试器在执行一个步骤后停止工作
- 从本地工作,但在执行之间的随机端口上没有收到任何内容
- 为什么添加代码会破坏工作,即使它没有执行?
- 现代C++.从继续执行的工作线程返回数据结构
- 我的程序在执行期间停止工作
- C 可执行文件不从文件夹工作,而是在Mac上从控制台上工作
- Java在QT 5.7中为Android执行但不执行C (在先前的QT中工作)
- 为什么函数 sleep() 在 klee 执行 Objectfile 时不能工作?
- 创建不需要任何进一步安装即可在其他计算机上工作的可执行文件?
- 调试执行工作正常,但正常的一段故障
- 使用静态数据的构造函数在main()之前执行工作
- ROS节点无法通过启动文件执行工作