未读取文本文件的第二行; C++; Visual Studio
Second Line of Text File Not Reading; C++; Visual Studio
我在读计算机科学大二 class 我们正在学习 C++。
作业是编写一个文本文件,并根据文本文件的数据计算总计和平均值。
这是我的文本文件的样子:
Angela Langston Maya Malcolm Total Average
Algebra 64.5 56.7 67.4 90.0
CS1 88.6 77.0 55.3 89.4
English 91.3 67.4 89.0 100.0
Science 100.0 89.4 80.2 91.4
Average
在我编译和调试之前,我做的很好。我的程序无法正确打印文本文件的第二行。它打印出:
-107374176.0 -107374176.0 -107374176.0 -107374176.0 -429496704.0 -107374176.0
而不是我存储在文本文件中的数据。
请帮忙。我将提供解决此问题所需的任何其他信息。另外,提醒一下,我是初学者 C++ 用户。
更新:
这是代码:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
void student_heading();
void report_heading();
float average_calc(float);
int main()
{
cout << fixed << showpoint << setprecision(1);
string name1, name2, name3, name4;
string title1, title2;
string class1, class2, class3, class4, title3;
string fecha;
float algebra1, cs1, english1, science1;
float algebra2, cs2, english2, science2;
float algebra3, cs3, english3, science3;
float algebra4, cs4, english4, science4;
float algebra_total;
algebra_total = 0;
float cs_total = 0;
cs_total = 0;
float english_total = 0;
english_total = 0;
float science_total;
science_total = 0;
float algebra_avg, cs_avg, english_avg, science_avg;
float angela_avg, langston_avg, maya_avg, malcolm_avg;
float angela_total, langston_total, maya_total, malcolm_total;
angela_total = algebra1 + cs1 + english1 + science1;
langston_total = algebra2 + cs2 + english2 + science2;
maya_total = algebra3 + cs3 + english3 + science3;
malcolm_total = algebra4 + cs4 + english4 + science4;
angela_avg = average_calc(angela_total);
langston_avg = average_calc(langston_total);
maya_avg = average_calc(maya_total);
malcolm_avg = average_calc(malcolm_total);
student_heading();
cout << "What is today's date? (Example: May 1, 1996 = 05/01/1996): " << endl;
cin >> fecha;
cout << "DATE:" << fecha << "*************************************************** Page 1" << endl;
report_heading();
ifstream inData;
inData.open("infile.txt");
inData >> name1 >> name2 >> name3 >> name4 >> title1 >> title2;
cout << " " << name1 << " " << name2 << " " << name3 << " " << name4 << " " << title1 << " " << title2 << endl;
inData >> class1 >> algebra1 >> algebra2 >> algebra3 >> algebra4 >> algebra_total >> algebra_avg;
algebra_total = algebra1 + algebra2 + algebra3 + algebra4;
algebra_avg = average_calc(algebra_total);
cout << class1 << " " << algebra1 << " " << algebra2 << " " << algebra3 << " " << algebra4 << " " << algebra_total << " " << algebra_avg << endl;
inData >> class2 >> cs1 >> cs2 >> cs3 >> cs4 >> cs_total >> cs_avg;
cs_total = cs1 + cs2 + cs3 + cs4;
cs_avg = average_calc(cs_total);
cout << class2 << " " << cs1 << " " << cs2 << " " << cs3 << " " << cs4 << " " << cs_total << " " << cs_avg << endl;
inData >> class3 >> english1 >> english2 >> english3 >> english4 >> english_total >> english_avg;
english_total = english1 + english2 + english3 + english4;
english_avg = average_calc(english_total);
cout << class3 << english1 << english2 << english3 << english4 << english_total << english_avg;
inData >> class4 >> science1 >> science2 >> science3 >> science4 >> science_total >> science_avg;
science_total = science1 + science2 + science3 + science4;
science_avg = average_calc(science_total);
cout << class4 << science1 << science2 << science3 << science4 << science_total << science_avg;
inData >> title3;
cout << title3 << angela_avg << langston_avg << maya_avg << malcolm_avg << endl;
inData.close();
return 0;
}
void report_heading()
{
cout << "********************SMALL COLLEGE GRADE REPORT*******************" << endl;
}
void student_heading()
{
cout << "*******************" << endl;
cout << "Student" << endl;
cout << "ID" << endl;
cout << "SYCS-135 Computer Science I" << endl;
cout << "Assignment 5" << endl;
cout << "September 24, 2015" << endl;
cout << "******************" << endl;
}
float average_calc(float total_value)
{
float average;
average = total_value / 4;
return average;
这一行
inData >> class1 >> algebra1 >> algebra2 >> algebra3 >> algebra4 >> algebra_total >> algebra_avg;
尝试将字符串 "CS1" 读入 algebra_total
,这会导致流失败。要么不要尝试读取这些数字,要么将您的文本文件修复为
Angela Langston Maya Malcolm Total Average
Algebra 64.5 56.7 67.4 90.0 0 0
CS1 88.6 77.0 55.3 89.4 0 0
English 91.3 67.4 89.0 100.0 0 0
Science 100.0 89.4 80.2 91.4 0 0
Average
我在读计算机科学大二 class 我们正在学习 C++。
作业是编写一个文本文件,并根据文本文件的数据计算总计和平均值。
这是我的文本文件的样子:
Angela Langston Maya Malcolm Total Average
Algebra 64.5 56.7 67.4 90.0
CS1 88.6 77.0 55.3 89.4
English 91.3 67.4 89.0 100.0
Science 100.0 89.4 80.2 91.4
Average
在我编译和调试之前,我做的很好。我的程序无法正确打印文本文件的第二行。它打印出:
-107374176.0 -107374176.0 -107374176.0 -107374176.0 -429496704.0 -107374176.0
而不是我存储在文本文件中的数据。
请帮忙。我将提供解决此问题所需的任何其他信息。另外,提醒一下,我是初学者 C++ 用户。
更新: 这是代码:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
void student_heading();
void report_heading();
float average_calc(float);
int main()
{
cout << fixed << showpoint << setprecision(1);
string name1, name2, name3, name4;
string title1, title2;
string class1, class2, class3, class4, title3;
string fecha;
float algebra1, cs1, english1, science1;
float algebra2, cs2, english2, science2;
float algebra3, cs3, english3, science3;
float algebra4, cs4, english4, science4;
float algebra_total;
algebra_total = 0;
float cs_total = 0;
cs_total = 0;
float english_total = 0;
english_total = 0;
float science_total;
science_total = 0;
float algebra_avg, cs_avg, english_avg, science_avg;
float angela_avg, langston_avg, maya_avg, malcolm_avg;
float angela_total, langston_total, maya_total, malcolm_total;
angela_total = algebra1 + cs1 + english1 + science1;
langston_total = algebra2 + cs2 + english2 + science2;
maya_total = algebra3 + cs3 + english3 + science3;
malcolm_total = algebra4 + cs4 + english4 + science4;
angela_avg = average_calc(angela_total);
langston_avg = average_calc(langston_total);
maya_avg = average_calc(maya_total);
malcolm_avg = average_calc(malcolm_total);
student_heading();
cout << "What is today's date? (Example: May 1, 1996 = 05/01/1996): " << endl;
cin >> fecha;
cout << "DATE:" << fecha << "*************************************************** Page 1" << endl;
report_heading();
ifstream inData;
inData.open("infile.txt");
inData >> name1 >> name2 >> name3 >> name4 >> title1 >> title2;
cout << " " << name1 << " " << name2 << " " << name3 << " " << name4 << " " << title1 << " " << title2 << endl;
inData >> class1 >> algebra1 >> algebra2 >> algebra3 >> algebra4 >> algebra_total >> algebra_avg;
algebra_total = algebra1 + algebra2 + algebra3 + algebra4;
algebra_avg = average_calc(algebra_total);
cout << class1 << " " << algebra1 << " " << algebra2 << " " << algebra3 << " " << algebra4 << " " << algebra_total << " " << algebra_avg << endl;
inData >> class2 >> cs1 >> cs2 >> cs3 >> cs4 >> cs_total >> cs_avg;
cs_total = cs1 + cs2 + cs3 + cs4;
cs_avg = average_calc(cs_total);
cout << class2 << " " << cs1 << " " << cs2 << " " << cs3 << " " << cs4 << " " << cs_total << " " << cs_avg << endl;
inData >> class3 >> english1 >> english2 >> english3 >> english4 >> english_total >> english_avg;
english_total = english1 + english2 + english3 + english4;
english_avg = average_calc(english_total);
cout << class3 << english1 << english2 << english3 << english4 << english_total << english_avg;
inData >> class4 >> science1 >> science2 >> science3 >> science4 >> science_total >> science_avg;
science_total = science1 + science2 + science3 + science4;
science_avg = average_calc(science_total);
cout << class4 << science1 << science2 << science3 << science4 << science_total << science_avg;
inData >> title3;
cout << title3 << angela_avg << langston_avg << maya_avg << malcolm_avg << endl;
inData.close();
return 0;
}
void report_heading()
{
cout << "********************SMALL COLLEGE GRADE REPORT*******************" << endl;
}
void student_heading()
{
cout << "*******************" << endl;
cout << "Student" << endl;
cout << "ID" << endl;
cout << "SYCS-135 Computer Science I" << endl;
cout << "Assignment 5" << endl;
cout << "September 24, 2015" << endl;
cout << "******************" << endl;
}
float average_calc(float total_value)
{
float average;
average = total_value / 4;
return average;
这一行
inData >> class1 >> algebra1 >> algebra2 >> algebra3 >> algebra4 >> algebra_total >> algebra_avg;
尝试将字符串 "CS1" 读入 algebra_total
,这会导致流失败。要么不要尝试读取这些数字,要么将您的文本文件修复为
Angela Langston Maya Malcolm Total Average
Algebra 64.5 56.7 67.4 90.0 0 0
CS1 88.6 77.0 55.3 89.4 0 0
English 91.3 67.4 89.0 100.0 0 0
Science 100.0 89.4 80.2 91.4 0 0
Average