-1

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();
}
Metally
  • 1
  • 1
  • Have you tried investigating why it gets stuck by stepping through the code with a debugger? – Quimby Oct 25 '22 at 12:17
  • 2
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Oct 25 '22 at 12:22
  • I have not. I am new and not too sure how to use that, started learning just about 8 weeks ago now. – Metally Oct 25 '22 at 12:22
  • [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Jesper Juhl Oct 25 '22 at 12:24

1 Answers1

1

Try replace your code

    while (!Infile.eof())
    {
         Infile >> saleNum >> itemType >> price;
         ...

by

    while (Infile >> saleNum >> itemType >> price)
    {
        ...
      
Kaktuts
  • 86
  • 3