i'm doing this project Banking System
This system tracks customers’ accounts in a bank. Each account has a number, name, and balance. The system provides the following functionalities: create new account, withdraw, deposit, and close account.
The system has the following interface:
Choose:
1- Add new account
2- Withdraw
3- Deposit
4- Get Balance
5- Exit
When the user chooses 1, the system generates a new ID, and then asks the user to enter a name for that account. The initial balance is set to zero.
When the user chooses 2, the system asks the user to enter account ID and amount to be withdrawn. If this amount is greater than the balance, a message is displayed that this transaction failed because insufficient balance. If balance is enough, it decreases by amount to be withdrawn.
When the user chooses 3. The system asks the user to enter account ID and amount to be deposited. System increases balance by this amount.
When the user chooses 4, the system asks the user to enter account ID then prints account’s name and balance.
Each time a task is completed the system gets back to the main menu above until the user chooses 5.
# include <iostream>
#include <string>
using namespace std;
# include<iomanip>
class Bank
{
private:
char name;
int acno;
float balance;
public:
void newAccount();
void withdraw();
void deposit();
void getbalance();
void disp_det();
};
//member functions of bank class
void Bank::newAccount()
{
cout<<"New Account";
cout<<"Enter the Name of the depositor : ";
cin>>name;
cout<<"Enter the Account Number : ";
cin>>acno;
cout<<"Enter the Amount to Deposit : ";
cin >>balance;
}
void Bank::deposit()
{
float more;
cout <<"Depositing";
cout<<"Enter the amount to deposit : ";
cin>>more;
balance+=more;
}
void Bank::withdraw()
{
float amt;
cout<<"Withdrwal";
cout<<"Enter the amount to withdraw : ";
cin>>amt;
balance-=amt;
}
void Bank::disp_det()
{
cout<<"Account Details";
cout<<"Name of the depositor : "<<name<<endl;
cout<<"Account Number : "<<acno<<endl;
cout<<"Balance : $"<<balance<<endl;
}
// main function , exectution starts here
void main(void)
{
Bank obj;
int choice =1;
while (choice != 5 )
{
cout<<"Enter \n 1- to create new account \n 2- Withdraw\n 3- Deposit \n 4- get balance\n 5 Exit"<<endl;
cin>>choice;
switch(choice)
{
case '1' :obj.newAccount();
break;
case '2' :obj.withdraw();
break;
case 3: obj.deposit();
break;
case 4: getbalance();
break;
case 5:
break;
default: cout<<"Illegal Option"<<endl;
}
}
}
Problem 1:
You have made a typo in method which gets you the balance & the one which you are calling, rename Bank::disp_det() to Bank::getbalance()
void Bank::getbalance()
{
cout<<"Account Details";
cout<<"Name of the depositor : "<<name<<endl;
cout<<"Account Number : "<<acno<<endl;
cout<<"Balance : $"<<balance<<endl;
}
Problem 2:
You are not calling Bank::getbalance through an object of Bank, Since it is a member function you should call it as follows:
case 4:
obj.getbalance();
break;
In case 4, you should call obj.getbalance(). And it's not written yet: it seems you have written a disp_det() instead that shows the balance. Try renaming.
This isn't doing exactly what you want, as the case labels have different types:
switch(choice)
{
case '1' :obj.newAccount();
break;
case '2' :obj.withdraw();
break;
case 3: obj.deposit();
break;
case 4: getbalance();
break;
case 5:
break;
default: cout<<"Illegal Option"<<endl;
}
To select options '1' or '2', the user will have to type 31 and 32 when choice is an int.
Related
A basic student database using classes, I accept
name,
roll number, and
sgpa(cgpa equivalent but out of 10).
I used while loop, and switch case,but if the code for sgpa validation, and code for displaying all students with same sgpa is removed, the program works neatly, but if not, the program goes into continuous loop, the while loop inside the main, as it accepts the options itself and keeps doing it.
The code fails when i accept the sgpa(while taking the student data) or call the displaySGPA fucntion(option 3)
int totalStudents=0;
class database{
float sgpa;
int roll;
string name;
public:
void getSGPA();
void getData(database []);
friend void displaySGPA(int,database []);
};
//Display Students with same SGPA
void displaySGPA(int temp,database students[]){
int i,sameSGPA=0;
for(i=0;i<totalStudents;i++){
if(students[i].sgpa==temp){
sameSGPA+=1;
}
}
if(sameSGPA>1){
cout<<"\nStudents with SGPA "<<temp<<"."<<endl;
for(i=0;i<totalStudents;i++){
if(students[i].sgpa==temp){
cout<<" "<<students[i].roll<<" "<<students[i].name;
}
}
}
else if(sameSGPA==1){
cout<<"Only one student with the SGPA , "<<temp<<". :"<<endl;
for(i=0;i<totalStudents;i++){
if(students[i].sgpa==temp){
cout<<" "<<students[i].roll<<" "<<students[i].name;
break;
}
}
}
else{
cout<<"No Student with given SGPA."<<endl;
}
}
void database :: getData(database students []){
cout<<"\nEnter Name : ";
cin>>name;
cout<<"Enter Roll. ";
cin>>roll;
getSGPA();
}
//SGPA validation
void database :: getSGPA(){
int x=1,temp;
while(x==1){
cout<<"Enter SGPA : ";
cin>>temp;
if(temp<=10){
sgpa=temp;
break;
}
else{
cout<<"Please enter a valid SGPA.";
}
}
}
//main Loop
int main() {
int x,temp;
database students[50];
while (x!=5){
cout<<"\n1.Enter a New Student.\n3.Display students with same SGPA.\n5.Exit.\n\nYour Choice : ";
cin>>x;
switch(x){
case 1:
students[totalStudents].getData(students);
totalStudents++;
break;
case 3:
cout<<"\nEnter the SGPA.";
cin>>temp;
displaySGPA(temp,students);
break;
case 5:
cout<<"Exiting the program.....";
break;
default:
cout<<"Please select a valid option."<<endl;
}
}
}
I found the bug, it was a type conversion bug, i was accepting a float value but passing it as a int,just worked, don't know why.But thanks for each comment and suggestion.
I am trying to make a really simple text menu of sorts using switch statements for a class assignment. The issue is when calling the various functions I've made in the different cases of the switch statement, the never seem to end.
Considering the same issue seems to be happening to all the functions in my switch statement I think it might have something to do with the switch statement itself and not the individual functions although I honestly don't know at this point.
Disclaimer: I know I have tons of other issues with my code, but im just trying to pick a big one for simplicity. I would appreciate advice on other parts of my code aswell. (Also its not done yet as you can probably see by the unfinished cases)
Example output:
==================
1. Admission's Office
2. Student
3. End Program
==================
Enter your choice: 1
Enter the password: 123
******************
1. Add a new student
2. Add new students from a file
3. Drop a student
4. View one students info
5. View all students info
******************
Enter your choice: 1
Enter the first name: First Name
Enter the last name: Last Name
Enter the gender: m
Enter the first name: It should only ask me once
Enter the last name: Why is this looping?
Enter the gender: ????
Enter the first name:
Enter the last name: ???????
Enter the gender: a
Enter the first name: ^C
Code
//main.cpp
int main() {
Student stuAr[SIZE];
int choice, password, userInput;
int numStu = 0;
bool valid;
do {
userMenu();
cin>>choice;
cout<<endl;
switch(choice){
case 1:
cout<<"Enter the password: ";
cin>>password;
cout<<endl;
valid = checkPass(password);
if (valid) {
adminMenu();
cin>>choice;
cout<<endl;
do {
switch(choice) {
case 1:
addStu(stuAr, numStu);
break;
case 2:
addStuFile(stuAr, numStu);
break;
case 3:
cout<<"Enter the student ID: ";
cin>>userInput;
cout<<endl;
dropStu(stuAr, numStu, userInput);
break;
case 4:
cout<<"Enter the student ID: ";
cin>>userInput;
cout<<endl;
viewStu(stuAr, numStu, userInput);
break;
case 5:
viewAllStu(stuAr, numStu);
break;
}
}while(choice != 6);
}
else {
cout<<"The password is wrong."<<endl;
}
break;
case 2:
break;
}
}while(choice != 3);
return 0;
//still main.cpp
//addStu function for case 1 of the nested switch statement
void addStu(Student stuAr[], int& numStu) {
cin.ignore();
cout<<"Enter the first name: ";
stuAr[numStu].setFN();
cout<<endl;
//cin.ignore();
cout<<"Enter the last name: ";
stuAr[numStu].setLN();
cout<<endl;
cout<<"Enter the gender: ";
stuAr[numStu].setGender();
cout<<endl;
numStu++;
return;
}
You are never rereading choice within your inner loop.
if (valid) {
adminMenu();
cin>>choice; // only done once, therefore do loop never terminates
cout<<endl;
do {
switch(choice) {
case 1:
addStu(stuAr, numStu);
break;
// ...
}
// after switch-case, add:
cin >> choice;
cout << endl;
You never change choice in the inner do..while loop, and therefore the condition choice != 6 will always be true, unless you enter it in the first time.
My teacher wanted me to make a program based on An Employee which includes -
His Name
His Id
His Sallary
and we needed to make this using - Structures & Functions & Array as we are not that good in C++ so we just make simple programs . the code of the program i made is below , [ http://pastebin.com/9UFFJseN ]
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<process.h>
#include<stdio.h>
struct employee
{
char empname[40];
int empid,empsalary;
}; employee e1;
void add_record(void);
void display_record(void);
void search_record(void);
void welcome();
void exito();
void main()
{
clrscr();
int choice,id;
cout<<"Welcome to Employee Info Menu by Rohit Jalan \n\n\n";
cout<<"1 -> Add Record\n";
cout<<"2 -> Display Record\n";
cout<<"3 -> Search Record\n";
cout<<"4 -> Exit";
cout<<"\n\t\t --> ";
cin>>choice;
switch(choice)
{
case 1: add_record();break;
case 2: display_record();break;
case 3: search_record();break;
case 4: exito();
}
getch();
}
void add_record()
{
clrscr();
cout<<"You have pressed 1\n\n\n";
cout<<"Please enter employee name : ";
gets(e1.empname);
cout<<"Please enter the employee id : ";
cin>>e1.empid;
cout<<"Please enter the employee salary : ";
cin>>e1.empsalary;
cout<<" \n \n \n \n";
main();
}
void display_record()
{clrscr();
cout<<"\nEmployee Name is : ";
puts(e1.empname);
cout<<"\nEmployee ID is : ";
cout<<e1.empid;
cout<<"\nEmployee salary is : ";
cout<<e1.empsalary;
getch();
main();
}
void search_record()
{int id;
clrscr();
cout<<"Please enter the id of the employee\n: ";
cin>>id;
if(id==e1.empid)
{
display_record();
}
else cout<<"\nRecord not found...";
getch();
main();
}
void exito()
{clrscr();
cout<<"\n\n\n\n\t\t\t Thank you. ";
cout<<"\n\n\n\n\n\n\t\t\t Program by \n\n\n";
cout<<"\t\t\t";
delay(500);cout<<"ROHIT";
delay(500);cout<<" JALAN";
cout<<"\n\t\t\t";
delay(500);cout<<"Roll No";
delay(500);cout<<" 11436";
cout<<"\n\t\t\t";
delay(500);cout<<"Class";
delay(500);cout<<" XI-D";
delay(500);cout<<"....";
delay(100);
exit(0);
}
And now my teacher is asking me what if she wishes to enter more then 1 record of data and display more then 1 , or you can say she wants to decide that how much she wants to enter and display , as i am unable to solve her problem i request you all guys to help me in a simple and easy manner as i`m newbie out here . Please don't delay in answer i have a practical examination on 27th Feb 2015 . if you are unable to see my coding -
http://pastebin.com/9UFFJseN#
Thanks In Advance .
In order to perform actions more than once, you need a loop.
unsigned int Display_Menu(void)
{
unsigned int selection = 0;
while (selection != 4)
{
static const char menu_text[] =
"\n"
"Welcome to Employee Info Menu by Rohit Jalan \n\n\n"
"1 -> Add Record\n"
"2 -> Display Record\n"
"3 -> Search Record\n"
"4 -> Exit\n"
"\t\t --> ";
cout.write(menu_text, sizeof(menu_text) - 1);
cin >> selection;
if ((selection >= 1) && (selection <= 4))
{
break;
}
cout << "\nInvalid selection, try again.\n";
}
return selection;
}
The above function displays a menu and if the User enters an invalid choice, it will print the menu again. After a valid choice is received, the function returns to the caller.
This is an example of doing something more than once.
So if your instructor wants to perform more than one action, how would you do it?
Edit 1:
In general a Menu consists of two pieces: 1) Displaying the selections and 2) Processing the choices. The above code handles the displaying of the selections.
// Forward declarations
void Add_Record(void);
void Display_Record(void);
void Search_Record(void);
void Process_Choices(void)
{
unsigned int choice = 0;
do
{
choice = Display_Menu();
switch (choice)
{
case 1: Add_Record(); break;
case 2: Display_Record(); break;
case 3: Search_Record(); break;
}
} while (choice != 4);
}
The above function displays the menu, calls functions according to the User's selection and repeats. It ends when the User enters the number 4.
Again, notice the loop construct that is used for performing actions more than once.
Ok so I'm having trouble with this C++ program I am supposed to do for school and I need some help with an error I keep getting. I have the basics but I need help with classes and objects.
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
class BankAccount {
private:
double accountBalance;
string name;
public:
BankAccount();
BankAccount(string,double);
double deposit(double);
double withdraw(double);
void checkBalance();
};
BankAccount::BankAccount()
{
accountBalance=0;
name="";
}
BankAccount::BankAccount(string name,double money)
{
name=name;
accountBalance=accountBalance;
}
double BankAccount::deposit(double money)
{
accountBalance+=money;
return accountBalance;
}
double BankAccount::withdraw(double money)
{
accountBalance-=money;
return accountBalance;
};
void BankAccount::checkBalance()
{
cout<<"The balance on the account is $"<<accountBalance<<"!!"<<endl;
};
int main(int argc, char *argv[])
{
int c;
double m;
string n;
cout<<"==================Bank======="<<endl;
cout<<"[1] Open a new Bank Account |"<<endl;
cout<<"[2] Deposit money |"<<endl;
cout<<"[3] Withdraw money |"<<endl;
cout<<"[4] Check balance |"<<endl;
cout<<"============================="<<endl;
cout<<endl;
cout<<"What would you like to do :";
cin>>c;
switch (c){
case 1:
cout<<"Ok I see you want to open a new Bank Account"<<endl;
cout<<"But first answer a few questions:"<<endl;
cout<<"What is your name? ";
cin>>n;
cout<<"Next tell me the amount of money you wish to open your account with: ";
cin>>m;
BankAccount::BankAccount(n,m);
cout<<"OK all set, "<<n<<"!!"<<endl;
break;
case 2:
cout<<"How much money would you like to deposit? : ";
cin>>m;
BankAccount.deposit(m);
break;
case 3:
cout<<"How much money would you like to withdraw? : ";
cin>>m;
BankAccount.withdraw(m);
break;
case 4:
cout<<"OK I'll check your balance"<<endl;
BankAccount.checkBalance();
break;
}
system("PAUSE");
return EXIT_SUCCESS;
}
So if you could help me that would be very much appreciated.
BankAccount is a type name, not a variable name. You cannot invoke instance methods on a type.
Create a variable of type BankAccount, assign it an instance, and then call methods on the instance using the same notation that you already have:
BankAccount acct;
switch (c){
case 1:
cout<<"Ok I see you want to open a new Bank Account"<<endl;
cout<<"But first answer a few questions:"<<endl;
cout<<"What is your name? ";
cin>>n;
cout<<"Next tell me the amount of money you wish to open your account with: ";
cin>>m;
acct = BankAccount(n,m);
cout<<"OK all set, "<<n<<"!!"<<endl;
break;
case 2:
cout<<"How much money would you like to deposit? : ";
cin>>m;
acct.deposit(m);
break;
case 3:
cout<<"How much money would you like to withdraw? : ";
cin>>m;
acct.withdraw(m);
break;
case 4:
cout<<"OK I'll check your balance"<<endl;
acct.checkBalance();
break;
}
BankAccount is a class you defined, you cannot use :: operator to access non-static members of that class. In this case, you need to first create an object of BankAccount, then using dot operator to access the deposit and other member functions since those non-static member functions are associated with instance of the class.
If deposit and other relevant functions are static member function, you can do that.
I can't seem to figure out why this while loop stopped looping. It was doing fine before I moved some code around. Now I got something else working and it just doesn't loop. I've also tried making quit a bool set to true and tried to have it loop while it was true until the user hit 4 to exit in which case it would turn it to false but that didn't work. I also tried adding a while loop to the function of showMenu but that also didn't work. I know it must be something simple I just can't catch it. gggrrrr.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
enum transType { SETUP=1, DEPOSITE, WITHDRAW, EXIT};
int showMenu(double balance);
double transaction(double amount, double balance, transType trans);
int menuSwitch;
int quit=0;
int _tmain(int argc, _TCHAR* argv[]){
int amount=0,balance=0;
while(quit!=4){
showMenu(balance);
switch (menuSwitch){
case DEPOSITE:
cout<<"Enter the amount of deposit: ";
cin>>amount;
cout<<"Your current balance is: "<<transaction(amount,balance,DEPOSITE)<<endl<<endl;
break;
case WITHDRAW:
cout<<"Enter the amount of withdraw: ";
cin>>amount;
if(amount>balance){
cout<<"*** Insufficient funds."<<"Your current balance is: "<<transaction(amount,balance,WITHDRAW)<<endl<<endl;
}
else cout<<"Your current balance is: "<<transaction(amount,balance,WITHDRAW)<<endl<<endl;
break;
case EXIT:
cout<<"Have a Nice Day."<<endl;
quit=4;
break;
}
return 0;
}
}
int showMenu(double balance){
// while(quit==true){
cout<<"Your Online Checking Account System"<<endl;
cout<<"-------------------------------------------"<<endl;
cout<<"Select an option:"<<endl<<endl;
cout<<" 1. Set up the account."<<endl;
cout<<" 2. Deposit Funds into your Account."<<endl;
cout<<" 3. Withdraw Funds out of your Account."<<endl;
cout<<" 4. Exit"<<endl;
cout<<endl<<">>";
cin>>menuSwitch;
switch (menuSwitch){
case SETUP:
cout<<"Enter the balance: ";
cin>>balance;
cout<<endl<<"Your current balance is: "<<balance<<endl<<endl;
break;
}
return balance;
// }
}
double transaction(double amount, double balance, transType trans){
double withdraw = balance-amount;
double deposite = balance+amount;
if(trans=DEPOSITE){
return deposite;
}
else
return withdraw;
}
//return balance;
You return 0 within the switch brackets, ie inside the while loop. Change it so that you return 0 outside of the while loop.