Search code examples
c++visual-studiofiletextgetfiles

Second Line of Text File Not Reading; C++; Visual Studio


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;

Solution

  • 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