这个程序运行但不正确的数字不对,我从文件中读取数字,然后当我在程序中使用它们时他们是不对的.:我正在尝试做的解释可以有人告诉我,如果看起来不对.
这就是我要做的:
编写一个程序,确定100名学生的成绩分散
您要将考试成绩读入三个阵列,每个考试一个阵列.然后你必须计算得分A(90或以上),B(80或以上),C(70或以上),D(60或以上)和F(少于60)的得分数.为每个考试执行此操作并将分发写入屏幕.
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;
int read_file_in_array(double exam[100][3]);
double calculate_total(double exam1[], double exam2[], double exam3[]); // function that calcualates grades to see how many 90,80,70,60
//void display_totals();
double exam[100][3];
int main()
{
double go,go2,go3;
double exam[100][3],exam1[100],exam2[100],exam3[100];
go=read_file_in_array(exam);
go2=calculate_total(exam1,exam2,exam3);
//go3=display_totals();
cout << go,go2,go3;
return 0;
}
/*
int display_totals()
{
int grade_total;
grade_total=calculate_total(exam1,exam2,exam3);
return 0;
} */
double calculate_total(double exam1[],double exam2[],double exam3[])
{
int calc_tot,above90=0, above80=0, above70=0, above60=0,i,j, fail=0;
double exam[100][3];
calc_tot=read_file_in_array(exam);
for(i=0;i<100;i++)
{
for (j=0; j<3; j++)
{
exam1[i]=exam[100][0];
exam2[i]=exam[100][1];
exam3[i]=exam[100][2];
if(exam[i][j] <=90 && exam[i][j] >=100)
{
above90++;
{
if(exam[i][j] <=80 && exam[i][j] >=89)
{
above80++;
{
if(exam[i][j] <=70 && exam[i][j] >=79)
{
above70++;
{
if(exam[i][j] <=60 && exam[i][j] >=69)
{
above60++;
{
if(exam[i][j] >=59)
{
fail++;
}
}
}
}
}
}
}
}
}
}
}
return 0;
}
int read_file_in_array(double exam[100][3])
{
ifstream infile;
int exam1[100];
int exam2[100];
int exam3[100];
infile.open("grades.txt");// file containing numbers in 3 columns
if(infile.fail()) // checks to see if file opended
{
cout << "error" << endl;
}
int num, i=0,j=0;
while(!infile.eof()) // reads file to end of line
{
for(i=0;i<100;i++) // array numbers less than 100
{
for(j=0;j<3;j++) // while reading get 1st array or element
infile >> exam[i][j];
infile >> exam[i][j];
infile >> exam[i][j];
cout << exam[i][j] << endl;
{
if (! (infile >> exam[i][j]) )
cout << exam[i][j] << endl;
}
exam[i][j]=exam1[i];
exam[i][j]=exam2[i];
exam[i][j]=exam3[i];
}
infile.close();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
读取代码有点困难,但是当你检查分数时,你应该使用chained else if语句而不是嵌套ifs.
有点像:
if(exam[i][j] >=90 && exam[i][j] <=100)
{
above90++;
}
else if(exam[i][j] >=80 && exam[i][j] <=89)
{
above80++;
}
else if(...)
{...}
Run Code Online (Sandbox Code Playgroud)
此外,您可能希望选择一种语法并在整个代码中与其保持一致,在尝试解决任何问题时,或者甚至弄清楚您在做什么,这将产生巨大的差异!不要紧,什么你去的方式尽可能多的为与它保持一致.
如果条件(parens中的表达式)为真,并且如果它为假,则语句允许您执行某些操作.
if( /* statement that evaluates to true */ )
{
/* insert the commands you want to run when it's true here */
}
else
{
/* insert the commands you want to run when it's false here */
}
Run Code Online (Sandbox Code Playgroud)
您可以使用"其他"部分链如果语句组合在一起,这样,如果你有两个以上的可能性是非常有用的.比如这样:
int number = 2; // Can be a value between 0 and 2 (0, 1, or 2)
if(number == 0)
{
cout << "The number is zero";
}
else if(number == 1)
{
cout << "The number is one";
}
else if(number == 2)
{
cout << "The number is two";
}
else
{
cout << "The value wasn't an allowed value!";
}
Run Code Online (Sandbox Code Playgroud)
如果number介于0和2之间,它将打印出它的数字,如果它不在该范围内,它将打印出一条消息,表明它不是.