2015-09-28 71 views
-1

我在第二年的計算機科學課,我們正在學習C++。第二行文本文件未讀; C++; Visual Studio

該任務是編寫一個文本文件並根據文本文件的數據計算總計和平均值。

這是我的文本文件看起來像:

 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; 
+0

直到您顯示代碼,這將很難說明確的事情。 –

+0

@Atomic_alarm我添加了代碼。 –

回答

0

此行

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 
+0

謝謝@ user657267 –

+0

嘿所以我完成了我的代碼,現在它給了我一個調試錯誤。任何意見? –

+0

@ Quishe'Branch什麼是錯誤? – user657267