input file

I am very new to programming, and I have little to no idea on what I am doing...
I need have a user type a input file and then I need the program to read the file and output the information... seems simple, but I can not get it to work. Here is what I have for my code... It could be completely wrong, but it is my best guess.. Thanks in advance for your help.

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
 #include <iostream>
#include <fstream>

using namespace std;

int main ()
{
    ifstream inFile;
    ofstream outFile;
    string name;
    string address;
    float childTickets;
    float adultTickets;
    float gamePacks;
    float donation;

    cout << "Enter the order filename: ";
    cin >> inFile;

    inFile.open();
    outFile.open();

    inFile >> name;
    inFile >> address;
    inFile >> childTickets;
    inFile >> adultTickets;
    inFile >> gamePacks;
    inFile >> donation;

    cout << name << address << childTickets << adultTickets
         << gamePacks << donation << endl;

inFile.close();
outFile.close();
return 0;
}
You had the write idea. Get it *write*, writing to files :P. Anyways on line 18 I believe you wanted the name of the file, so instead you should you input the file name via a string. And then you could inFile.open(*string variable name*) and then get all of your data from that file. Of course you should check if the file exists and if it doesn't just create a blank txt file via if (filestream.fail())
This looks like a good start; however, you need to save the user's answer to the prompt "Enter the order filename:". Then, use that answer to open your file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

string InFileName;

cout >> "Enter the order filename: ";

cin >> InFileName;

infile.open( InFileName.c_str() );

if( infile.good() )
    {
    ... // The rest of your program ...

    }
Topic archived. No new replies allowed.