Banking
Oct 4, 2009 at 5:14am UTC
ok I am very new to C++. This is like the 2nd time I am writing a program. I need to write a simple banking program that you can deposit withdraw and see your balance and stuff.Also there is a data file called bank.dat and I need to use it to get account numbers and balances.Am I on the right track?And if not can you help me pls?And why does it exits the program when i type the transaction code?Thanks for the help
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
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main ()
{
ifstream fin;
int transaction_code,
account_number,
total_withdrawals,
total_deposits,
total_balance;
fin.open("Bank.dat" )
cout << setprecision(2)
<< setiosflags(ios::fixed)
<< setiosflags(ios::showpoint);
total_balance = total_deposits - total_withdrawals;
; cout << "Enter your account number " ;
cin >> account_number;
cout << "Your balance is " ;
cin<<total_balance;
cout << endl;
cout << "Enter the transaction code according to the following."
<< endl << endl;
cout << "Deposit, enter D" << endl;
cout << "Withdrawal, enter W" << endl;
cout << "Please make your selection: " ;
cin.get();
transaction_code = cin.get();
switch (transaction_code)
{
case 'D' :
case 'd' :
total_deposits;
break ;
case 'W' :
case 'w' :
total_withdrawals;
break ;
default :
cout << endl << endl
<<"Invalid Transaction Code! Try Again." << endl;
break ;
}
cout << endl << endl;
cout << "The balance is " << total_balance << endl;
cout << "Total Deposits are: " << total_deposits << endl;
cout << endl;
cout << "Total Withdrawals are: " << total_withdrawals << endl;
cout << endl;
return 0;
}
Oct 4, 2009 at 7:26am UTC
Do you want to read data from bank.dat file? Is it a text file or binary?
In your 21th line you didn't put any value to the variables.
In your 26th line the << operator is used incorrectly. The cin can be used for only reading by >> operator.
In your switch statement the name of the variables (for example: in 42th line) aren't instructions and don't do anything.
Oct 4, 2009 at 1:18pm UTC
If you want to do file operations then
Line 15 you are opening a file but not doing anything with that, so do something like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
fin.open("Bank.dat" );
// check whether it is open
if (!fin.is_open())
return 1; // not opened so we can't proceed further
// it is opened so what do you want to do with it
// read the buffer and store it in your variables
while (fin.good())
{
fin >> account_number;
fin >> balance;
// make sure yourself(or debug)
cout << account_number << ":" << balance << endl;
}
// then rest of your code
Oct 4, 2009 at 9:12pm UTC
screw I dont know wat do you mean by I didn't put any variables and are you saying that i should delete line 42
And I know I still have alot of errors can you guys help me without telling me the code
thanks for both your help
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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main ()
{
ifstream fin;
int transaction_code,
account_number,
total_withdrawals,
total_deposits,
total_balance;
fin.open("Bank.dat" )
if (!fin.is_open())
return 1;
while (fin.good())
{
fin >> account_number;
fin >> balance;
// make sure yourself(or debug)
cout << account_number << ":" << balance << endl;
}
cout << setprecision(2)
<< setiosflags(ios::fixed)
<< setiosflags(ios::showpoint);
total_balance = total_deposits - total_withdrawals;
cout << "Enter your account number " ;
cin >> account_number;
cout << "Your balance is " ;
cin >>total_balance;
cout << endl;
cout << "Enter the transaction code according to the following."
<< endl << endl;
cout << "Deposit, enter D" << endl;
cout << "Withdrawal, enter W" << endl;
cout << "Please make your selection: " ;
cin.get();
transaction_code = cin.get();
switch (transaction_code)
{
case 'D' :
case 'd' :
total_deposits;
break ;
case 'W' :
case 'w' :
total_withdrawals;
break ;
default :
cout << endl << endl
<<"Invalid Transaction Code! Try Again." << endl;
break ;
}
cout << endl << endl;
cout << "The balance is " << total_balance << endl;
cout << "Total Deposits are: " << total_deposits << endl;
cout << endl;
cout << "Total Withdrawals are: " << total_withdrawals << endl;
cout << endl;
return 0;
}
Oct 5, 2009 at 9:21am UTC
1 2 3 4 5 6 7 8
while (fin.good())
{
fin >> account_number;
fin >> balance;
// make sure yourself(or debug)
cout << account_number << ":" << balance << endl;
}
This code fragment you read data but only last two data (account_number and balance) is preserved, the previous data isn't stored. So the previous balanced were droped.
total_balance = total_deposits - total_withdrawals;
You didn't put any initialization to total_deposit and total_withdrawals;
Before you want to use a variable you should put a value. At least zero value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
transaction_code = cin.get();
switch (transaction_code)
{
case 'D' :
case 'd' :
total_deposits;
break ;
case 'W' :
case 'w' :
total_withdrawals;
break ;
default :
cout << endl << endl
<<"Invalid Transaction Code! Try Again." << endl;
break ;
}
Here the total_deposits is only a variable. It does nothing. Like this:
1 2 3
int apple = 10;
apple; // It isn't instruction.
Sorry, I have to work. Later.
Oct 5, 2009 at 3:19pm UTC
ok thank you I got this far but it is still giving me errors
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
#include <fstream>
#include <iostream>
int main()
{
ifstream fin;
char hold[255];
int ACCOUNT_NUMBER, CODE;
float AMOUNT, BALANCE;
int deposits_count = 0;
float deposits_total = 0.0;
int succesful_withdrawals = 0;
float total_succ_withdrawals = 0.0;
fin.open("Bank.dat" , ios::in, filebuf::openprot );
if (!fin) {
cout << "Error opening stream." << endl;
abort();
}
while ( fin.getline( hold, 256) ) {
// cout << hold << endl;
sscanf(hold, "%d %d %f %f" , &ACCOUNT_NUMBER, &CODE, &AMOUNT, &BALANCE);
// cout << ACCOUNT_NUMBER << CODE << AMOUNT << BALANCE << endl;
switch (CODE) {
case 1 : BALANCE += AMOUNT;
cout << ACCOUNT_NUMBER << " " << BALANCE << endl;
deposits_count++;
deposits_total += AMOUNT;
break ;
case 2 : if (BALANCE < AMOUNT)
cout << ACCOUNT_NUMBER << " Insufficient funds" << endl;
else {
BALANCE -= AMOUNT;
succesful_withdrawals++;
total_succ_withdrawals += AMOUNT;
cout << ACCOUNT_NUMBER;
if (BALANCE < 100.0) {
BALANCE -= 10.0;
cout << " " << BALANCE << " balance below minimum --$10.00 fee assessed" << endl;
}
else
cout << " " << BALANCE << endl;
}
break ;
default : cout << ACCOUNT_NUMBER << " bad transaction code" << endl;
break ;
}
}
cout << "Total Number of Deposits Made : " << deposits_count << endl;
cout << "Total Amount of Deposits Made : " << deposits_total << endl;
cout << "Total Number of Successful Witdrawals Made : " << succesful_withdrawals << endl;
cout << "Total Amount of Successful Witdrawals Made : " << total_succ_withdrawals << endl;
return 0;
Oct 5, 2009 at 3:45pm UTC
What are the errors?
Oct 5, 2009 at 5:12pm UTC
I could repeat ZHuge question: what/where are the errors?
I don't like standard C commands like
sscanf(hold, "%d %d %f %f" , &ACCOUNT_NUMBER, &CODE, &AMOUNT, &BALANCE);
to mix with C++.
I would use the
1 2 3 4
fin >> ACCOUNT_NUMBER;
fin >> CODE;
fin >> AMOUNT;
fin >> BALANCE;
Your code is better.
Oct 5, 2009 at 5:14pm UTC
it says fin,ifstream,filebuf,in,cerr,enld are undeclared
Oct 5, 2009 at 8:50pm UTC
1 2 3 4
#include <fstream>
#include <iostream>
using namespace std;
Oct 5, 2009 at 9:10pm UTC
OMG im so stupid how did i forget that thank you screw
and screw when i change sscanf to fin it tells me that its overloaded
I just have one more question after i run the program it gives me debugging error
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
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream fin;
char hold[255];
int ACCOUNT_NUMBER, CODE;
float AMOUNT, BALANCE;
int deposits_count = 0;
float deposits_total = 0.0;
int succesful_withdrawals = 0;
float total_succ_withdrawals = 0.0;
fin.open("G:\Cplusplus\Bank.dat" );
if (!fin) {
cout << "Error opening stream." << endl;
abort();
}
while ( fin.getline( hold, 256) ) {
// cout << hold << endl;
sscanf(hold, "%d %d %f %f" , &ACCOUNT_NUMBER, &CODE, &AMOUNT, &BALANCE);
// cout << ACCOUNT_NUMBER << CODE << AMOUNT << BALANCE << endl;
switch (CODE) {
case 1 : BALANCE += AMOUNT;
cout << ACCOUNT_NUMBER << " " << BALANCE << endl;
deposits_count++;
deposits_total += AMOUNT;
break ;
case 2 : if (BALANCE < AMOUNT)
cout << ACCOUNT_NUMBER << " Insufficient funds" << endl;
else {
BALANCE -= AMOUNT;
succesful_withdrawals++;
total_succ_withdrawals += AMOUNT;
cout << ACCOUNT_NUMBER;
if (BALANCE < 100.0) {
BALANCE -= 10.0;
cout << " " << BALANCE << " balance below minimum --$10.00 fee assessed" << endl;
}
else
cout << " " << BALANCE << endl;
}
break ;
default : cout << ACCOUNT_NUMBER << " bad transaction code" << endl;
break ;
}
}
cout << "Total Number of Deposits Made : " << deposits_count << endl;
cout << "Total Amount of Deposits Made : " << deposits_total << endl;
cout << "Total Number of Successful Witdrawals Made : " << succesful_withdrawals << endl;
cout << "Total Amount of Successful Witdrawals Made : " << total_succ_withdrawals << endl;
return 0;
}
Oct 5, 2009 at 11:38pm UTC
Assuming that in your file
one line looks like this
1234 1 200 100
Read what screw said. chage your code to
1 2 3 4 5 6 7 8 9 10 11 12 13 14
while (fin.good() /*fin.getline( hold, 256)*/ ) {
// cout << hold << endl;
//sscanf(hold, "%d %d %f %f", &ACCOUNT_NUMBER, &CODE, &AMOUNT, &BALANCE);
fin >> ACCOUNT_NUMBER;
fin >> CODE;
fin >> AMOUNT;
fin >> BALANCE;
cout << ACCOUNT_NUMBER << CODE << AMOUNT << BALANCE << endl;
// rest of your code
Oct 6, 2009 at 4:53am UTC
ok i got it working thanks to everyone
Topic archived. No new replies allowed.