How to use cin.eof()?
Aug 1, 2010 at 2:04am UTC
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
#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: " ;
}
Topic archived. No new replies allowed.