Hello everyone. I am trying to pull data from a file and show it on the screen. There is a condition in the file that says if it is a "payment" or "order". A "P" and an "O" in the first column. My output becomes an infinite loop. I have an if statement because if it's a payment, there are less fields in the file and the datafile statement might confuse the "item" field when it is an order, for the "amnt" field when it's a payment.

Ultimately this is for an accounts receivable program that uses this file to manipulate a customer balance file. For now I want to just display the file contents.

Thank you in advance:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct record  //Transaction.
{
       char name[14];
       char type[1];
       int custnum;
       int transnum;
       char item[6];
       int qty;
       int amnt;
       int balance;
};

int main()
{
const int num_cust = 7; //num of customers
record cust[num_cust]; 


cout << "ABC Hardware Company \n\n";  

 fstream datafile;                                   
    datafile.open("trans.txt");
    if(!datafile)
    {
      cout << "Could not open file. \n\n";          
      cin.get();
      cin.get();
    
       return 0;
    }           
   while (!datafile.eof())       
   {            
    for (int i = 0; i < num_cust; i++)    
        {             
          if ('cust[i].type == O')
          {                                        
           datafile >> cust[i].type >> cust[i].custnum >> cust[i].item >> cust[i].qty >> cust[i].amnt; 
           cout << cust[i].type << "  " << cust[i].custnum << "  " << cust[i].item << "  " << cust[i].qty << "  " << cust[i].amnt "  ";
           
          } 
           else 
            datafile >> cust[i].type >> cust[i].custnum >> cust[i].amnt;
            cout << cust[i].type << "  " << cust[i].custnum << "  ";
          
        }    
    }
cin.get();    
   return 0;
}

DATA FILE:

O 1111 BOLTS 5 1
O 1111 SCREWS 6 5
P 1111 20
P 1111 5
O 1111 NAILS 3 2
O 2222 NAILS 1 2
P 2222 1
O 2222 HAMMER 1 10
O 2222 DRIVER 2 5
P 2222 10
O 3333 LADDER 1 10
P 3333 5
P 3333 1
O 3333 BELT 1 5
O 3333 GLOVES 1 3
O 4444 BOLTS 5 1
O 4444 SCREWS 6 5
P 4444 20
P 4444 5
O 4444 NAILS 3 2
O 5555 NAILS 1 2
P 5555 1
O 5555 HAMMER 1 10
O 5555 DRIVER 2 5
P 5555 10
O 6666 LADDER 1 10
P 6666 5
P 6666 1
O 6666 BELT 1 5
O 6666 GLOVES 1 3
O 7777 DRILL 1 20
O 7777 SCREWS 2 5
O 7777 BATTERY 2 10
P 7777 30
P 7777 50

I was supposed to include "payment" in the item field according to the assignment, that will make this easier. No need for If statement since I will be printing out all fields regardless.

while (!datafile.eof())       
   {            
    for (int i = 0; i < num_trans; i++)
    {
          datafile >> cust[i].type >> cust[i].custnum >> cust[i].item >> cust[i].qty >> cust[i].amnt;                     
          cout << cust[i].type << "  " << cust[i].custnum << "  " << cust[i].item << "  " << cust[i].qty << "  " << cust[i].amnt << "  \n";    
    }
cin.get();    
   return 0;
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.