Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,573 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,921 people online right now. Registration is fast and FREE... Join Now!




Read From a File

 
Reply to this topicStart new topic

Read From a File

cooplis
3 May, 2008 - 11:33 AM
Post #1

D.I.C Head
**

Joined: 11 Sep, 2007
Posts: 77


My Contributions
Hello there, I need some help. I need to read from a file and and build my structs. This is what I have done.

Help

cooplis biggrin.gif

CODE

#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

// Function Prototypes
    void DisplayTitle();
    double GetBegBal();
    void DisplayBal(double);
    void GetData(int& , double&);
    double ProcessCheck(double, double);
    double ProcessDeposit(double, double);
    double ProcessATM(double, double);
    double ProcessSvcChg(double);
    double ProcessAutopay(double, double);

//Global Constants
const double    CHARGE = 10,
                ATMFEE =  2;
struct record
    {
    int transCode;
    double transAmt;
    };
int main()
{
    //Variable Declarations
    using namespace std;
    int    transCode;  
    double  balance, transAmt;
    
    

    ifstream in_stream;
    ofstream out_stream;
      
    
    //in_stream.open("c:\\desktop\checkin.dat");
    
    
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    DisplayTitle();
    balance = GetBegBal();
    GetData(transCode, transAmt);
    
    while(transCode != 0)
    {
        switch(transCode)
        {
            case 1: balance = ProcessCheck(balance, transAmt); break;
            case 2: balance = ProcessDeposit(balance, transAmt); break;
            case 3: balance = ProcessATM(balance, transAmt); break;
            case 4: balance = ProcessAutopay(balance, transAmt); break;
        }
        DisplayBal(balance);
        if(balance < 0)
            balance = ProcessSvcChg(balance);
        GetData(transCode, transAmt);
    }
    
system("pause");
}    


    void DisplayTitle()
    {
        cout << "\n       Check Register\n\n";
    }

    double GetBegBal()
    {
        double x;
        cout << "  Enter beginning balance ";
        cin >> x;
        return x;
    }


    void DisplayBal(double x)
    {
        cout << "\t\tBalance = $" << setw(10) << x;
    }

    void GetData(int& x, double& y)
    {
        cout << "\n\n  Enter transaction code (0 to exit) ";
        cin >> x;
        if(x > 0)
        {
            cout << "\n  Enter transaction amount ";
            cin >> y;
        }
    }

    double ProcessCheck(double bal, double amt)
    {
        cout << "\n  Check =    " << setw(10) << amt;
        return (bal - amt);
    }

    double ProcessDeposit(double bal, double amt)
    {
        cout << "\n  Deposit =  " << setw(10) << amt;
        return (bal + amt);
    }
    double ProcessATM(double bal, double amt)
    {
        cout << "\n  ATM     =  " << setw(10) << amt;
        bal = bal - amt;
        DisplayBal(bal);
        bal = bal - ATMFEE;
        cout << "\n  ATM Fee =  " << setw(10) << ATMFEE;
        return (bal);
    }
    double ProcessSvcChg(double bal)
    {
        cout << "\n  Service chg =" << setw(8) << CHARGE;
        bal = bal - CHARGE;
        DisplayBal(bal);
        return (bal);
    }
     double ProcessAutopay(double bal, double amt)
    {
        cout << "\n  Withdrawal =    " << setw(10) << amt;
        return (bal - amt);
    }

    


User is offlineProfile CardPM
+Quote Post

cooplis
RE: Read From A File
5 May, 2008 - 11:17 AM
Post #2

D.I.C Head
**

Joined: 11 Sep, 2007
Posts: 77


My Contributions
CODE

#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

// Function Prototypes
    void DisplayTitle();
    double GetBegBal();
    void DisplayBal(double);
    void GetData(int& , double&);
    double ProcessCheck(double, double);
    double ProcessDeposit(double, double);
    double ProcessATM(double, double);
    double ProcessSvcChg(double);
    double ProcessAutopay(double, double);

//Global Constants
    
    ifstream in_stream;
    ofstream out_stream;

const double    CHARGE = 10,
                ATMFEE =  2;
        double    next;
struct chkrecord
    {
    int transCode;
    double transAmt;
    };
struct totals
    {
    int credits;
    int debits;
    int service;
    };
int main()
{
    //Variable Declarations
    using namespace std;
    
    //GetData(record.transCode, record.transAmt);

    double  balance;

    
    in_stream.open("checkin_dat.txt");
    in_stream >> next;
    
        
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    //DisplayTitle();
    balance = GetBegBal("checkin.dat");
    GetData(transCode, transAmt);
    
    while(record.transCode != 0)
    {
        switch(record.transCode)
        {
            case 1: balance = ProcessCheck(balance, transAmt); break;
            case 2: balance = ProcessDeposit(balance, transAmt); break;
            case 3: balance = ProcessATM(balance, transAmt); break;
            case 4: balance = ProcessAutopay(balance, transAmt); break;
        }

        DisplayBal(balance);
        if(balance < 0)
        balance = ProcessSvcChg(balance);
        GetData(transCode, transAmt);
    }

        out_stream.open("checkout.txt");
    

system("pause");
}    


