0

I have a test file ("InputFile.txt") saved with some random information as well as a blank output file ("output.txt"). When I debug the program and check the output file, it's still blank.

Should "output.out" be "output.txt" (line 25)? Am I having problems because I have "InputFile.txt" as a cstring at line 22 in the if statement? Is this correct otherwise? I'm sort of lost when it comes to fstream so far, please help.

My code:

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

using namespace std;

int main()
{
    ifstream InputFile;
    ofstream OutputFile;

    string lastName, firstName, fileName,
        line;
    int creditHours, state;
    int tuition;
    bool valid = false;

    do{
    cout << "Input file name (.txt): ";
    cin >> fileName;
    if(fileName == "InputFile.txt")
    {
        InputFile.open("InputFile.txt");
        OutputFile.open("output.out");
        valid = true;
    }
    else{
        cout << "Invalid file name/type." << endl;
    }
    }while(valid == false);




    OutputFile << "Last Name: " << '   ' << "First Name: " << '   '
        << "Credit Hours: " << '   ' << "Tuition Amount: " << endl;
    while(!InputFile.eof()){

    getline(cin, line);
    InputFile >> firstName >> lastName >> creditHours >> state;
    if(state == 1)
    {
        tuition = (creditHours * 350);
    }
    else
    {
        tuition = (creditHours * 570);
    }
    OutputFile << lastName << '   ' << firstName << '   ' 
        << creditHours << '   ' << tuition << endl;
    }



    InputFile.close();
    OutputFile.close();

    return 0;
}
user65384
  • 153
  • 1
  • 5
  • You never check any of your reads for success, so that would be a good place to start. Change `while (!InputFile.eof())` to `while (getline(cin, line) && InputFile >> firstName >> ... >> state)`. – chris Feb 26 '14 at 04:18
  • If you want the file output to be `output.txt` then it has to be `.open("output.txt")` – yizzlez Feb 26 '14 at 04:19
  • @chris : That whole thing should be in the while statement? – user65384 Feb 26 '14 at 04:20
  • @user65384, Yes, that would be the usual way of reading data until a read fails. I'm not particularly sure why the `getline(cin, line)` is in there at all, though. – chris Feb 26 '14 at 04:22
  • @chris: I didn't have the getline(cin, line) in there initially, but when I couldn't get it to work, I tried to use that one. Should it just be while(InputFile >> ... >> state), without the InputFile >> ...>> state; in the while loop itself? – user65384 Feb 26 '14 at 04:26
  • @user65384, Yes. You might refer to http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – chris Feb 26 '14 at 04:34

1 Answers1

2

Try this

ifstream inFile;
ofstream outFile;

and open your file in and out mode like

inFile.open("InputFile.txt", ios::in);
outFile.open("Output.txt",ios::out);

after that read Inputfile data and write it into output file

while (!inFile.eof()) {
inFile >> firstName >> lastName >> creditHours >> state;
outFile << firstName << " " << lastName<<"  "<<creditHours<< " " <<state<<"  "<< endl;
}

and apply logic within loop own way

Rishi Dwivedi
  • 898
  • 4
  • 19