I am building an ATM sort of program where you can deposit and withdraw balance with the use of functions. I noticed at first that after i deposit first time, it works fine but after the second time using deposit it doesn't add it to the current balance but the balance before it.
For example:
(1st Attempt)
*Balance = 1000
*I deposit 500
*Balance is now = 1500
(2nd Attempt)
*I deposit 700
*Balance is now 1700
Instead of making it to 2200, it resets back to 1000 before went for my second attempt resulting in a result of 1700.
Can anyone please explain what went wrong in the code? I am willing to not only get the correct code but to also learn how it was done.
This is for my training in c++.
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int deposit(int x, int y)
{
int newbal = x + y;
return newbal;
}
int withdraw(int x, int y)
{
int newbal = x - y;
return newbal;
}
int main()
{
int menu;
int selection;
double x = 1000, y;
int trans;
char user[20], pass[20], pktc[3];
string newuser, newpass;
do{
system ("CLS");
cout << "Welcome To Bank" << endl;
cout << "[1] Register" << endl;
cout << "[2] Login" << endl;
cout << "[3] Exit" << endl;
cout << "\n\nEnter command: " <<endl;
cin >> menu;
switch(menu)
{
//----------------------------------------------------------------CASE 1------------------------------------------------------------------------------//
//----------------------------------------------------------------REGISTER----------------------------------------------------------------------------//
case 1:
system ("CLS");
cout << "<-------REGISTER------->\n\n";
cout << "Enter Name: ";
cin >> user;
newuser = user;
cout << "Enter Password: ";
cin >> pass;
newpass = pass;
cout <<"REGISTERED SUCCESSFULLY!" << endl;
cout <<"\n\nPress Any key to contniue" << endl;
cin >> pktc;
system ("CLS");
break;
//------------------------------------------------------------END OF REGISTER--------------------------------------------------------------------------//
//----------------------------------------------------------------CASE 2------------------------------------------------------------------------------//
//-----------------------------------------------------------------LOGIN------------------------------------------------------------------------------//
case 2:
system ("CLS");
cout << "<-------LOGIN------->\n\n";
cout << "Enter Username: ";
cin >> newuser;
cout << "Enter Password: ";
cin >> newpass;
if(newuser != user || newpass != pass)
{
//-------------------------------------------------------------FAILED LOGIN----------------------------------------------------------------------------//
cout << "\nInvalid account" << endl;
cout <<"\n\nPress Any key to contniue" << endl;
cin >> pktc;
system ("CLS");
}
else if (newuser == user || newpass == pass)
{
//----------------------------------------------------------------CASE 2.1------------------------------------------------------------------------------//
//------------------------------------------------------------SUCCESFULL LOGIN--------------------------------------------------------------------------//
system ("CLS");
cout << "\n\n<-------------------------------WELCOME------------------------------->\n\n" << endl;
cout<<"\nWelcome To Banco De Nelio";
cout<<"\nUSERNAME: "<< user << endl;
cout<<"\n\n TIP ATM MACHINE";
cout<<"\n 1. Balance";
cout<<"\n 2. Deposit";
cout<<"\n 3. Withdraw";
cout<<"\n 4. Exit";
do{
cout<<"\n\nChoose Transaction[1-3]:";
cin>>trans;
switch(trans)
{
//----------------------------------------------------------------ATM CASE 1------------------------------------------------------------------------------//
//--------------------------------------------------------------CHECK BALANCE--------------------------------------------------------------------------//
case 1:
system ("CLS");
cout << "\n\n<-------------------------------WELCOME------------------------------->\n\n" << endl;
cout<<"\nWelcome To Banco De Nelio";
cout<<"\nUSERNAME: "<< user << endl;
cout<<"\n\n TIP ATM MACHINE";
cout<<"\n 1. Balance";
cout<<"\n 2. Deposit";
cout<<"\n 3. Withdraw";
cout<<"\n 4. Exit";
cout<<"\n\nYour total balance is: "<< deposit(x, y) ;
break;
//----------------------------------------------------------------ATM CASE 2------------------------------------------------------------------------------//
//--------------------------------------------------------------BEFORE DEPOSIT--------------------------------------------------------------------------//
case 2:
system ("CLS");
cout << "\n\n<-------------------------------WELCOME------------------------------->\n\n" << endl;
cout<<"\nWelcome To Banco De Nelio";
cout<<"\nUSERNAME: "<< user << endl;
cout<<"\n\n TIP ATM MACHINE";
cout<<"\n 1. Balance";
cout<<"\n 2. Deposit";
cout<<"\n 3. Withdraw";
cout<<"\n 4. Exit";
cout<<"\n\nEnter the amount:" ;
cin>>y;
//--------------------------------------------------------------AFTER DEPOSIT--------------------------------------------------------------------------//
system ("CLS");
cout << "\n\n<-------------------------------WELCOME------------------------------->\n\n" << endl;
cout<<"\nWelcome To Banco De Nelio";
cout<<"\nUSERNAME: "<< user << endl;
cout<<"\n\n TIP ATM MACHINE";
cout<<"\n 1. Balance";
cout<<"\n 2. Deposit";
cout<<"\n 3. Withdraw";
cout<<"\n 4. Exit";
cout<<"\n\nYour total balance now is: " << deposit(x, y) <<endl;
break;
//----------------------------------------------------------------ATM CASE 3------------------------------------------------------------------------------//
//--------------------------------------------------------------WITHDRAW BALANCE--------------------------------------------------------------------------//
case 3:
cout<<"\nEnter the amount:" ;
cin>>y;
if ( y > x)
{
cout<<"\nYou cannot withdraw " << y;
cout<<" because the amount is higher than your balance" << endl;
break;
}
else
x = x - y;
cout<<"\nYour Total Balance is now " << withdraw(x, y) << endl;
break;
//----------------------------------------------------------------ATM CASE 4------------------------------------------------------------------------------//
//-------------------------------------------------------------------BACK--------------------------------------------------------------------------------//
case 4:
cout<<"\n\nThank You!" << endl;
break;
default:
cout<<"\nYou did not enter any valid number" << endl;
break;
}
}while (trans<=3);
}
break;
//----------------------------------------------------------------CASE 3------------------------------------------------------------------------------//
//-----------------------------------------------------------------EXIT-------------------------------------------------------------------------------//
case 3:
system ("CLS");
cout << "Thank you for using me!\n";
return 0;
//-------------------------------------------------------------END OF EXIT----------------------------------------------------------------------------//
}
}while (menu<=3);
}
i am not sure if the problem here is the function or a conflict in switch statement.
Thanks In Advance
EDIT Please register first :)
Its really hard to spot the important pieces, but basically your problem can be boiled down to something like
int add(int x,int y) { return a+b; }
int sub(int x,int y) { return a-b; }
int main() {
int initial = 0;
// add 10 then subtract 5
std::cout << add(initial,10) << '\n'; // prints 10
std::cout << sub(initial,5) << '\n'; // prints -5
}
When what you actually want is something like
int main() {
int initial = 0;
// add 10 then subtract 5 and update initial
initial = add(initial,10);
std::cout << initial << '\n';
initial = sub(initial,5);
std::cout << initial << '\n';
}
Your methods correctly calculates the new amount, but when you call those methods you ignore the return value when instead you want to update some variable.
Some other suggestions (in random order):
use meaningful names. x and y do not convey any meaning, compare int deposit(int current_balance, int amount) to int deposit(int x,int y)
use functions to split your code into smaller pieces (eg it would be a good idea to seperate input, output and logic). Whenever you find yourself writing a comment describing what a block of code is doing, this block of code is a good candidate for a function that gets a proper name instead of a comment
dont use std::endl when you want to end a line (std::endl ends the line and flushes the stream, which is most of the time not what you want), use \n instead
dont use using namespace std;. It wont do much harm in your current code, but read here why you never should do it in a header, and better dont get used to bad habits from the start.
Related
I was tasked to create an ATM mock program and my problem is overwriting the money and PIN variables with the information that the user will enter.
Here it is:
#include <iostream>
using namespace std;
void Check(int money) { cout << "Your current balance is: " << money << endl; }
void Deposit(int money) {
int deposit;
cout << "Please enter the amount of cash you wish to deposit.\n";
cin >> deposit;
money += deposit;
cout << "Your new balance is: " << money << endl;
}
void Withdraw(int money) {
int withdraw;
cout << "Please enter the amount of cash you wish to withdraw.\n";
cin >> withdraw;
money -= withdraw;
cout << "Your new balance is: " << money << endl;
}
void Interest(int money) {
money += money * 0.05;
cout << "Your money with interest is: " << money << endl;
}
void Penalty(int money) {
if (money < 5000) {
money -= money * 0.02;
cout << "Your penalty is: " << money << endl;
} else
cout << "Your account will not incur a penalty because you are above the "
"minimum threshold.\n";
}
void ChangePIN(int PIN) {
int p;
cout << "Enter a new PIN: ";
cin >> p;
PIN = p;
cout << "Your new PIN is: " << PIN << endl;
}
int main() {
int money = 5000, PIN = 1234, EPIN;
cout << "Enter your PIN (Default PIN is 1234): \n";
cin >> EPIN;
if (EPIN == PIN) {
int choice;
cout << "Welcome!\n"
<< "1 - Check available balance \n"
<< "2 - Deposit cash \n"
<< "3 - Withdraw cash \n"
<< "4 - Compute for the interest of your account(5%)\n"
<< "5 - Compute for the penalty of having a balance below 5000 (2%) \n"
<< "6 - Change your PIN\n"
<< "7 - Exit\n"
<< "Your choice: ";
cin >> choice;
switch (choice) {
case 7: {
break;
}
{
case 1: {
Check(money);
break;
}
case 2: {
Deposit(money);
break;
}
case 3: {
Withdraw(money);
break;
}
case 4: {
Interest(money);
break;
}
case 5: {
Penalty(money);
break;
}
case 6: {
ChangePIN(PIN);
break;
}
}
}
return 0;
}
}
As you can see I'm pretty much a beginner at this. My problem is the money and PIN have the default values of 5000 and 1234 respectively. Now, I need to make the user be able to change these values but once I use return main() they get assigned the same starting values again, What would be the best workaround for this? I thought of using some sort of accumulator for this but I'd like some advice first.
You can simply do this by using a while loop.
Run an infinite while loop and break it whenever you want to exit from the program.
Here is the code:
#include <iostream>
using namespace std;
void Check(int money) { cout << "Your current balance is: " << money << endl; }
void Deposit(int money) {
int deposit;
cout << "Please enter the amount of cash you wish to deposit.\n";
cin >> deposit;
money += deposit;
cout << "Your new balance is: " << money << endl;
}
void Withdraw(int money) {
int withdraw;
cout << "Please enter the amount of cash you wish to withdraw.\n";
cin >> withdraw;
money -= withdraw;
cout << "Your new balance is: " << money << endl;
}
void Interest(int money) {
money += money * 0.05;
cout << "Your money with interest is: " << money << endl;
}
void Penalty(int money) {
if (money < 5000) {
money -= money * 0.02;
cout << "Your penalty is: " << money << endl;
} else
cout << "Your account will not incur a penalty because you are above the "
"minimum threshold.\n";
}
int ChangePIN(int PIN) {
int p;
cout << "Enter a new PIN: ";
cin >> p;
cout << "Your new PIN is: " << PIN << endl;
return p;
}
int main() {
int money = 5000, PIN = 1234;
while(1){ // run an infinite loop
int EPIN;
cout << "Enter your PIN (Default PIN is 1234): \n";
cin >> EPIN;
if (EPIN == PIN) {
int choice;
cout << "Welcome!\n"
<< "1 - Check available balance \n"
<< "2 - Deposit cash \n"
<< "3 - Withdraw cash \n"
<< "4 - Compute for the interest of your account(5%)\n"
<< "5 - Compute for the penalty of having a balance below 5000 (2%) \n"
<< "6 - Change your PIN\n"
<< "7 - Exit\n"
<< "Your choice: ";
cin >> choice;
switch (choice) {
case 7: {
return 0; // breaking condition
}
{
case 1: {
Check(money);
break;
}
case 2: {
Deposit(money);
break;
}
case 3: {
Withdraw(money);
break;
}
case 4: {
Interest(money);
break;
}
case 5: {
Penalty(money);
break;
}
case 6: {
PIN = ChangePIN(PIN);
break;
}
}
}
}
}
return 0;
}
Does this answer your question?
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am a first-year college student and I am currently having trouble with this program I am working on, It is about a billing system. I managed to do everything but the final part, which is printing the total, tax, and the final bill. Anything I try to do either gives me a wrong answer or a 0. Please help.
#include <iostream>
#include <string.h>
#include <iomanip>
#define MAX 8
using namespace std;
struct menuItemType{
string menuItem;
double menuPrice;
};
menuItemType menulist[MAX];
void getData();
void showMenu();
int printCheck(void);
//int b[8] = {0,0,0,0,0,0,0,0};
int main(){
cout << "Welcome to Mavel's Restaurant\n\n";
cout << "------------ Menu ------------ \n";
showMenu();
getData();
int choice;
char add;
do {
cout << "Enter choice: ";
cin >> choice;
switch (choice){
case 1:
cout<< "You ordered Plain Egg.\n";
break;
case 2:
cout<< "You ordered Bacon and Egg.\n";
break;
case 3:
cout<< "You ordered a muffin.\n";
break;
case 4:
cout<< "You ordered French Toast.\n";
break;
case 5:
cout<< "You ordered Fruit Basket.\n";
break;
case 6:
cout<< "You ordered Cereal.\n";
break;
case 7:
cout<< "You ordered Coffee.\n";
break;
case 8:
cout<< "You ordered Tea.\n";
break;
default:
cout<< "Invalid Choice.";
break;
}
cout<< "Would you like to order another item? [Y]es / [N]o : ";
cin >> add;
if (add == 'N'||add =='n'){
printCheck();
}
}
while (add == 'Y'|| add == 'y');
}
void getData(){
menulist[0].menuItem = "Plain Egg";
menulist[0].menuPrice = 140.50;
menulist[1].menuItem = "Bacon and Egg";
menulist[1].menuPrice = 245.00;
menulist[2].menuItem = "Muffin";
menulist[2].menuPrice= 295.00;
menulist[3].menuItem = "French Toast";
menulist[3].menuPrice = 495.00;
menulist[4].menuItem = "Fruit Basket";
menulist[4].menuPrice = 555.00;
menulist[5].menuItem = "Cereal";
menulist[5].menuPrice = 385.00;
menulist[6].menuItem = "Coffee";
menulist[6].menuPrice = 415.00;
menulist[7].menuItem = "Tea";
menulist[7].menuPrice = 333.00;
}
void showMenu(){
cout << "[1] Plain Egg\t\tPhp140.50\n";
cout << "[2] Bacon and Egg\tPhp245.00\n";
cout << "[3] Muffin\t\tPhp295.00\n";
cout << "[4] French Toast\tPhp495.00\n";
cout << "[5] Fruit Basket\tPhp555.00\n";
cout << "[6] Cereal\t\tPhp385.00\n";
cout << "[7] Coffee\t\tPhp415.00\n";
cout << "[8] Tea\t\t\tPhp333.00\n\n";
}
double total = 0;
int printCheck(){
getData();
double total = 0 , tax, totalbill;
for (int i = 0; i < 8; i++){
total += menulist[i].menuPrice;
}
tax = total * 0.05;
totalbill = total+tax;
cout << "----------------------------------------------\n";
cout << "Tax\t\t" << tax<< endl;
cout << "Amount Due\tPhp" << totalbill << endl;
cout << "----------------------------------------------\n";
return total;
}
Your code is a bit all over the place, with some small reorganization you can get the intended result, here is a possible fix, with comments where needed:
Live sample
#include <iostream>
#include <string.h>
#include <iomanip>
#define MAX 8
using namespace std; // I would avoid using namespace std, explanation ahead
struct menuItemType
{
string menuItem;
double menuPrice;
};
//menuItemType menulist[MAX]; //does not need to be global
void getData(menuItemType *menulist);
void showMenu();
int printCheck(double total); // pass the total to the printCheck() function
int main()
{
menuItemType menulist[MAX]; // go local when possible
cout << "Welcome to Mavel's Restaurant\n\n";
cout << "------------ Menu ------------ \n";
showMenu(); // here you could also pass menulist and print the menu using the data
//it would be easier to refactor if you add a menu item, try it
getData(menulist);
int choice;
char add;
double total = 0;
do
{
cout << "Enter choice: ";
cin >> choice;
// do the math in the cycle so you can keep track of the sum of selected items
if(choice > 0 && choice <= MAX)
total += menulist[choice - 1].menuPrice;
switch (choice) // if you want to separate logic from UI, this could
// be a separate function, i.e. printChoice(choice);
{
case 1:
cout << "You ordered Plain Egg.\n";
break;
case 2:
cout << "You ordered Bacon and Egg.\n";
break;
case 3:
cout << "You ordered a muffin.\n";
break;
case 4:
cout << "You ordered French Toast.\n";
break;
case 5:
cout << "You ordered Fruit Basket.\n";
break;
case 6:
cout << "You ordered Cereal.\n";
break;
case 7:
cout << "You ordered Coffee.\n";
break;
case 8:
cout << "You ordered Tea.\n";
break;
default:
cout << "Invalid Choice.";
break;
}
cout << "Would you like to order another item? [Y]es / [N]o : ";
cin >> add;
if (add == 'N' || add == 'n') // this condition is also refactorable, you could
// simply move the print to after the loop ends
{
printCheck(total);
}
} while (add == 'Y' || add == 'y');
//printCheck(total); //here
}
void getData(menuItemType *menulist)
{
menulist[0].menuItem = "Plain Egg";
menulist[0].menuPrice = 140.50;
menulist[1].menuItem = "Bacon and Egg";
menulist[1].menuPrice = 245.00;
menulist[2].menuItem = "Muffin";
menulist[2].menuPrice = 295.00;
menulist[3].menuItem = "French Toast";
menulist[3].menuPrice = 495.00;
menulist[4].menuItem = "Fruit Basket";
menulist[4].menuPrice = 555.00;
menulist[5].menuItem = "Cereal";
menulist[5].menuPrice = 385.00;
menulist[6].menuItem = "Coffee";
menulist[6].menuPrice = 415.00;
menulist[7].menuItem = "Tea";
menulist[7].menuPrice = 333.00;
}
void showMenu()
{
cout << "[1] Plain Egg\t\tPhp140.50\n";
cout << "[2] Bacon and Egg\tPhp245.00\n";
cout << "[3] Muffin\t\tPhp295.00\n";
cout << "[4] French Toast\tPhp495.00\n";
cout << "[5] Fruit Basket\tPhp555.00\n";
cout << "[6] Cereal\t\tPhp385.00\n";
cout << "[7] Coffee\t\tPhp415.00\n";
cout << "[8] Tea\t\t\tPhp333.00\n\n";
}
// now can simply print the check
int printCheck(double total)
{
double tax = total * 0.05;
double totalbill = total + tax;
cout << "----------------------------------------------\n";
cout << "Tax\t\t" << tax << endl;
cout << "Amount Due\tPhp" << totalbill << endl;
cout << "----------------------------------------------\n";
return total; // you're not using this return value, you may aswell make the function return type void
}
For more information whether you should use using namespace std; check this link:
Why is "using namespace std;" considered bad practice?
Note that the code still have some weaknesses, namely the lack of input validation, if, as an example, your input is not a number when you are prompted to give a choice you have a problem. If you want to learn more about this there are many threads in SO you can find, for instance:
Good input validation loop using cin - C++
I am doing a project for school and keep running into this reoccurring problem, my code does not seem to run as I have "undeclared identifiers." I have tried renaming them or redefining them but the same errors keep going or more. I am using VS code at the moment and read about maybe it was VScode itself but I get the same errors regardless.
#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
using namespace std;
class bankAccount {
public:
int accountNum;
string accountName;
string accountType;
double accountBalance;
double accountInterest;
double getInterest;
double updateBalance;
bankAccount(){
accountNum = 0;
accountName = "";
accountType = "";
accountBalance = 0.0;
accountInterest = 0.0;
}
void deposit()
{
long amount;
cout << "\n Please enter the amount you would like to deposit: ";
cin >> amount;
accountBalance = accountBalance + amount;
}
void withdraw()
{
long amount;
cout << "Please enter the amount you would like to withdraw: ";
cin >> amount;
if (amount <= accountBalance)
{
accountBalance = accountBalance - amount;
}
else
{
cout << "You do not have sufficient funds" << endl;
}
}
void interest(){
double getInterest;
cout << "Please enter desired interest amount: ";
cin >> getInterest;
}
void update(){
double updateBalance;
updateBalance = accountBalance * getInterest;
accountBalance += updateBalance;
}
void print(){
string print;
cout << "Welcome Back " << accountName << "," << endl;
cout << "\n Account Number: " << accountNum << endl;
cout << "\n Account Type: " << accountType << endl;
cout << "\n Current Balance: " << accountBalance << endl;
cout << "\n Account Interest: " << accountInterest << endl;
}
void openAccount(){
cout << "Enter Account Number: ";
cin >> accountNum;
cout << "Enter Account Holders Name: ";
cin >> accountName;
cout << "Enter Account Type: ";
cin >> accountType;
cout << "Enter Initial Balance: ";
cin >> accountBalance;
cout << "Enter Interest Rate: ";
cin >> accountInterest;
}
};
int main() {
int choice;
do
{
cout << "Please select the following options ";
cout << "\n 1:View Account";
cout << "\n 2: Open Account";
cout << "\n 3: Deposit" ;
cout << "\n 4: Withdraw ";
cout << "\n 5: Update account";
cin >> choice;
switch (choice)
{
case '1':
print ();
break;
case '2':
openAccount();
break;
case '3':
deposit();
break;
case '4':
withdraw();
break;
case '5':
updateBalance();
break;
}
} while (case !=5);
}
suggested creating an instance of class bankAccount somewhere before switch statement and call the functions like, instance_of_bankAccount.functionName();
At end of loop instead of (case !=5) it should be (choice != 5)
The problem with your code is that you do not have any instance of the class bankAccount, which means you try to call the function which actually does not exist. To fix it, you should create an object of the bankAccount type before the loop in which you can select the operation you want to perform:
int main() {
int choice;
bankAccount bankAcc; // this creates the object we need
do ...
And then for every case you wanted to call the function f(), add prefix bankAcc., so it becomes bankAcc.f(). In your code, it would be:
switch (choice) {
case '1':
bankAcc.print();
break;
case '2':
bankAcc.openAccount();
break;
case '3':
bankAcc.deposit();
break;
case '4':
bankAcc.withdraw();
break;
case '5':
bankAcc.updateBalance();
break;
}
Thanks to it, all functions called will know that they belong to bankAcc and they will change properties only of this particular object.
There is also another mistake in your code: did you really mean to use chars in your switch? At the moment cin >> choice reads an int, saves it in choice and then in switch it gets compared with all values corresponding to cases. The problem is that 1 is very different from '1' (which is casted to the value 49), so inputted int will never satisfy conditions from the menu you print.
Also, while (case !=5) should be changed to while (choice != 5).
The things that i would like to accomplish is the functions of methods of the applications but getting many errors from the code which i don't completely understand and try to solve but came up with nothing.
#include <conio.h>
#include <stdio.h>
#include <iostream>
using namespace std;
class bank
{
char name [100],add[100],y;
int balance, amount;
public:
void open_account();
void deposite_money();
void withdraw_money();
void display_account();
};
void bank:open_account()
{
cout << "Enter your full name: ";
cin.ignore();
cin.getline(name,100);
cout << "What type of account you want to open savings (s) or current (c)";
cin >> y;
cout << "Enter amount of deposite: ";
cin >> balance;
cout << "Your account is created ";
}
void bank:deposite_money()
{
int a ;
cout << "Enter how much money you want to deposit: ";
cin >> a;
balance += a;
cout << "Your total deposit amount\n ";
}
void bank:display_account()
{
cout << "Enter the name: "<<name<<endl;
cout << "Enter your address: "<<add<<endl;
cout << "Type of account that you open: "<<y<<endl;
cout << "Amount you deposite: "<<balance<<endl;
}
void bank:withdraw_money()
{
cout << "Withdraw: ";
cout << "Enter the amount of withdrawing";
cin >> amount;
balance = balance - amount;
cout << "Now your total amount left is" <<balance;
}
int main()
{
int ch,x,n;
bank obj;
do
{
cout <<"01")open account\n";
cout <<"02")deposite money\n";
cout <<"03")withdraw money\n";
cout <<"04")display account\n";
cout <<"05")Exit\n";
cout << "Please select from the options above";
cin>>ch;
switch(ch)
{
case 1:"01")open account\n";
obj.open_account();
break;
case 2:"02")deposite money\n";
obj.deposite_money();
break;
case 3:"03")withdraw money\n";
obj.withdraw_money();
break;
case 4:"04")display account\n";
obj.display_account();
break;
case 5:
if(ch == 5)
{
cout << "Exit";
}
default:
cout<< "This is not the proper exit please try again";
}
cout << "\ndo you want to select the next step then please press : y\n";
cout << " If you want to exit then press : N";
x=getch();
if(x == 'y' || x == 'Y');
cout<< "Exit";
}
while (x == 'y' || x == 'Y');
getch();
return 0;
}
The errors that i am getting are
error stray '\'
error missing terminating character
error found ':' in nested name specifier
expected ; before ')' token
These are the errors that usually appears on the logs and has repeated a few times in different lines please help me so that i can finish this application i have learned a lot from making it and is looking forward to build more before my school starts Any help and suggestions on what i should do next is appreciated and advices on what i should do next will greatly be welcome for me to learn from this experience
Im using c++ btw and am trying to build some projects any advice for next time? How can improve myself after this errors and what should i next practice on like a next project that you guys would suggest me
See here:
#include <stdio.h>
#include <iostream>
using namespace std;
class bank
{
char name [100],add[100],y;
int balance, amount;
public:
void open_account();
void deposite_money();
void withdraw_money();
void display_account();
};
void bank::open_account()//: missing
{
cout << "Enter your full name: ";
cin.ignore();
cin.getline(name,100);
cout << "What type of account you want to open savings (s) or current (c)";
cin >> y;
cout << "Enter amount of deposite: ";
cin >> balance;
cout << "Your account is created ";
}
void bank::deposite_money()//: missing
{
int a ;
cout << "Enter how much money you want to deposit: ";
cin >> a;
balance += a;
cout << "Your total deposit amount\n ";
}
void bank::display_account()//: missing
{
cout << "Enter the name: "<<name<<endl;
cout << "Enter your address: "<<add<<endl;
cout << "Type of account that you open: "<<y<<endl;
cout << "Amount you deposite: "<<balance<<endl;
}
void bank::withdraw_money()//: missing
{
cout << "Withdraw: ";
cout << "Enter the amount of withdrawing";
cin >> amount;
balance = balance - amount;
cout << "Now your total amount left is" <<balance;
}
int main()
{
int ch,x,n;
bank obj;
do
{
cout <<"01)open account\n";//Delete " after Number
cout <<"02)deposite money\n";
cout <<"03)withdraw money\n";
cout <<"04)display account\n";
cout <<"05)Exit\n";
cout << "Please select from the options above";
cin>>ch;
switch(ch)
{
case 1:"01)open account\n";//Delete " after Number
obj.open_account();
break;
case 2:"02)deposite money\n";
obj.deposite_money();
break;
case 3:"03)withdraw money\n";
obj.withdraw_money();
break;
case 4:"04)display account\n";
obj.display_account();
break;
case 5:
if(ch == 5)
{
cout << "Exit";
}
default:
cout<< "This is not the proper exit please try again";
}
cout << "\ndo you want to select the next step then please press : y\n";
cout << " If you want to exit then press : N";
cin >> x;
if(x == 'y' || x == 'Y');
cout<< "Exit";
}
while (x == 'y' || x == 'Y');
}
Also don't use using namespace in headers, and in overall learn a better style. This is 80s C with C++ mixed in. Use containers not built in arrays, use iostream and overall get a good C++ book.
Hello I've been working on a C++ banking application that should be able to hold more than one account with all the related field's. I have come across a few issues:
When displaying account info in Display or ShowInfo functions, the first letter of the first name and the middle initial wont show up.
When creating an account only the most recent account is able to be searched for and displayed in display data. I know that an array is needed for this to be possible, I'm just not sure if implemented this correctly.
Thanks for the help. Any input is appreciated!
#include <stdlib.h>
#include <conio.h>
#include <iostream>
#include <string>
using namespace std;
class BankAccount{
double Balance = 0.0;
char ans;
public:struct Name{
char Last_Name[50];
char First_Name[50];
char Middle_Initial[5];
}Name;
public:struct Account{
char Type[1];
int Account_Number;
}Account;
public:
void CreateAccount();
void Withdraw();
void Deposit();
void Display();
void ShowInfo();
int Menu();
};
void BankAccount::CreateAccount(){
do
{
cout << "\nEnter account number: ";
cin >> Account.Account_Number;
cout << "\nEnter the last name for the account: ";
cin.ignore();
cin.getline(Name.Last_Name, 50);
cout << "\nEnter the first name for the account: ";
cin.ignore();
cin.getline(Name.First_Name, 50);
cout << "\nEnter the Middle initial for the account: ";
cin.ignore();
cin.getline(Name.Middle_Initial, 5);
cout << "\nEnter the type of account (C/S) : ";
cin >> Account.Type;
cout << "\nEnter the initial balance of the account: ";
cin >> Balance;
cout << "\n\nAccount Created.";
cout << "\n\nCreate new account? (Y/N) : ";
cin >> ans;
while (ans != 'Y' && ans != 'N'){
cout << "Invalid input. Create new account? (Y/N) : ";
cin >> ans;
}
cout << endl;
} while (ans != 'N');
};
void BankAccount::Withdraw(){
int actNum;
double amount;
cout << "Enter the account number for the account that you wish to withdraw funds: ";
cin >> actNum;
if (actNum == Account.Account_Number){
cout << "Enter the amount you would like to withdraw: ";
cin >> amount;
Balance = Balance - amount;
}
else if (actNum != Account.Account_Number){
cout << "No account found under that number! Try again!";
}
}
void BankAccount::Deposit(){
int actNum;
double amount;
cout << "Enter the account number for the account that you wish to deposit funds: ";
cin >> actNum;
if (actNum == Account.Account_Number){
cout << "Enter the amount you would like to deposit: ";
cin >> amount;
Balance = Balance + amount;
}
else if (actNum != Account.Account_Number){
cout << "No account found under that number! Try again!";
}
}
void BankAccount::Display(){
int actNum;
cout << "Enter the account number for the account that you wish to display account information for: ";
cin >> actNum;
if (actNum == Account.Account_Number){
cout << "Account details for " << Name.First_Name << " " << Name.Middle_Initial << " " << Name.Last_Name << "'s account: " << endl;
cout << "Account Number: " << Account.Account_Number << endl;
cout << "Account Type (Checking / Savings): " << Account.Type << endl;
cout << "Account Balance: $" << Balance << endl;
}
else if (actNum != Account.Account_Number){
cout << "No account found under that number! Try again!";
}
}
void BankAccount::ShowInfo(){
cout << "Account details for " << Name.First_Name << " " << Name.Middle_Initial << " " << Name.Last_Name << "'s account: " << endl;
cout << "Account Number: " << Account.Account_Number << endl;
cout << "Account Type (Checking / Savings): " << Account.Type << endl;
cout << "Account Balance: $" << Balance << endl;
}
int main(int argc, char *argv){
BankAccount ob;
char ch;
cout << "Welcome to Console Banking Application V 1.0!";
cout << "\nSelect an item from the list below by entering the corresponding letter.";
do{
cout << "\n\n A. Create Account \n B. Withdraw \n C. Deposit \n D. Show Account Details \n\n Q. Exit Application\n\n";
ch = ob.Menu();
switch (ch){
case 'A':
case 'a': ob.CreateAccount();
ob.ShowInfo();
break;
case 'B':
case 'b': ob.Withdraw();
break;
case 'C':
case 'c': ob.Deposit();
break;
case 'D':
case 'd': ob.Display();
break;
case 'Q':
case 'q': ob.ShowInfo();
exit(1);
break;
}
} while (1);
}
int BankAccount::Menu(){
char ch;
cout << "Select an option: ";
cin >> ch;
return ch;
}
The simple answer is: You only have one bank account.
If you look at your main function, you create one, and only one BankAccount. As you would probably guess, one BankAccount cannot be used to represent multiple BankAccounts(SoAs aside).
Therefore, you need an array of BankAccounts. It goes something like this:
BankAccount obs[10];
Now you have 10 BankAccounts, no more. So every time you want to create a new BankAccount, just make sure that you create it on a BankAccount in the array that is not currently in use.
obs[0].CreateAccount();
obs[0].ShowInfo();
obs[0].Deposit();
//Make a new account
obs[1].CreateAccount();
...
To go even further, let's consider the fact that you have 10 BankAccounts, and only 10. This is not very extensive now is it? What's a bank with only 10 accounts available? Probably out of business in due time.
The naive solution would be to just add more. 50, 100, even 1000. But do you really want to go back and update that number every single time? That's way too tedious.
Fortunately, there's a convenient container we can use:
#include <vector>
...
std::vector<BankAccount> obs;
...
This is basically an array that expands itself automatically when required. I will not go into detail on how to use vectors because I am sure that you can easily learn to do so yourself. I will, however, leave you with this link so you know where you can start.