I have a program where it reads 2 files, one of them containing a file with customer number (int), customer name (string) and balance (double).

There are 7 lines, but my program would only read the first line 7 times. This program is confusing me as to why they won't update.

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

using namespace std;

int main ()
{
 int Mcust_num;         //master customer number
 string Mcust_name;     //master customer name
 string Mlast_name;
 double Mcust_bal;      //master customer balance
 string line;
 
 char Tcode;            //transaction code type
 int Tcust_num;         //transaction customer number
 string Titem_name;     //transaction item name
 int Tquant;            //transaction quantity order
 double Tcostpay;       //transaction cost/payment
 
  fstream masterfile ("masterfile.txt");
  fstream transactionfile ("transactionfile.txt");
  
  masterfile >> Mcust_num >> Mcust_name >> Mlast_name >> Mcust_bal; //read master file
  transactionfile >> Tcode >> Tcust_num >> Titem_name >> Tquant >> Tcostpay;   //read transaction file

  if ( (masterfile.is_open()) && (transactionfile.is_open()) )         //if both masterfile and transaction file are open
  {
       
    while ( (!masterfile.eof()) && (!transactionfile.eof()) )       //while neither are not end of file
    {
      getline (masterfile, line);

//              cout << line << endl;
          if (Mcust_num == Tcust_num) //if transaction and master customer numbers equal to each other
          {
                    if (Tcode == 'O') 
                    {
                       Mcust_bal = Mcust_bal + (Tcostpay * Tquant);
                    }
                    else
                    {
                       Mcust_bal = Mcust_bal - Tcostpay;
                    }
              cout << '\t' << Tcode << '\t' << Tcust_num << '\t' << Titem_name << '\t' << Tquant << '\t' << Tcostpay << endl;
              
          }    //closes transaction and master cust numbers equals if
          
//                cout << '\t' << line << endl;
                cout << '\t' << Mcust_num << '\t' << Mcust_name << " " << Mlast_name << endl;
                cout << '\t' << "Previous Balance: $" << Mcust_bal << endl;
//                cout << '\t' << Tcode << '\t' << Tcust_num << '\t' << Titem_name << '\t' << Tquant << '\t' << Tcostpay << endl;
                cout << '\t' << '\t' << "Balance Due: "  << '\t' << "$" << Mcust_bal << endl;
                cout << endl;
    }       //closes while statement
    
    masterfile.close();
    transactionfile.close();

    } //closes if statement

  else cout << "Unable to open file"; 

  system("PAUSE");
  return 0;
}

Recommended Answers

All 16 Replies

the reason you are only getting one set of inputs from the file is because you only read it once. On lines 26 and 27 you read in from the files and store the data into your variables and that is the only time you put anything into those variables. I would suggest doing something like

//...
ifstream masterfile ("masterfile.txt");
ifstream transactionfile ("transactionfile.txt");
if (masterfile.isopen() && transactionfile.isopen())
{
    while (masterfile >> Mcust_num >> Mcust_name >> Mlast_name >> Mcust_bal &&
           transactionfile >> Tcode >> Tcust_num >> Titem_name >> Tquant >> Tcostpay)
    {
        // your code here
    }
//...
}

You should try using the seekg function to set the read pointer to the spot where you want to start reading. You might design a function to find the beginning of the line, then use seekg to get there.

commented: Very bad suggestion. Unnecessary and convoluted. -4

I am seeing a read on line 34 as well, into the variable line. However, you simply read it into that variable and never use it. I see a couple of commented out lines that display it, but it never gets into any of your other variables and it looks completely different from 26 and 27.

I am seeing a read on line 34 as well, into the variable line. However, you simply read it into that variable and never use it. I see a couple of commented out lines that display it, but it never gets into any of your other variables and it looks completely different from 26 and 27.

The only reason why I commented out the line is because it displays the whole line, and the only thing I didn't want to display the line in that order.

the reason you are only getting one set of inputs from the file is because you only read it once. On lines 26 and 27 you read in from the files and store the data into your variables and that is the only time you put anything into those variables. I would suggest doing something like

//...
ifstream masterfile ("masterfile.txt");
ifstream transactionfile ("transactionfile.txt");
if (masterfile.isopen() && transactionfile.isopen())
{
    while (masterfile >> Mcust_num >> Mcust_name >> Mlast_name >> Mcust_bal &&
           transactionfile >> Tcode >> Tcust_num >> Titem_name >> Tquant >> Tcostpay)
    {
        // your code here
    }
//...
}

