[英]test score average and number of students with A,B,C,D,F grade without pointer
I am trying to make a C++ program counting the number of A, B, C, D, F grades without using Pointers. 我试图制作一个C ++程序,不使用Pointer来计数A,B,C,D,F等级的数量。 I thought this would be easy but still have a problem.
我以为这很容易,但是仍然有问题。 My code counts the number of C, D, F grade correctly but whenever I input A(90-100) and B(80-89) scores, It displays weird numbers such as 907517809. Why is this working like this?
我的代码正确地计算了C,D,F等级的数量,但是每当我输入A(90-100)和B(80-89)分数时,它都会显示奇怪的数字,例如907517809。为什么这样工作? it calculates average score right.
它计算平均得分正确。 This might be a basic question, but I'm curious.. Sorry in advance.
这可能是一个基本问题,但我很好奇。
#include <iostream>
using namespace std;
int main(){
int i,testscore,N;
int sum=0;
int Acount,Bcount,Ccount,Dcount,Fcount=0;
std::cout<<"How many test scores? " <<endl;
cin>> N;
if(N<1){
std::cout<<"Invalid input. try again"<<endl;
}
else if(N>25)
{
std::cout<<"1-25 only."<<endl;
}
else if(N>0 && N<25){
std::cout<<"Total number of test is: "<< N << endl;
}
for(i = 0; i < N; i++)
{
cout << "Enter the score of students " << i + 1 << ": ";
cin >>testscore;
if(testscore >= 90 && testscore < 100){
Acount++;
}
else if(testscore >= 80 && testscore < 90){
Bcount++;
}
else if(testscore >= 70 && testscore < 80){
Ccount++;
}
else if(testscore >= 60 && testscore < 70){
Dcount++;
}
else if(testscore <60){
Fcount++;
}
sum+=testscore;
}
std::cout<<"The average test score is: "<<sum/N<<endl;
std::cout<<"The number of A grades: " <<Acount<<endl;
std::cout<<"The number of B grades: " <<Bcount<<endl;
std::cout<<"The number of C grades: " <<Ccount<<endl;
std::cout<<"The number of D grades: " <<Dcount<<endl;
std::cout<<"The number of F grades: " <<Fcount<<endl;
return 0;
}
Because you've only initialised Fcount to zero. 因为您只将Fcount初始化为零。 You need to assign all the others too.
您还需要分配所有其他人。
int Acount=0,Bcount=0,Ccount=0,Dcount=0,Fcount=0;
You probably already know that without this assignment the variables will have random numbers. 您可能已经知道,没有此分配,变量将具有随机数。
You should be getting a warning about using an uninitialized value. 您应该收到有关使用未初始化值的警告。 It's good practice to compile in the strictest mode you can.
最好以最严格的模式进行编译。 Doing so will help avoid these trivial but time consuming errors.
这样做将有助于避免这些琐碎但耗时的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.