/*    void DisplayTitle()
    {
        //cout << "\n       Check Register\n\n";
    }*/

        double GetBegBal()
    {
        double x;
        ifstream ChkBal;
        ChkBal.open("e:Checkin.dat");
            ChkBal >> x;
        return x;
    }


    void DisplayBal(double x)
    {
        in_stream << "\t\tBalance = $" << setw(10) << x;
    }

    void GetData(int& x, double& y)
    {
        
        cout << "\n\n  Enter transaction code (0 to exit) ";
        in_stream >> x;
        if(next > 0)
        {
        //    cout << "\n  Enter transaction amount ";
            in_stream >> next;
        }
    }

    double ProcessCheck(double bal, double amt)
    {
        //cout << "\n  Check =    " << setw(10) << amt;
        return (bal - amt);
    }

    double ProcessDeposit(double bal, double amt)
    {
        //cout << "\n  Deposit =  " << setw(10) << amt;
        return (bal + amt);
    }
    double ProcessATM(double bal, double amt)
    {
        //cout << "\n  ATM     =  " << setw(10) << amt;
        bal = bal - amt;
        DisplayBal(bal);
        bal = bal - ATMFEE;
        //cout << "\n  ATM Fee =  " << setw(10) << ATMFEE;
        return (bal);
    }
    double ProcessSvcChg(double bal)
    {
        //cout << "\n  Service chg =" << setw(8) << CHARGE;
        bal = bal - CHARGE;
        DisplayBal(bal);
        return (bal);
    }
     double ProcessAutopay(double bal, double amt)
    {
        //cout << "\n  Withdrawal =    " << setw(10) << amt;
        return (bal - amt);
    }



File reads like this:
2000 1 1225.72 1 463.81 3 200 4 632 2 1500 1 300 2 1800


Ok I got it to read the file and get the beginning balance, but now I have to change the cin's to pull from the file and display on the screen. This project got hard to do fast. Can anyone help me??

Thanks,

Cooplis smile.gif
User is offlineProfile CardPM
+Quote Post

cooplis
RE: Read From A File
7 May, 2008 - 05:06 AM
Post #3

D.I.C Head
**

Joined: 11 Sep, 2007
Posts: 77


My Contributions
Can someone give me assistance on how the struct should be used in my code?

Thanks,

Lisa W.
User is offlineProfile CardPM
+Quote Post

jeronimo0d0a
RE: Read From A File
7 May, 2008 - 07:14 AM
Post #4

D.I.C Head
**

Joined: 3 Mar, 2008
Posts: 141


My Contributions
structure members are accessed with the . operator as struct.variable. They can also be accessed as pointer variables struct->varaible, but for now let's stay with the dot. The patched code contains examoles of this and comments.
If you need more help, just ask,
Jeronimo
CODE


#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

// Function Prototypes
    void DisplayTitle();
    double GetBegBal();
    void DisplayBal(double);
    void GetData(int& , double&);
    double ProcessCheck(double, double);
    double ProcessDeposit(double, double);
    double ProcessATM(double, double);
    double ProcessSvcChg(double);
    double ProcessAutopay(double, double);

//Global Constants
    
    ifstream in_stream;
    ofstream out_stream;

const double    CHARGE = 10,
                ATMFEE =  2;
        double    next;
struct chkrecord
    {
    int transCode;
    double transAmt;
    };
struct totals
    {
    int credits;
    int debits;
    int service;
    };
int main()
{
    //Variable Declarations
    using namespace std;
    
    //GetData(record.transCode, record.transAmt);

    double  balance;

    
    in_stream.open("checkin_dat.txt");
    in_stream >> next;
    
        
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    //DisplayTitle();
    balance = GetBegBal(); // no arguements ("checkin.dat");
    chkrecord record; // must have a struct that contains this variable
    // must use that struct with the variables i.e. struct.variable
    record.transCode = 0; // initialize condition
    GetData(record.transCode, record.transAmt);
    
    while(record.transCode != 0)
    {
        switch(record.transCode)
        {
            case 1: balance = ProcessCheck(balance, record.transAmt); break;
            case 2: balance = ProcessDeposit(balance, record.transAmt); break;
            case 3: balance = ProcessATM(balance, record.transAmt); break;
            case 4: balance = ProcessAutopay(balance, record.transAmt); break;
        }

        DisplayBal(balance);
        if(balance < 0)
        balance = ProcessSvcChg(balance);
        record.transCode = 0; // re initialize condition
        GetData(record.transCode, record.transAmt);
    }

        out_stream.open("checkout.txt");
    

system("pause");
}    


/*    void DisplayTitle()
    {
        //cout << "\n       Check Register\n\n";
    }*/

        double GetBegBal()
    {
        double x;
        ifstream ChkBal;
        ChkBal.open("e:Checkin.dat");
            ChkBal >> x;
        return x;
    }


    void DisplayBal(double x)
    {
//        in_stream << "\t\tBalance = $" << setw(10) << x; cannot << to in_stream
        cout << "\t\tBalance = $" << setw(10) << x;
    }

    void GetData(int& x, double& y)
    {
        
        cout << "\n\n  Enter transaction code (0 to exit) ";
        in_stream >> x;
        if(next > 0)
        {
        //    cout << "\n  Enter transaction amount ";
            in_stream >> next;
        }
    }

    double ProcessCheck(double bal, double amt)
    {
        //cout << "\n  Check =    " << setw(10) << amt;
        return (bal - amt);
    }

    double ProcessDeposit(double bal, double amt)
    {
        //cout << "\n  Deposit =  " << setw(10) << amt;
        return (bal + amt);
    }
    double ProcessATM(double bal, double amt)
    {
        //cout << "\n  ATM     =  " << setw(10) << amt;
        bal = bal - amt;
        DisplayBal(bal);
        bal = bal - ATMFEE;
        //cout << "\n  ATM Fee =  " << setw(10) << ATMFEE;
        return (bal);
    }
    double ProcessSvcChg(double bal)
    {
        //cout << "\n  Service chg =" << setw(8) << CHARGE;
        bal = bal - CHARGE;
        DisplayBal(bal);
        return (bal);
    }
     double ProcessAutopay(double bal, double amt)
    {
        //cout << "\n  Withdrawal =    " << setw(10) << amt;
        return (bal - amt);
    }

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 12:00AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month