I am currently working on an assignment where I have to read data from a text file and then separate its content into separate text files based on a character, ie:
5555 t 2345
2345 f 9910
5520 x 1850
What I'm currently struggling with is getting the code to read the next line after it finishes the first line. It just gets stuck looping the first line from the input file over and over. I've read multiple people saying .eof could be the problem but even when I rewrote the code without it, it still looped on first line. Even when I removed all conditionals, is it cause I'm using a char? I was able to loop different text files without multiple inputs just fine.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream Infile;
ofstream OutfileTree;
ofstream OutfileFlow;
ofstream ErrorFile;
int saleNum, price, flowerTotal=0, treeTotal=0;
int current_line =0;
char itemType;
Infile.open ("TreeIn.txt");
OutfileTree.open ("TreesOut.txt");
OutfileFlow.open ("FlowersOut.txt");
ErrorFile.open ("Errors.txt");
if (!Infile)
{
cout <<"Cannot open Infile, terminating program" << endl;
exit (1);
}
if (!OutfileTree)
{
cout <<"Cannot open Outfile Tree, terminating program" << endl;
exit (1);
}
if (!OutfileFlow)
{
cout <<"Cannot open Outfile Flow, terminating program" << endl;
exit (1);
}
if (!ErrorFile)
{
cout <<"Cannot open ErrorFile, terminating program" << endl;
exit (1);
}
OutfileTree << fixed << showpoint << setprecision(2);
OutfileFlow << fixed << showpoint << setprecision(2);
while (!Infile.eof())
{
Infile >> saleNum >> itemType >> price;
if (itemType =='T')
OutfileTree << "Sales Number:" << saleNum << " " << "Price:" << price << endl;
else if (itemType =='F')
{
OutfileTree << "Sales Number:" << saleNum << " " << "Price:" << price << endl;
}
else
{
ErrorFile << "Error" << endl;
ErrorFile << saleNum << price << endl;
}
}
Infile.close();
OutfileTree.close();
OutfileFlow.close();
ErrorFile.close();
}