私はちょうど1月にコードする方法を学び始めましたが、これでも非常に新しいものです!私のコードはおそらく/間違いなくごみのように見えますが、効率的にする方法は間違いありませんが、まだ学習していません!C++:ループに入った後の余分な新しい行?
なぜ私のディスプレイに新しい行があるのか分かりません!
Student name Test1 Test2 Test3 Test4 Average Grade
Ben Smith
90 88 77 55 76.55 A
あなたは試してみたい場合はここで、テキストファイルの内容を形成します:
出力は次のようになります。
90 88 77 55 Ben Smith
66 77 66 55 Stuart Bit
ここに私の完全なコードは、(出力文は終わりに向かっている)だ:
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string name[25], grade[25];
string dummy;
int lineNum=0;
int x,y;
int table[25][4]{};
double average[25]{},classAvg[4]{};
ifstream in;
in.open("classData.txt");
while(getline(in, dummy))
{
lineNum++;
}
in.close();
in.open("classData.txt");
while(!in.eof())
{
for(x=0; x<lineNum;x++)
{
for(y=0;y<4;y++)
{
in>> table[x][y];
}
in.ignore(1, '\n');
getline(in, name[x]);
}
}
in.close();
for(x=0; x<lineNum;x++)
{
for(y=0;y<4;y++)
{
average[x] += table[x][y];
}
average[x]= average[x]/4;
}
for(int x=0;x<4;x++)
{
for(int y=0;y<lineNum;y++)
{
classAvg[x]+=table[y][x];
}
classAvg[x]=classAvg[x]/lineNum;
}
for(int x=0;x<lineNum;x++)
{
if(average[x]>= 90)
grade[x]="A";
else if(average[x]>= 80)
grade[x]="B";
else if(average[x]>= 70)
grade[x]="C";
else if(average[x]>= 60)
grade[x]="D";
else
grade[x]="F";
}
cout<<setw(20)<<"Student Name"
<<setw(20)<<"Test 01"<<setw(20)<<"Test 02"
<<setw(20)<<"Test 03"<<setw(20)<<"Test 04"
<<setw(20)<<"Average"
<<setw(20)<<"Grade"<<endl; //Print headers.
for(int x=0;x<lineNum;x++)
{
cout<<setw(20)<<name[x]; //Print all students' names.
for(int y=0;y<4;y++)
{
cout<<setw(20)<<table[x][y]; //Print 4 tests per student.
}
cout<<setw(20)<<setprecision(2)<<fixed<<average[x]; //Print average of 4 tests.
cout<<setw(20)<<grade[x]<<endl; //Print the grade of the student.
}
}
デバッガを使用してください。 – Danh
コード「Test 01」は、出力「Test1」を生成しません。誰もあなたが正しくコピー/ペーストできなかったものを解読しようとします。 –
Danhの提案に従うには、[この記事は良いものです](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。 – BoBTFish