Pathing for reading a text file

I need to open a text file game_scores.txt and i can not figure out a way to.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
int n1;
int n2;
int n3;
string name;
double avg;
ifstream inFile;
ifstream outFile;
inFile.open("game_scores.txt");
inFile >>n1>>n2>>n3>>name;

cout << n1 << n2 << n3 << name;
avg=n1 * .20 +n2 * .30 +n3 * .50;

the code is used to read the text and take the first 4 pieces of info but i do not know how to do it. i have already created the text file and moved it into the source. i am using visual studios 2012. thanks

in 2010 the text file would show a relative path, it doesn't in 2012
Last edited on
How do you know this isn't working? Also, sometimes the file needs to be in the project or solution directory rather than the source directory.
i believe it is not working because when i run it, a bunch of random numbers appear
Try adding this line in after you open the file:

std::cout << "File is open: " << std::boolalpha << inFile.is_open() << std::endl;

See whether it says true or false.
Last edited on
ok i got it working!! now i just need to write the contents of the program to an output file which i have no idea how to, can anyone help me with this
thanks again
Writing to an output file is just like writing to cout, except you use the name of the variable for the output file instead. You can open the outfile in a similar way to how you open the input file. I see in the code in your first post you have outFile, but it's of type ifstream instead of ofstream. It needs to be ofstream.
How would i do that with an if statement?
Last edited on
How wouldn't you? I'm not sure what you mean.
for ex.
outFile.open("game_totals.txt");
outFile<<
if (avg<1000)
{
cout<<name<<" -beginner- Keep Practicing!";
cout<<endl;
}
if (avg>1000&&avg<1500)
{
cout<<name<<" -intermediate-";
cout<<endl;
}
if (avg>1500)
{
cout<<name<<" -advanced- Good Job!";
cout<<endl;
name;
}

the if would be underlined red and it wont print into the output doc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
outFile.open("game_totals.txt");
outFile<< //remove this line
if (avg<1000)
{
    cout<<name<<" -beginner- Keep Practicing!"; //change 'cout' to 'outFile'
    cout<<endl; //change 'cout' to 'outFile'
}
if (avg>1000&&avg<1500)
{
    cout<<name<<" -intermediate-"; //change 'cout' to 'outFile'
    cout<<endl; //change 'cout' to 'outFile'
}
if (avg>1500)
{
    cout<<name<<" -advanced- Good Job!"; //change 'cout' to 'outFile'
    cout< <endl; //change 'cout' to 'outFile'
    name; //remove this line
}
i love you ;) thanks
Topic archived. No new replies allowed.