I tried this and instead I get no data displayed at all :(

I am sorry but I could not follow your problem statement. Can you please re word your problem statement in a more simpler manner

replace line 34 with line 26

I am sorry but I could not follow your problem statement. Can you please re word your problem statement in a more simpler manner

Basically I'm reading in 2 files: masterfile and transaction file. I'm trying to read in the masterfile and display in the console, but it only repeats the first line, instead of going through the file.

Basically I'm reading in 2 files: masterfile and transaction file. I'm trying to read in the masterfile and display in the console, but it only repeats the first line, instead of going through the file.

Which makes sense looking at the code. Have you tried Walt P's suggestion yet? Copy line 27 to line 35 while you're at it.

Which makes sense looking at the code. Have you tried Walt P's suggestion yet? Copy line 27 to line 35 while you're at it.

I have done both your suggestions. The masterfile is looping correctly, but the transaction only lists the first one and then stops, basically only showing the first line. If the transaction file loops as well, then this can all go well.

This is what my code looks like :

ifstream masterfile ("masterfile.txt");
  ifstream transactionfile ("transactionfile.txt");
  
//  transactionfile >> Tcode >> Tcust_num >> Ttrans_num >> Titem_name >> Tquant >> Tcostpay;   //read transaction file

  if ( (masterfile.is_open()) && (transactionfile.is_open()) )         //if both masterfile and transaction file are open
  {

   while ( (!masterfile.eof()) && (!transactionfile.eof()) )       //while neither are not end of file
    {
           masterfile >> Mcust_num >> Mcust_name >> Mlast_name >> Mcust_bal;       //read master file
           transactionfile >> Tcode >> Tcust_num >> Ttrans_num >> Titem_name >> Tquant >> Tcostpay;   //read transaction file
           
              cout << '\t' << Mcust_num << '\t' << Mcust_name << " " << Mlast_name << endl;
              cout << '\t' << "Previous Balance: $" << fixed << setprecision(2) << Mcust_bal << endl;
              cout << endl;

          if (Mcust_num == Tcust_num) //if transaction and master customer numbers equal to each other
          {
                    if (Tcode == 'O') 
                    {
                       Mcust_bal = Mcust_bal + (Tcostpay * Tquant);
                    }
                    else
                    {
                       Mcust_bal = Mcust_bal - Tcostpay;
                    }
              cout << '\t' << Tcode << '\t' << Titem_name << '\t' << Tquant << '\t' << Tcostpay << endl;
          }    //closes transaction and master cust numbers equals if
          
                cout << '\t' << '\t' << "Balance Due: "  << '\t' << "$" << fixed << setprecision(2) << Mcust_bal << endl;
                cout << endl;
    }       //closes while statement
    
    masterfile.close();
    transactionfile.close();

    } //closes if statement

I don't know why the masterfile is fine but the transaction is not.

the reason you are only getting one set of inputs from the file is because you only read it once. On lines 26 and 27 you read in from the files and store the data into your variables and that is the only time you put anything into those variables. I would suggest doing something like

//...
ifstream masterfile ("masterfile.txt");
ifstream transactionfile ("transactionfile.txt");
if (masterfile.isopen() && transactionfile.isopen())
{
    while (masterfile >> Mcust_num >> Mcust_name >> Mlast_name >> Mcust_bal &&
           transactionfile >> Tcode >> Tcust_num >> Titem_name >> Tquant >> Tcostpay)
    {
        // your code here
    }
//...
}

I tried it your way and I end up with a correct masterfile which loops correctly, and a transactionfile that only displays the first line.

Can you post the code you have now?

Can you post the code you have now?

ifstream masterfile ("masterfile.txt");
  ifstream transactionfile ("transactionfile.txt");
  
//  transactionfile >> Tcode >> Tcust_num >> Ttrans_num >> Titem_name >> Tquant >> Tcostpay;   //read transaction file

  if ( (masterfile.is_open()) && (transactionfile.is_open()) )         //if both masterfile and transaction file are open
  {

//   while ( (!masterfile.eof()) && (!transactionfile.eof()) )       //while neither are not end of file
     while (masterfile >> Mcust_num >> Mcust_name >> Mlast_name >> Mcust_bal &&
            transactionfile >> Tcode >> Tcust_num >> Ttrans_num >> Titem_name >> Tquant >> Tcostpay)
    {
//           masterfile >> Mcust_num >> Mcust_name >> Mlast_name >> Mcust_bal;       //read master file
  //         transactionfile >> Tcode >> Tcust_num >> Ttrans_num >> Titem_name >> Tquant >> Tcostpay;   //read transaction file
           
              cout << '\t' << Mcust_name << " " << Mlast_name << '\t' << Mcust_num << endl;
              cout << '\t' << '\t' << "Previous Balance $" << fixed << setprecision(2) << Mcust_bal << endl;
              cout << endl;

          if (Mcust_num == Tcust_num) //if transaction and master customer numbers equal to each other
          {
                    if (Tcode == 'O') 
                    {
                       Tcostpay = Tcostpay * Tquant;
                       Mcust_bal = Mcust_bal + Tcostpay;
                    }
                    else
                    {
                       Mcust_bal = Mcust_bal - Tcostpay;
                    }
              cout << "Transaction #" << Ttrans_num << '\t' << Titem_name << '\t' << "$" << Tcostpay << endl;
              cout << endl;
          }
          else if (Tcust_num != Mcust_num)
          {
              cout << "Invalid matching of master and transaction file" << endl;
          }    //closes transaction and master cust numbers equals if
          
                cout << '\t' << '\t' << "Balance Due: "  << '\t' << "$" << fixed << setprecision(2) << Mcust_bal << endl;
                cout << endl;
    }       //closes while statement
    
    masterfile.close();
    transactionfile.close();

    } //closes if statement

  else cout << "Unable to open file";

You print out some of the values of the next line of the transaction file only if the current transaction number equals the current customer number.

Your loop now reads a line out of the masterfile and a line out of the transactionfile, and prints one of "Transaction#..." (and some of the values from the current line of the transaction file) only if the transaction customer number equals the masterfile customer number; otherwise it prints "Invalid matching...". Either way, the line is followed by "Balance due:...", right?

If you're not seeing this, please provide your two input files, and the output you're seeing, it might help determine what's going wrong.

I ended up solving this. Here is the code:

int main ()
{
 int Mcust_num;         //master customer number
 string Mcust_name;     //master customer name
 string Mlast_name;
 double Mcust_bal;      //master customer balance
 
 char Tcode;            //transaction code type
 int Tcust_num;         //transaction customer number
 int Ttrans_num;        //transaction number
 string Titem_name;     //transaction item name
 int Tquant;            //transaction quantity order
 double Tcostpay;       //transaction cost/payment
 
  ifstream masterfile ("masterfile.txt");
  ifstream transactionfile ("transactionfile.txt");
  
  if ( (masterfile.is_open()) && (transactionfile.is_open()) )         //if both masterfile and transaction file are open
  {
       while (masterfile >> Mcust_num >> Mcust_name >> Mlast_name >> Mcust_bal) 
           {
              cout << '\t' << Mcust_name << " " << Mlast_name << '\t' << Mcust_num << endl;
              cout << endl;
              cout << '\t' << '\t' << "Previous Balance $" << fixed << setprecision(2) << Mcust_bal << endl;
              cout << endl;
              
               while (transactionfile >> Tcode >> Tcust_num >> Ttrans_num >> Titem_name >> Tquant >> Tcostpay) 
                  {
                     if (Mcust_num == Tcust_num) //if transaction and master customer numbers equal to each other
                        {
                               if (Tcode == 'O') 
                                  {
                                        Tcostpay = Tcostpay * Tquant;
                                        Mcust_bal = Mcust_bal + Tcostpay;
                                  }
                               else
                               {
                                        Mcust_bal = Mcust_bal - Tcostpay;
                               }
                               cout << "Transaction #" << Ttrans_num << '\t' << Titem_name << '\t' << "$" << Tcostpay << endl;
                        }   //closes if statement
                  }    //closes transaction while loop
             transactionfile.clear();
             transactionfile.seekg(0L, ios::beg);
                cout << endl;
                cout << '\t' << '\t' << "Balance Due: "  << '\t' << "$" << fixed << setprecision(2) << Mcust_bal << endl;
                cout << endl;
             }       //closes while statement
    
    masterfile.close();
    transactionfile.close();

    } //closes if statement

  else cout << "Unable to open file" << endl; 
}

The only thing I want to do is an error check, if there are any duplicate master records, but I'm not too sure how to do that. The only thing I can think of is reading every masterfile and doing a check, but I don't know how to put that into code form.

Well you can read the Mcust_num of masterfile into some sort of array like a vector or a plan old array. Then you can loop through the array and using the find function from the <algorithms> header call find on the current record that you are on and if you find another one then you would have a duplicate.

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.