0
votes

Problem statement: C++ program that reads a customer’s checking account information calculates his/her account balance. menu based application should be developed to perform the following functionalities iteratively until the user request to quit the program: 1. Display, 2. Deposit, 3. Withdraw, 4. Quit.

This is what I have so far.

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

//Main function
int main()
{ 
    //Identify type variables
    int x=0;
    float amountinput,famount,sum;
    string firstname, lastname,date,filename,input,ID,amount;
    fstream file;
//Program loop
    while(true)
    {
        //Main menu loop
        do
        {

        cout<<"Enter the number of the option you would like carried out.\n";
        cout<<"1. Summary\n";
        cout<<"2. Deposit\n";
        cout<<"3. Withdraw\n";
        cout<<"4. Quit\n";
        cin>>x;
    }
    while(!x);

    //When Quit is input, break
    if(x==4)
    {
        break;
    }
        //Summary display
        if (x==1)
        {
            cout<<"You have selected option number 1. Summary.\n"<<endl;
            cout<<"Enter your Cust_ID: "; 
            cin>>ID;
            file.open("C:\\Users\\Raggulddon\\Desktop\\C++ supplement
                       \\Cust_"+ID+".dat", ios::in|ios::out|ios::app);

            //IF not found error.
            if(!file)
            {
                cout<<"Sorry your account could not be found\n";
            }
            //Else display all content.
            else
            {
                    cout<<endl<<file.rdbuf()<<"\n";
                    file.close();

            }           
        }
        //Deposit
        else if(x==2)
        {
            cout<<"You have selected option number 2. Deposit.\n";
            cout<<"Please enter you account ID: ";
            cin>>ID;
            file.open("C:\\Users\\Raggulddon\\Desktop\\C++ supplement
                        \\Cust_"+ID+".dat", ios::in|ios::out|ios::app);

            if(!file)
            {
                cout<<"Sorry the requested account could not be located.\n";
            }
            else
            {
            file>>firstname>>lastname;
            cout<<endl<<firstname<<" "<<lastname<<endl; 
                while(!file.eof())
                {
                    file>>date>>amount;
                    //
 %%%%%%%%%%%%%%%%%%%//This is mainly where I am missing the lines of code..
                    //                      
                    float atof(string& amount);
                    cout<<date<<"\t\t"<<amount<<endl;
                }


                    cin.get();cin.get();
                cout<<"How much would you like to deposit today.";
                cin>>amountinput;

                cout<<endl;
                file.close();

            }
        }

            else if(x==3);
            else if(x==4);


}       
cin.get();cin.get(); //or system("PAUSE");
return 0;
}

A sample text file looks like this but has different number of lines.

James Bond
01/01/12 200010
03/30/12 -40000
04/30/12 -40000
05/30/12 -40000
06/30/12 -40000
07/30/12 -40000

I have set amount = string, I then want each string to be individual from each other (so that I may add them) and have them converted to float or double, whichever.

Basically, something like:

fstream file;
do
file>>date>>amount++; //I know this doesn't work, what will..
while(!file.eof());
float deposit;
finalAmount= deposit+amount;
1

1 Answers

1
votes

The nice thing about the C++ input operator >> is that it can take different types of arguments, like e.g. a double:

std::string date;
double amount;

file >> date >> amount;

However, I rather recommend using std::getline and std::istringstream to parse it:

std::string line;
std::getline(file, line);

std::istringstream iss(line)
iss >> date >> amount;

On a somewhat related note, don't use while (!file.eof()). The eof flag will not be set until an actual input operation fails, so unless you check for errors inside the loop you will attempt to read once to many.

Instead do e.g.

while (std::getline(file, line))
{
    ...
}