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.
Related
I'm trying to write a program that simulates an ATM. The user can log in with a pin and account number, check their account balance, and make a withdrawal. I'm having trouble initializing the array that contains the account information, here's what I have so far:
#include <iostream>
#include <string>
using namespace std;
class Account
{
private: int accountNum;
string accountPin;
double balance;
void setPin();
void setAccountNum();
public: Account ()//default constructor
{
accountNum = -1;
accountPin = -1;
balance = 0.0;
};
Account (int accountNum, string accountPin, double balance)
//overloaded construtor
{
accountNum = accountNum;
accountPin = accountPin;
balance = balance;
};
void setAccountBalance(double bal);//acc balance setter
int getAccountNum() //acc balance getter
{
return balance;
}
bool confirmPin(string)//confirm pin# func
{
}
void updateBalance(double)
};
int main ()
{
int option;
//accounts array
Account account[]= (123, "abc123", 100.00), (456, "def456", 50.00),(789,"ghi789", 500.63);
//login code, unfinished
cout << "LOGIN\nEnter Account#: "<< endl;
cin >> accNum;
cout << "Enter pin#: "<<endl;;
getline(accPin);
//menu do while loop, unfinshed
do {
cout << "1. Check balance\n2.Make a deposit\n3.Logout\n";
cin >> option;
switch (option)
//check balance case
case 1:
// make a deposit case
case 2:
}
while (option !=3);
return 0;
}
Line 48 is where the array needs to be initialized, it contains the account number, the pin code, and the account balance (in that order). Can anyone point out the mistake I'm making? Thanks for the help in advance.
You need curly braces around the entire initializer list, and also around the initializer for each element.
Account account[]= {
{123, "abc123", 100.00},
{456, "def456", 50.00},
{789,"ghi789", 500.63}
};
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.
How can I store my input in the following catagories i.e water,domestic etc.When I give information about water birds then I give information about domestic birds.but 2nd information overrides the first one.Is there any way to store data for catagory wise?
/* declaring header files */
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include <fstream>
#include <bits/stdc++.h>
#include <string>
using namespace std;
class Bird{
private:
char name[50],colour[50],nature[50],location[50];
float living_duration;
public:
int code;
int set_info(){
char name='\0';
char colour='\0';
char nature='\0';
char location='\0';
float living_duration=0.0;
}
int get_info(){
cout<<"\nEnter bird's name: ";
cin>>name;
cout<<"Colour: ";
cin>>colour;
cout<<"Nature: ";
cin>>nature;
cout<<"Location: ";
cin>>location;
cout<<"Living Duration: ";
cin>>living_duration;
cout<<"Bird's code: ";
cin>>code;
}
int display_info(){
cout<<"\nBird's name: "<<name;
cout<<"\nColour : "<<colour;
cout<<"\nNature : "<<nature;
cout<<"\nlocation : "<<location;
cout<<"\nLiving Duration : "<<living_duration<<" year";
cout<<"\nCode : "<<code;
}
}obj[100];
int main(){
int i,j,k,n,m;
do{
cout<<"\n\nWhat do you want to do\n1.Input bird's information"
<<"\n2.Display\n3.Search\n4.Exit."
<<"\n\nChoose appropriate number: ";
cin>>n;
switch(n){
case 1://bird information
cout<<"Please Select Birds Category"<<endl;
cout<<"------------------"<<endl;
cout<<"1)Water\n2)Domestic\n3)prey\n4)treebased\n5)flightless\n6)migratory\n"<<endl;
cin>>m;
switch(m){
case 1:
cout<<"Enter the number of bird how many to input: ";
cin>>j;
for(i=1;i<=j;i++){
cout<<"\nInformation of Bird "<<i<<".\n";
obj[i].get_info();
}
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
default:
cout<<"Wrong choice!!\nPlease enter correct number.";
break;
}
case 2://display
for(i=1;i<=j;i++)
{
cout<<"\nBird no "<<i<<".\n";
obj[i].display_info();
cout<<"\n";
}
break;
case 3://search
cout<<"\nEnter the bird code: ";
cin>>k;
for(i=1;i<=j;i++)
{
if(k==obj[i].code)
{
cout<<"\nBird no "<<i<<".\n";
obj[i].display_info();
break;
}
}
if(k!=obj[i].code)
cout<<"Wrong code input...\n";
break;
case 4://exit
break;
default:
cout<<"Wrong choice!!\nPlease enter correct number.";
break;
}
}while(n!=4);
}
You need to split your concepts between bird data and a container of data.
In a relational database, you would have tables. Let the columns of a table be represented by the data members of a structure. A record (row) of the table will be an instance of the record structure:
class Bird
{
public:
std::string name;
std::string colour;
std::string nature;
std::string location;
float living_duration;
};
For the container or table, you could use std::vector:
std::vector<Bird> bird_table;
Many relational databases also include index tables to speed up searches. The index table will contain pairs, the key (or column value) and an index into the std::vector. The C++ language has a handy container called a std::map:
std::map<string, unsigned int> name_index;
The string parameter represents the key or column type.
The unsigned int parameter represents the index into the database (a.k.a foreign key).
To retrieve a Bird record by name you access the index table first, then the vector:
unsigned int database_index = name_index["crow"];
Bird crow = database[index];
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.
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.