HELP with Input/Output file

Hello all! I have recently begun practicing programming using c++ for my classes in computer programming and am encountering issues producing the correct output to an output file on my personal computer.

I have noticed that the same program will run on computers at school and at home and the results of the program run on computers at school are accurate. However, those produced on my personal computer are not. I am using exactly the same program and ensuring that my input files and output files are in the same folder as my program is in, yet, when I run the program, the output file is populated with nonsensical numbers that do not display the data extracted from the input file or the calculations performed on the data intended to be displayed in the output file.

Below, is the program referred to above. Again, when run on computers at school, the program produces the correct output which should be along the lines of

2492 Vegeta <average1> // replace average 1 with actual average
9001 Goku <average2> // replace average 2 with actual average
2361 Piccolo <average3> // replace average 3 with actual average,

after formatting functions are included (i.e. setw() and setprecision()).

However, the program run on my personal computer produces output along the lines of

-7244844694.63503e-308

for every row of data that is to be extracted from the input file and manipulated for the output file. I am using code::blocks on both my personal computer and the computers at school so I am suspicious as to whether there may be a setting that is distinct on one from the other that must be activated or deactivated for my computer to produce the same output. Please help, I have been manipulating the program at home for some time now and have not produced much more than the nonsensical numbers similar to that displayed above.


#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>

using namespace std;

int main ( )
{
int student_id;

double grade1,
grade2,
grade3,
average;

string student_name;

ifstream fin;
fin.open ("student_data.txt");

ofstream fout;
fout.open ("averages.txt");



fin >> student_id >> student_name >> grade1 >> grade2 >> grade3;

average = ( grade1 + grade2 + grade3 ) / 3;

fout << student_id << student_name << average;


fin >> student_id >> student_name >> grade1 >> grade2 >> grade3;

average = ( grade1 + grade2 + grade3 ) / 3;

fout << student_id << student_name << average;


fin >> student_id >> student_name >> grade1 >> grade2 >> grade3;

average = ( grade1 + grade2 + grade3 ) / 3;

fout << student_id << student_name << average;


system ("PAUSE > NUL");

return 0;

}
initialize your variables
Why would initializing the variables help? I am attempting to extract data from an input file so initializing the variables would give values that would interfere with the data to be pulled in from the input file, wouldn't it? Or is there a version of initialization different from what I'm thinking, that of assigning values to the variables in the declaration section of the program?

The input file contains data that resembles the table below:

2492 Vegeta 68 78 90
9001 Goku 98 97 87
2361 Piccolo 95 85 97

My intent is to extract this data using the ifstream function to manipulate by calculating averages in the calculations indicated under each extraction line and above each insertion line so as to insert the student's ID, name, and average based on the values indicated above, in the output file.
Last edited on
The garbage output such as -7244844694.63503e-308 would result from uninitialised variables.

However, the root cause of the problem is that (a) there is no check that the input file was opened, and (b) there is no check that the reading from the input was successful.

(a) Open the input and check it was successful:
1
2
3
4
5
    ifstream fin("student_data.txt");
    if (!fin)
    {
        cout << "could not open input file\n";
    }


(b) Check that the input operation succeeded:
1
2
3
4
5
    if (fin >> student_id >> student_name >> grade1 >> grade2 >> grade3)
    {  
        average = ( grade1 + grade2 + grade3 ) / 3;
        fout << student_id << student_name << average << '\n';
    }


Also, rather than using individual statements for each and every student, your program should use a loop.
Last edited on
Thank you, Chervil. I have including the file testing code and when run, the program states that the input file was not read. I do not understand why the input file is not being read though, as I have verified that both the input file and the source code are in the same folder and the input file name is spelled correctly or the same within the source code and the file itself.
I have verified that both the input file and the source code are in the same folder

Try placing the input file in the same folder as wherever the output file "averages.txt" is being created. That may not be the same as the source files.
-12136170694.63503e-308

:'( Just more garbage. I don't see why it's producing these nonsensical outputs when the same program run on school computers produces the desired result. That's why I was of the opinion that perhaps some setting with the IDE being used may be the culprit (i.e. code::blocks). I am using the same IDE on my personal computer and computers at school.
SamuelAdams and Chervil. Thank you for your help. I determined the issue. I mistakenly named the text document "student_data.txt" instead of "student_data" so upon removing the ".txt" portion of the text document's name, no more garbage. The program actually ran according to its intention. Thank you for your help. I appreciate your time and insight.
Last edited on
Topic archived. No new replies allowed.