-1

I am in a second-year computer science class and we are learning C++.

The assignment is to write a text file and calculate totals and averages based on the text file's data.

This is what my text file looks like:

        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

I was doing just fine until I compiled and debugged. My program will not print the second line of my text file correctly. It prints out:

-107374176.0 -107374176.0 -107374176.0 -107374176.0 -429496704.0 -107374176.0

instead of the data I have stored in the text file.

Please help. I'll provide any additional information that is needed to solve this problem. Also, just a reminder, I am a beginner C++ user.

Update: Here's the code:

#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;

1 Answers1

0

This line

inData >> class1 >> algebra1 >> algebra2 >> algebra3 >> algebra4 >> algebra_total >> algebra_avg;

attempts to read the string "CS1" into algebra_total, which causes the stream to fail. Either don't attempt to read those numbers, or fix your text file to something like

        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
user657267
  • 20,568
  • 5
  • 58
  • 77
  • Hey so I finished my code and now it gives me a debug error. Any comments? – Quishe' Branch Sep 28 '15 at 03:37
  • Run-Time Check Failure #3 - T – Quishe' Branch Sep 28 '15 at 04:18
  • @Quishe'Branch There has to be more information than that in the error. – user657267 Sep 28 '15 at 04:21
  • 'Assignment 5.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Cannot find or open the PDB file. 'Assignment 5.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll'. Cannot find or open the PDB file. 'Assignment 5.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel.appcore.dll'. Cannot find or open the PDB file. The thread 0x1438 has exited with code 0 (0x0). The thread 0x28c4 has exited with code 0 (0x0). The thread 0x3be8 has exited with code 0 (0x0). The program '[12712] Assignment 5.exe' has exited with code 0 (0x0). @user657267 – Quishe' Branch Sep 28 '15 at 04:22
  • Are you sure you're using the **exact** same code as in your question? Is there nothing else after "Run-Time Check Failure #3"? – user657267 Sep 28 '15 at 04:27
  • "Debug Error!" File: Run-Time Check Failure #3 - T" (Press retry to debug the application) & that's it. Can you find no errors? – Quishe' Branch Sep 28 '15 at 04:32
  • @Quishe'Branch I didn't realise you updated your code, sorry. You have a bunch of undefined variables being using in arithmetic at the top, e.g. `angela_total = algebra1 + cs1 + english1 + science1`. This is undefined behavior, make sure all of the variables are initialized before using them. – user657267 Sep 28 '15 at 04:38
  • They are defined in this line: float algebra1, cs1, english1, science1; – Quishe' Branch Sep 28 '15 at 05:04
  • @Quishe'Branch Sorry I meant *initialized*. Give them a value of 0 (or any value, just don't leave them uninitialized) before using them in calculations. – user657267 Sep 28 '15 at 05:20