[英]Logic error somewhere in code, cannot figure out where
Hello I'm writing a small program that allows the user to enter three test scores for a number of students than to compute student average, give a letter grade than give class average for each test once the user has given no (n) as an answer. 您好,我正在编写一个小程序,该程序允许用户输入多个学生的三个测试分数,而不是计算学生的平均分数,给出的字母等级高于每个测试的班级平均分数,前提是用户未给出(n)回答。 For some reason I'm getting a very large number for total test 3 (tottest3) I've been looking at the code for almost an hour and a half now and I can't find the error.
出于某种原因,我在总测试3(tottest3)中得到了大量的数据,现在我已经看了将近一个半小时的代码,但找不到错误。
the formulas for total test 1 to three are all identical, would the problem lie else where? 总体测试1到3的公式都相同,问题出在哪里?
here is what I've tried doing so far: 到目前为止,这是我尝试过的操作:
-changing the average variable -更改平均变量
-changing the class average to merely the total average -将班级平均数更改为仅总平均数
-adding test 1 or test 2 numbers to total test 3 instead of test 3 . 将测试1或测试2的数字加到总测试3中,而不是测试3中。
here is the output on command prompt. 这是命令提示符下的输出。
Assignment #13
作业13
Hello Student #1
你好学生#1
Please input test 1 Grade 1
请输入测试1等级1
Please input test 2 Grade 1
请输入测试2等级1
Please input test 3 Grade 1
请输入测试3等级1
Your Average is: 1
您的平均值是:1
Your grade is F
你的成绩是F
Do you want to grade another student?
您想给另一个学生评分吗? (y/n) n
(y / n)n
here are the averages for tests 1-3
这是测试1-3的平均值
average for test 1 is: 1
测试1的平均值为:1
average for test 2 is: 1
测试2的平均值为:1
average for test 3 is: 335796348684545909624128047159363492209569460693822234007169656776676343892387006827439949582508669307827616982407178504474449980239883634115896048874405460441709614779949982258368367180374307657350038129751972987645105499446376379659139781953069252608
测试3的平均值是:3357963486845459096241280471593634922095694606938222340071696567766763438923870068274399495825086693078276169824071785044744499802398836341158960488744054604417096147799499822583683671803743073500350028129751972197298764510549944637637965913995395372
Process exited with return value 0
进程退出,返回值为0
Press any key to continue .
按任意键继续 。 .
。 .
。
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main ()
{
cout<<"Jon Grezda CS 12 Wednesday 6-8pm"<<endl;
cout<<"Assignment #13"<<endl;
double test1, test2, test3, average; //test1/2/3, test scores, Average: average of test scores
double tottest1, tottest2, tottest3, avg1, avg2, avg3; //tottest1/2/3, 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;
cout<<"Please input test 1 Grade ";
cin>> test1;
cout<<"Please input test 2 Grade ";
cin>> test2;
cout<<"Please input test 3 Grade ";
cin>> test3;
average=(test1+test2+test3)/3.0;
cout<<setprecision(0)<<fixed;
cout<<"Your Average is: "<<average<<endl;
tottest1=tottest1+test1;
tottest2=tottest2+test2;
tottest3=tottest3+test3;
// Determine the letter grade. What grade will be assigned?
if (average > 0 && average < 60)
grade = 'F';
else if (average >= 61 && average < 70)
grade = 'D';
else if (average >= 70 && average < 80)
grade = 'C';
else if (average >= 80 && average < 90)
grade = 'B';
else if(average >= 90 && average <101)
grade = 'A';
else
{
cout << "We do not give scores higher than 100 or lower than 0.\n"; // Is the score valid?
grade = '-';
}
cout << "Your grade is " << grade << endl;
cout<<"Do you want to grade another student? (y/n)";
cin>>ans;
cout<<"\n";
} while(ans=='y');
if (ans=='n')
{
avgvar=student;
avg1=tottest1/avgvar;
avg2=tottest2/avgvar;
avg3=tottest3/avgvar;
cout<<setprecision(0)<<fixed;
cout<<"here are the averages for tests 1-3\n";
cout<<"average for test 1 is: "<<avg1<<endl;
cout<<"average for test 2 is: "<<avg2<<endl;
cout<<"average for test 3 is: "<<avg3<<endl;
}
}
/*
*/
Your trouble happens in these lines; 您的麻烦就发生在这些方面。
tottest1=tottest1+test1;
tottest2=tottest2+test2;
tottest3=tottest3+test3;
You declared the variables tottest1, tottest2, and tottest3 but you did not give them a value. 您声明了变量tottest1,tottest2和tottest3,但没有给它们赋值。 Your program has set aside some memory for those variables but it has not placed a value into those spots of memory so they are just filled with garbage.
您的程序为这些变量留出了一些内存,但尚未在这些内存中放置值,因此它们只是被垃圾填满了。 When you try to assign values to the variables by adding test1/2/3 to them, you add something meaningful to whatever garbage was stored in those uninitialized slots of memory.
当您尝试通过向变量添加test1 / 2/3来为变量赋值时,您将对那些未初始化的内存插槽中存储的垃圾添加有意义的内容。 The result is something meaningless.
结果是毫无意义的。
Solution: When you declare a variable, make sure to initialize it with some value, eg, 0. 解决方案:声明变量时,请确保使用某个值(例如0)对其进行初始化。
It doesn't look like you're initializing your variables. 似乎您没有初始化变量。 Do that and you'll get more predictable results.
这样做,您将获得更多可预测的结果。 Probably set them to 0 initially.
最初可能将它们设置为0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.