Help! Input/Output file trouble

I decided to take a Computer science class and i have been doing well on my previous labs until this one was assigned. We are suppose to have an input file with games scores have have them outputted and calculated. It seems prety easy but for some reason, I cant do it! I've read my textbook over and over and i can't figure out what I'm dong wrong.
This is what I have in my input file:
Leila 700 800 1000

And this is my program so far:

#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main () {

ifstream inFile;
ofstream outFile;

double score1, score2, score3;
double average;

string firstName;


inFile.open ("C:\\game_scores.txt");
outFile.open ("gamescores.out");

cout<< "Processing data"<<endl;

inFile>> firstName;
outFile<<"Student name: "<< firstName
<< " "<<endl;

inFile>> score1>> score2>> score3;
outFile<<"Game Scores: "<<score1<<score2<<score3<<endl;

average = (score1+ score2 + score3)/ 3.0;

outFile<< "Average game scores: "<<average<<endl;

inFile.close();
outFile.close();

return 0;
}

there has to be a reason that the compiler can't read my input file!!! Please help!!! This is due monday and I can't figure out what is wrong!
Is the file being opened properly?

1
2
3
4
5
inFile.open ("C:\\game_scores.txt");
if (inFile.is_open())
   { cout << "File opened successfully"; }
else
  { cout << "File NOT opened successfully"; }
Last edited on
it outputted "File opened successfully"

but nothing from the input outputted!

and the compiler says that build is unsuccessful!

I don't know what I'm doing wrong
it outputted "File opened successfully"
...
and the compiler says that build is unsuccessful!


This makes no sense. If the build did not work, how can it have been run? Does the compiler give any other error messages? Your code works fine, by the way, although I didn't put game_scores.txt at the root of the C: drive when I tested it.
did you test it with a file? how did you enter the file in the code, maybe i'm doing that part wrong.
This is the code I ran.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main () {

ifstream inFile;
ofstream outFile;

double score1, score2, score3;
double average;

string firstName;


inFile.open ("game_scores.txt");
outFile.open ("gamescores.out");

cout<< "Processing data"<<endl;

inFile>> firstName;
outFile<<"Student name: "<< firstName
<< " "<<endl;

inFile>> score1>> score2>> score3;
outFile<<"Game Scores: "<<score1<<score2<<score3<<endl;

average = (score1+ score2 + score3)/ 3.0;

outFile<< "Average game scores: "<<average<<endl;

inFile.close();
outFile.close();

return 0;
}


I put game_scores.txt in the same directory as the executable I built. Note that on many systems (actually, I say "many", but I'm thinking Visual Studio on Windows), the executable is not built in the same directory that the source code is stored in.
Last edited on
i just ran the code you ran. When i execute it, all it reads is
"Processing data"
the information from input file did not show.

is there a specific place that the file should be saved to?
I made some changes to the program and put the input file where the executable is but it does not seem to be working;

there is the program as of now:

#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;
int main () {
ifstream inFile;
ofstream outFile;

double score1, score2, score3;
double average;
string firstName;

inFile.open ("game_scores.txt");
outFile.open ("gamescores.out");

cout<< "Processing data"<<endl;

if (inFile.is_open())
{
inFile>> firstName;
outFile<<"Student name: "<< firstName<<endl;
inFile>> score1>> score2>> score3;
outFile<<"Game Scores: "<<score1<<score2<<score3<<endl;
average = (score1+ score2 + score3)/ 3.0;
outFile<< "Average game scores: "<<average<<endl;
}
else cout<<"Unable to open files"<<endl;

inFile.close();
outFile.close();

return 0;
}



please help nd tell me what is wrong with it!!!!

the information from input file did not show.


It's not meant to show. It's meant to go into the output file.
but i'm suppose to be creating a program that reads names of three kids and their game scores
This program does that. It reads all the data from the file and processes it and writes the results out to another file. Is that not what you wanted? We can't guess. If you want your program to do something that it's not doing, you have to tell us what.
Topic archived. No new replies allowed.