4 Replies - 2576 Views - Last Post: 31 July 2010 - 11:34 PM Rate Topic: -----

#1 pkjm17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 31-July 10

How to use cin.eof()?

Posted 31 July 2010 - 04:56 PM

I don't think the cin.eof() is working correctly in my program. I have it in my while loop, which goes through a switch-case structure. Basically, when the user enters the cin.eof() (Control-z) command, I want the program to end and display 4 totals (see in my code). However, I don't think it's working properly because the totals aren't correct. For example, if I enter two deposits of $10, Total Deposits will say $30 instead of $20. Here is my whole program.

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

class Transaction
{
private:
	double balance, depositTotal, checkTotal, ATMtotal, FeeTotal;

public:

	Transaction() {balance = 0; depositTotal = 0;
	checkTotal = 0; ATMtotal = 0; FeeTotal = 0;} // default constructor, initial balance set to $0

    void makeDeposit (double amount)
	{balance += (amount - 0.25);
	depositTotal += amount;}

	double getBalance () const
	{return balance;}

	void OverDraftFee (double amt)
	{balance -= (amt + 35.00);
	ATMtotal += amt;}

	bool withdrawCheck (double a)
	{if (balance >= a)
	        {
	        balance -= (a + 0.25);
	        checkTotal += a;
	        return true;
	        }
        else
        {
            balance -= (a + 35.25);
            checkTotal += a;
            return false;}
	}

	bool withdrawATM (double amt)
	{if (balance >= amt)
	        {
	        balance -= amt;
	        ATMtotal += amt;
	        return true;
	        }
        else
            return false;}

    void putFeeTotal(double f)
    {FeeTotal += f;}

    double getDepositTotal() {return depositTotal;}

    double getCheckTotal() {return checkTotal;}

    double getATMtotal() {return ATMtotal;}

    double getFeeTotal() {return FeeTotal;}

};
void showMenu();

int main()
{
double dollars, check, atm;
int num;
char choice;
char* ErrorMsg1 = "Deposit or withdrawal amount cannot be negative";
char* OverDraft = "There will be a $35.00 Overdraft Fee if you continue with the transaction. Do you want to continue. Y or N? ";
Transaction CheckBook; // object CheckBook, default constructor, initial balance is $0
while(!cin.eof() && cin.good())
{
showMenu();
cin>>num;

switch(num)
{
    case 1: cout<<"Enter the amount of the deposit: ";
            cin>>dollars;
            if(dollars < 0)
            cout<<ErrorMsg1 <<endl;
            else
            {
            CheckBook.makeDeposit(dollars);
            CheckBook.putFeeTotal(0.25); // keep track of deposit fees
            cout<<setprecision(2) <<fixed <<showpoint;
            cout<<"\nCurrent balance is $" <<CheckBook.getBalance() <<endl;
            }
            break;


    case 2:
            cout<<"Enter the amount of the check withdrawal: ";
            cin>>check;
            if(check < 0)
            {
            cout<<ErrorMsg1 <<endl;
            break;
            }
            else
            if(CheckBook.withdrawCheck(check))
            {
            CheckBook.putFeeTotal(0.25); // keep track of sum of check withdrawal fees
            cout<<setprecision(2) <<fixed <<showpoint;
            cout<<"\nCurrent balance is $" <<CheckBook.getBalance()<<endl;
            break;
            }
            else
            {
            CheckBook.putFeeTotal(35.00); // keep track of sum ot the $35 overdraft fee
            cout<<setprecision(2) <<fixed <<showpoint;
            cout<<"\nCurrent balance is $" <<CheckBook.getBalance()<<endl;
            }
            break;


    case 3:
            cout<<"Enter the amount of the ATM withdrawal: ";
            cin>>atm;
            if(atm < 0)
            {
            cout<<ErrorMsg1 <<endl;
            break;
            }
            else
            if(CheckBook.withdrawATM(atm))
            {
            cout<<setprecision(2) <<fixed <<showpoint;
            cout<<"\nCurrent balance is $" <<CheckBook.getBalance()<<endl;
            break;
            }
            else
            cout<<OverDraft;
            cin>>choice;
            if(choice == 'Y' || choice == 'y')
            {
            CheckBook.OverDraftFee(atm);
            CheckBook.putFeeTotal(35.00);
            cout<<setprecision(2) <<fixed <<showpoint;
            cout<<"\nCurrent balance is $" <<CheckBook.getBalance()<<endl;
            break;
            }
            else
            break;

    default: cout<<"Invalid selection. Enter an option 1-3, or Control Z to quit.\n";
}
};
if(cin.eof())
{
cout<<"\nTotal Desposits = $" <<CheckBook.getDepositTotal() <<endl;
cout<<"Total Check Withdrawals = $" <<CheckBook.getCheckTotal() <<endl;
cout<<"Total ATM Withdrawals = $" <<CheckBook.getATMtotal() <<endl;
cout<<"Total Fees = $" <<CheckBook.getFeeTotal() <<endl <<endl;
cout<<"Thank you for using the check book program\n";
}
//system("pause");
return 0;
}

//function definition
void showMenu()
{
    cout<<"\n------CHECK BOOK";
    cout<<"-----------\n";
    cout<<"1) Make a Deposit\n";
    cout<<"2) Make a Withdrawal by Check\n";
    cout<<"3) Make a Withdrawal by ATM Card\n";
    cout<<"Enter your choice: ";
}

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: How to use cin.eof()?

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: How to use cin.eof()?

Posted 31 July 2010 - 05:40 PM

On line 75 of code: cin>>num;

This is an I/O operation, yes? Can it fail? For example, if someone entered ctrl-z, what should happen?
Was This Post Helpful? 0
  • +
  • -

#3 pkjm17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 31-July 10

Re: How to use cin.eof()?

Posted 31 July 2010 - 06:51 PM

View PostOler1s, on 31 July 2010 - 04:40 PM, said:

On line 75 of code: cin>>num;

This is an I/O operation, yes? Can it fail? For example, if someone entered ctrl-z, what should happen?


To quit the input of transactions (i.e quit the program), user should enter ctrl-z. When that happens, the program should display the 4 totals of the transactions.
Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: How to use cin.eof()?

Posted 31 July 2010 - 08:37 PM

You missed the issue I was bringing up. On line 75, you have an I/O operation. If this fails (EOF, or some other reason), it stands to reason that 1) you should detect this and 2) you shouldn't make use of num, because I/O failed...

Yet, on line 77, you have code that relies on the value of num. That's interesting. Can you tell me how you ensured I/O didn't fail between line 75 and 77?
Was This Post Helpful? 0
  • +
  • -

#5 pkjm17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 31-July 10

Re: How to use cin.eof()?

Posted 31 July 2010 - 11:34 PM

I figured it out and solved my problem. What I did was I put an IF statment after the cin>>num input.

while(!cin.eof())
{
showMenu();
cin>>num;
if(!cin.eof())
{
switch(num)


And then have the else statement at the end of main.
else
{
cout<<"\n\nTotal Desposits = $" <<CheckBook.getDepositTotal() <<endl;
cout<<"Total Check Withdrawals = $" <<CheckBook.getCheckTotal() <<endl;
cout<<"Total ATM Withdrawals = $" <<CheckBook.getATMtotal() <<endl;
cout<<"Total Fees = $" <<CheckBook.getFeeTotal() <<endl <<endl;
cout<<"Thank you for using the check book program\n";
}


This works now and displays the totals correctly. What I did wrong was I didn't test to see if the number entered by the user was the control-z or not. And instead I had an IF statement at the bottom of main.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1