Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
so i am working on an a little project the main idea of is that there library that have library items(book,magazine) and student and the main menu of the program have adding student,library item removing student library items...etc,so when i am trying to add a book it work perfect but when i am trying to add magazine it get into a infinite loop and doesn't letting me to enter value to enter other option and its my first program i may have a-lot of error using dev c++ and it not done yet the code::
#include<iostream>
#include<string>
using namespace std;
char enter;
string none,none1,none2;
int is,iff;
int B_num=0;//books number
int M_num=0;//magazine number
int S_num=0;//student number
class Clibrary_items{//the clibrary class is a class that got the common elemt of the magazine and the book
protected:
string name,publisher;
string av="availabil";//the availability of a book or a magazine
};
class Cbook: public Clibrary_items{
private:
string author_name;
public:
virtual void set(string n,string p,string a)
{
name=n;
publisher=p;
author_name=a;
}
virtual void print()//the the printer function that print the pointed book
{
cout<<"the name is::"<<name<<endl;
cout<<"the publisher is::"<<publisher<<endl;
cout<<"the author name is::"<<author_name<<endl;
}
};
class Cmagazine: public Clibrary_items{
private:
int isbn;
public:
virtual void set(string na,string pu,int i)
{
name=na;
publisher=pu;
isbn=i;
}
virtual void print()//the the printer function that print the pointed magazine
{
cout<<"the name is::"<<name<<endl;
cout<<"the publisher is::"<<publisher<<endl;
cout<<"the issue number is::"<<isbn<<endl;
}
};
class Cstudent{
friend class Clibrary;
private:
string name="";
int ID;
public:
void set(string sn,int number)
{ID=number;
name=sn;}
void print_student()//the the printer function that print the pointed student
{ cout<<"ID:"<<ID<<" ";
cout<<"student name is::"<<name<<endl;}
};
class Clibrary//the main class
{
public:
Cbook book[100];//dynamic array for books
Cmagazine magazine[100];//dynamic array for magazine
Cstudent student[100];//dynamic array for student
void print()//the main function that print the main menu and get you to everthing
{
while(enter!='q')
{
cout << "[a] Add student" << endl;
cout << "[b] Remove student" << endl;
cout << "[c] Add Item to the Library" << endl;
cout << "[d] Remove Item from the Library" << endl;
cout << "[e] Borrow Item from the Library" << endl;
cout << "[f] Return Item to the Library" << endl;
cout << "[g] Show all Library Items" << endl;
cout << "[i] Show all reserved Library items" << endl;
cout << "[j] Show all free Library items" << endl;
cout << "[k] Show all students" << endl;
cout << "[l] Show all students who borrowed items" << endl;
cout << "[m] Find the student who borrowed a specific item" << endl;
cout << "[n] Show all Items borrowed by student" << endl;
cout << "q) Quit to Windows" << endl;
cin>>enter;
switch (enter)
{
case 'a':
cout<<"please enter the name::";
cin>>none;
student[S_num].set(none,S_num);
S_num++;
break;
case 'k':
for(int i=0;i<S_num;i++)
{
student[i].print_student();
}
break;
case 'b':
cout<<"please enter the name of the student you want to remove:";
cin>>none;
for(int i=0;i<S_num;i++)
if(none==student[i].name)
student[i].name="";
break;
case 'c':
cout<<"[1].add book"<<endl;
cout<<"[2].add magazine"<<endl;
cin>>iff;
if(iff==1)
{
cout<<"please enter the name of the book:"<<endl;
cin>>none;
cout<<"please enter the publisher:"<<endl;
cin>>none1;
cout<<"please enter the author name:"<<endl;
cin>>none2;
book[B_num].set(none,none1,none2);
}
if(iff==2)
{
cout<<"please enter the name of the magazine:"<<endl;
cin>>none;
cout<<"please enter the publisher:"<<endl;
cin>>none1;
cout<<"please enter the issue number:"<<endl;
cin>>is;
magazine[M_num].set(none,none1,is);
}
break;
}
}
}
};
main()
{
cout <<"****** ****** ****** ****** ****** ******" << endl;
cout <<"* *" << endl;
cout <<"****** Library Management System ******" << endl;
cout <<"* *" << endl;
cout <<"* BY: <M.Saed Ramadan> *" << endl;
cout <<"* *" << endl;
cout <<"****** ****** ****** ****** ****** ******" << endl;
Clibrary librarby;
librarby.print();
}
To make your code more manageable, I suggest you use function calls in your switch statement:
switch (selection) // "enter" is such a bad name.
{
case 'a':
Add_Student();
break;
case 'b':
Remove_Student();
//...
default:
cout << "Invalid menu selection, try again.\n";
break;
}
This simplification (for easy reading) leads to the following table driven menu system:
// Declare a function pointer syntax
typedef void (*Function_Pointer)(void);
struct Menu_Entry
{
char selection_char;
Function_Pointer menu_function;
};
// The table
const Menu_Entry First_Menu[] =
{
{'a', Add_Student}, // A selection entry
{'b', Remove_Student},
//...
};
const unsigned int Number_Of_Selections =
sizeof(First_Menu) / sizeof(First_Menu[0]);
// The lookup engine
char selection; // This you input from user.
for (unsigned int i = 0; i < Number_Of_Selections; ++i)
{
if (selection == First_Menu[i].selection_char)
{
// Execute the function associated with the selection
First_Menu[i].menu_function();
break;
}
}
An advantage to the table system is that you can easily add a menu selection item by:
Creating a function to perform the selection.
Adding a Menu_Item entry to the table.
That's it. Quite simple.
Which allows you to get the menu selection process working for one selection, then work on another; piece by piece.
By placing the functions in separate files, you only need to compile the lookup table source and the new selection source files. All the other code remains untouched. Much faster build times.
Related
So im trying to run a program that receives a set data from a file that updates each time the program is run and new data is added. But when i try to run it the program terminates after trying to receive input from user.The full code is as follow, im using devc++ if that matters
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
struct Deets
{
string NameSite[];
string AddressName[];
int numVaccine[];
int numStall[];
string DateVax[];
string ContNum[];
};
void Outputmenu();
int AddData(int,struct Deets V);
void EditData(int,struct Deets V);
void ShowExisting(int,struct Deets V);
void ClearArr(int,struct Deets V);
int main()
{
int i;
ifstream r("Total.txt");
r>>i;
r.close();
Deets V;
ifstream a;
a.open("Data.txt");
for(int c=0;c<=i;c++)
while(a>>V.NameSite[c]>>V.AddressName[c]>>V.numVaccine[c]>>V.numStall[c]>>V.DateVax[c]>>V.ContNum[c]);
a.close();
int menuS;
do
{
Outputmenu();
cout<<"\nPlease choose from 1-4, press 0 to close menu"<<endl;
cin>>menuS;
if (menuS==1)
AddData(i,V);
else if (menuS==2)
EditData(i,V);
else if (menuS==3)
ShowExisting(i,V);
else if (menuS==0)
cout<<"Thank you for your cooperation, We hope to see you soon! \nClosing program..,";
else
cout<<"Error, not within selection"<<endl;
}
while (menuS!=0);
ofstream b("Data.txt");
for(int c=0;c<=i;c++)
while(b<<V.NameSite[c]<<" "<<V.AddressName[c]<<" "<<V.numVaccine[c]<<" "<<V.numStall[c]<<" "<<V.DateVax[c]<<" "<<V.ContNum[c]<<endl);
return 0;
}
void Outputmenu() //instruction about the command for the user
{
cout << " -hello there!- " << endl;
cout << "===================================================================" << endl;
cout << " ***PLEASE CHOOSE ONE OF THE OPTION BELOW*** " << endl;
cout << "===================================================================" << endl;
cout << "\t1. Add Data (about Vaccination Center)" << endl;
cout << "\t2. Edit Data" << endl;
cout << "\t3. Show Existing Data" << endl;
cout << "\t0. Close Program" << endl;
cout << "===================================================================" << endl;
}
int AddData(int c, struct Deets A)
{
bool rq;
int i = c;
cout<<"Enter your company name/ institute name : ";
cin.ignore();
getline(cin,A.NameSite[i]);
cout<<"\n\t\t Safety Measure"<<endl;
cout<<"-Maintain a distance of 1 meter between one other access to vaccine doses.\n"<<"-Have a dedicated toilets for patient\n"<<"-Ability to maintain room temperature between 19 – 25 degrees \n"<<"-Have an adequate place for patients to wait "<<"\n-Have adequate sharps disposal bins, appropriate for the volume of patients, and securely placed and spaced to mitigate the risk of needle stick injuries."<<endl;
cout<<"-Adequate handwashing facilities for staff, and antimicrobial hand sanitizers available.\n"<<"-Store and handle COVID-19 vaccines under proper conditions, including maintaining cold chain conditions and chain of custody at all times in accordance with a EUA or vaccine package insert, manufacturer guidance, and CDC guidance in the Vaccine Storage and Handling Toolkit."<<endl;
cout<<"-Appropriate security provisions to ensure no unauthorized access to vaccine doses."<<endl;
cout<<"\nAre all requirement met? \n1:Yes \n0:No"<<endl;
cin>>rq;
if(rq == 1)
{
cout<<"\nWhere is the location : "<<endl;
cin>>A.AddressName[i];
cout<<"How many vaccines required : "<<endl;
cin>>A.numVaccine[i];
cout<<"How many vaccine stall can a site hold : "<<endl;
cin>>A.numStall[i];
cin.ignore();
cout<<"Date for vaccine to be distributed"<<endl;
getline(cin,A.DateVax[i]);
cin.ignore();
cout<<"Enter your management contact number : "<<endl;
getline(cin,A.ContNum[i]);
cout<<"Certificate for "<<A.NameSite[i]<<endl;
i++;
}
else
{
cout<<"Thanks for the thought. We hope to cooperate with "<<(A.NameSite[i])<<" in the near future\n\n"<<endl;
if(i>0)
{
i = i-1;
}
else
{
i = 0;
}
}
return i;
}
void EditData(int i,struct Deets A)
{
int c,EN,edit=0;
for (int c=0;c<i;c++) //showing all Company/Institute name data
{
cout << c+1 << ". "<<A.NameSite[c]<<endl;
}
cout << "Enter number you would like to edit : "; //asking user to enter which data user want to edit
cin >> EN; //user enter which data they want to edit
c=EN-1;
cout << A.NameSite[c] << "\n" << "Do you want to edit this data? Yes:1 No:0"<<endl; //asking user whether they want to edit Company/Institute name
cin >> edit;
if(edit ==1) //if true, it will change the data
cin >> A.NameSite[c];
cout <<"Total stall : "<< A.numStall[c] << "\n" << "Do you want to edit this data? Yes:1 No:0"<<endl; //asking user whether they want to edit total Company/Institute vaccine stall
cin >> edit;
if(edit ==1) //if true, it will change the data
cin >> A.numStall[c];
cout <<"Adress : " << A.AddressName[c] << "\n" << "Do you want to edit this data? Yes:1 No:0"<<endl; //asking user whether they want to edit Company/Institute's address
cin >> edit;
if(edit ==1) //if true, it will change the data
cin >> A.AddressName[c];
cout << "Contact number : " << A.ContNum[c] << "\n" << "Do you want to edit this data? Yes:1 No:0"<<endl; //asking user whether they want to edit Company/Institute's contact number
cin >> edit;
if(edit ==1) //if true, it will change the data
cin >> A.ContNum[c];
cout << "Total Vaccine : "<< A.numVaccine[c] << "\n" << "Do you want to edit this data? Yes:1 No:0"<<endl; //asking user whether they want to edit total Company/Institute vaccine needed
cin >> edit;
if(edit ==1) //if true, it will change the data
cin >> A.numVaccine[c];
cout << "Date of vaccine : "<< A.DateVax[c]<< "\n" << "Do you want to edit this data? Yes:1 No:0"<<endl; //asking user whether they want to edit Company/Institute's receiving vaccine date
cin >> edit;
if(edit ==1) //if true, it will change the data
cin >> A.DateVax[c];
}
void ShowExisting(int i, struct Deets A) // void of ShowExisting that show the existing data stored in the program
{
for(int c=0; c<i; c++) //array to show all the existing data in the program so far
{
cout << (c+1) << ". "<< endl; //number that show the count of which set of data shown
cout << "Site Name: " << A.NameSite[c] << endl; //show the name of site in appropriate array of turn c
cout << "Address: " << A.AddressName[c] << endl; //show the address of site in appropriate array of turn c
cout << "Amount Vaccine: " << A.numVaccine[c]<< endl; //show the amount of vaccine needed in site in appropriate array of turn c
cout << "Amount Stall: " << A.numStall[c] << endl; //show the amount of stall needed in appropriate array of turn c
cout << "Vaccination Date: " << A.DateVax[c] << endl; //show the vaccination date in appropriate array of turn c
cout << "Contact Num.: " << A.ContNum[c] << "\n" << endl; //show the contact number of the reprsentative from the site in appropriate array of turn c
}
}
The program runs until input when there is no data in the file but when there is data, the program wont even go past
for(int c=0;c<=i;c++)
while(a>>V.NameSite[c]>>V.AddressName[c]>>V.numVaccine[c]>>V.numStall[c]>>V.DateVax[c]>>V.ContNum[c]);
please help as my caffeine brain is fried
*After considering everyone's response ive decided to rework the basis of the program with what everyone taught, Thank you for those who responded
IMHO, I would rewrite/redesign your code rather than debug it.
Let's start by differentiating a single Deet from a container of Deet:
struct Deet
{
string NameSite;
string AddressName;
int numVaccine;
int numStall;
string DateVax;
string ContNum;
};
You can then overload operator>>:
struct Deet
{
//...
friend std::istream& operator>>(std::istream& input, Deet& d);
};
std::istream& operator>>(std::istream& input, Deet& d)
{
std::getline(input, d.NameSite);
std::getline(input, d.AddressName);
input >> d.numVaccine;
input >> d.numStall;
std::getline(input, d.DateVax);
std::getline(input, d.ContNum);
return input;
}
You could declare a container and input the container like so:
std::vector<Deet> deet_container;
Deet d;
while (input_file >> d)
{
deet_container.push_back(d);
}
BTW, you should always put descriptive variables inside your function declarations. This gives people a better understanding of what the variables are and their positions.
Your function declarations could change:
typedef std::vector<Deet> Deet_Vector;
int AddData(int, Deet_Vector& V);
void EditData(int, Deet_Vector& V);
void ShowExisting(int, Deet_Vector& V);
void ClearArr(int, Deet_Vector& V);
By looking at AddData, the usage of the int first parameter is ambiguous.
Are you adding an int to the container?
Is the int the quantities of items to add to the container?
Likewise with the other functions.
Next, I would add one function at a time to the program, compile and debug, then add another, repeat.
BTW, you can simplify your Outputmenu function:
void Outputmenu()
{
static const char menu_text[] =
" -hello there!-\n"
"===========================================================\n"
" ***PLEASE CHOOSE ONE OF THE OPTION BELOW***\n"
"===========================================================\n"
"\t1. Add Data (about Vaccination Center)\n"
"\t2. Edit Data\n"
"\t3. Show Existing Data\n"
"\t0. Close Program\n"
"============================================================\n"
;
std::cout.write(menu_text, sizeof(menu_text);
}
By using the write method, the text is written as a block and is usually a lot faster than using a lot of operator<<.
So, I am making this bank management system in c++ where I will have to give the user an option to create an account, deposit the money, withdraw it, and display the details. I also need to store in the array of objects so that the entire data can be displayed after the user exits. The restrictions are that I cannot use file handling. But it isn't working properly.
Please help.
When I run it, it keeps asking me for full name. How do I resolve this issue?
I feel like this issue is occurring because of the persons array of type bankaccount, but I don't see any other possible way to do this.
I have deleted the details of some functions because it became a lengthy block of code.
#include<iostream>
#include<string>
#include <time.h>
#include <cstdlib>
using namespace std;
class bankaccount {
private:
int id;
string name;
int cash;
int money;
int age;
public:
string get_name() {
return name;
}
int get_id() {
return id;
}
void withdraw();
void deposit();
int see_money();
bankaccount(int id1) {
id = id1;
cout << "\n Enter Full Name:";
getline(cin, name);
}
friend ostream& operator<<(ostream& os, const bankaccount& d);
};
ostream& operator<<(ostream& os, bankaccount& d) {
os << "\n Your name is:" << d.get_name();
os << "\n Your id is:" << d.get_id();
os << "\n You have a total of : " << d.see_money();
}
int main() {
bankaccount persons[100] = 0;
int option;
int id;
int number = 0;
cout << "BANKING MANAGEMENT SYSTEM!" << endl;
cout << "-------------------------------------------------------------------------------";
while (1) {
cout << "\nEnter 1 to create an account. Enter 2 to deposit money. Enter 3 to withdraw money. Enter 4 to check money. Enter 5 to display. Enter 6 to exit";
cin >> option;
switch (option) {
case 1: {
bankaccount p(number);
persons[number] = p;
cout << "Your ID is:" << number << endl;
number++;
break;
}
case 2: {
cout << "\n Enter Your ID:";
cin >> number;
persons[number].deposit();
break;
}
case 3: {
cout << "\n Enter Your ID:";
cin >> number;
persons[number].withdraw();
break;
}
case 4: {
cout << "\n Enter Your ID:";
cin >> number;
persons[number].see_money();
break;
}
case 5: {
cout << "\n Enter Your ID:";
cin >> number;
cout << persons[number];
break;
}
}
}
}
bankaccount persons[100]=0;
Here you construct 100 objects of your bankaccount.
Your bankaccount constructor has these 2 lines:
cout<<"\n Enter Full Name:";
getline(cin,name);
So each time you create a bankaccount object, you're prompted for its name. You probably didn't intend that. You need to de-couple this, so asking the user for the Full Name, assigning it to a bankaccount, and constructing a bankaccount object are separate.
e.g. you can create option 6 to assign a name to the bankaccount instead of doing it inside the constructor of your bankaccount class.
If you aren't restricted the use of pointers or you don't have to specifically store it in a static array you can try this. You can create an array of bankaccount type pointers. In this case an array of 100 pointers. Each pointer will point to a separate object of bankaccount. https://imgur.com/a/KflNVbc for a better visual understanding.
You can create it like this:
bankaccount* persons[100];
So in case 1, when you create a new account and add it to the array you can do this:
persons[number] = new bankaccount();
number++;
This way you won't have to change your constructor. Just a cool way of doing this.
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
struct student
{
string firstname;
string lastname;
int age;
string gender;
string course;
string year;
string section;
int studno;
};
int menu(int &menuChoice);
void userInfo(student user[], int size, int const track);
void listUser(student user[], int size, int const track);
int main(int argc, char** argv) {
int menuChoice;
int static track = 0;
student user[30];
do {
menu(menuChoice);
switch(menuChoice) {
case 3:
userInfo(user, 30, track);
track++;
break;
case 4:
listUser(user, 30, track);
break;
}
} while(menuChoice != 5);
return 0;
}
int menu(int &menuChoice)
{
do {
cout<< "\n\t\t==========================================="
<< "\n\t\t|\tSimple Student Record Program\t |"
<< "\n\t\t==========================================="
<< "\n\n\t\t\t\t [MAIN MENU]"
<< "\n\n\n[1] - Search Student\n[2] - Best Students\n[3] - Add New Student\n[4] - View Students\n[5] - Delete Student"
<< "\n\nEnter a choice: ";
cin>> menuChoice;
if (menuChoice>5||menuChoice<1)
{
cout<< "Please select an existing option!";
}
}
while (menuChoice>5||menuChoice<1);
return menuChoice;
}
void userInfo(student user[], int size, int const track)
{
cout<< "\n\n\t\t\t\t[Add a student]";
cout<< "\n\nEnter student's first name: ";
cin>> user[track].firstname;
cout<< "\nEnter student's last name: ";
cin>> user[track].lastname;
cout<< "\nAge: ";
cin>> user[track].age;
cout<< "\nGender: ";
cin>> user[track].gender;
cout<< "\nCourse: ";
cin>> user[track].course;
cout<< "\nYear: ";
cin>> user[track].year;
cout<< "\nSection: ";
cin>> user[track].section;
cout<< "\nStudent No.: ";
cin>> user[track].studno;
return;
}
void listUser(student user[], int size, int const track)
{
int list;
cout<< "\n\tName|||Age|||Gender|||Course|||Year|||Section|||Student No.\n";
for (list=0; list<track; list++)
{
cout<< "\n\n" << user[list].firstname <<" " << user[list].lastname <<"\t\t" << user[list].age <<"\t" << user[list].gender << "\t" << user[list].course << "\t" << user[list].year << "\t" << user[list].section << " " << user[list].studno <<"\n";
}
return;
}
My previous question was about a student record program got on hold for being broad, so, taking the advice and answers from some users there, I started to search and take action, I'll be specific this time.
So here's the whole code so far. My question is: how to clear the screen so that when you choose an option on the menu, you are then taken to that specific 'place' in the code and once you are done with that part of the program, clear the screen again and go back to the menu?
This is impossible to do with standard C++, which has no notion of a screen at all. You'd have to use a 3rd party library (e.g. the Windows API if you're on Windows). Since I assume that this is an exercise where only standard, platform-independent C++ is allowed, you cannot meet the requirement.
What I can tell you, again, is that you are not supposed to use arrays for such basic programming. Use std::vector.
This code is from my "virtual ATM machine" program which deals with customers depositing, checking balance and withdrawing money from their account. When I deposit the money, it displays that it gets deposited.. But... here goes the code before I state my problem:
double bankAccount::deposit()
{
bankAccount b;
double amt;
system("cls");
cout << " ----------------------------------------------------------------------- \n";
cout << "| Customer Menu | \n";
cout << " ----------------- ----------------- ----------------- ----------------- \n";
cout << "\n\nYOUR CURRENT BALANCE: " << balance << endl;
cout << "\nEnter amount to deposit: ";
cin >> amt;
balance = (balance + amt);
cout << "\nAmount depositted successfully!" << endl;
cout <<"\nYOUR CURRENT BALANCE: " << balance;
getch();
customer_actions();
return balance;
}
"customer_actions()" being the main menu for the customers, when I go back on that screen and select the option to check balance, it displays as ZERO. Which means the values didn't get updated from the previous function. Here's my header file which consists of the class file:
#ifndef bank
#define bank
using namespace std;
class bankAccount
{
public:
int accNo;
int password;
double balance;
double withdrawamt;
double depositamt;
char name[20];
char address[40];
char username[10];
public:
double checkbalance();
double deposit();
double withdraw();
public:
bankAccount()
{
balance = 0; // Is this the reason?
}
};
#endif
I'm thinking, when the program switches from one menu to the other, the values get reset-ed. Any suggestions, dear folks?
Thanks in advance!
CUSTOMER_ACTIONS:
int customer_actions()
{
bankAccount b;
int cust_selection;
system("cls");
cout << " ----------------------------------------------------------------------- \n";
cout << "| Customer Menu | \n";
cout << " ----------------- ----------------- ----------------- ----------------- \n";
cout << " Please Select option to continue: \n" << endl << endl;
cout << "1) Check balance : Press 1" << endl;
cout << "2) Withdraw Cash : Press 2" << endl;
cout << "3) Deposit Cash : Press 3" << endl;
cout << "4) Transfer Cash : Press 4" << endl;
cout << "5) Return home : Press 5" << endl;
cout << "\nEnter option: ";
cin >> cust_selection;
switch(cust_selection)
{
case 1: b.checkbalance(); break;
case 2: b.withdraw(); break;
case 3: b.deposit(); break;
case 4: break;
case 5: main(); break;
}
}
Your problem (from what I can see) is that you are trying to create an infinite loop where the user can keep pressing making changes on the menu until they exit. However you are going about this by calling customer_actions() from within the deposit function.
Try creating an infinite loop in an outer method, then returning from deposit without calling customer_actions().
Following OP edit
Try this:
int main(...)
{
int result = 0;
while(result == 0)
{
result = customer_actions();
}
}
Now change the switch statement in customer_actions to be like this:
switch(cust_selection)
{
case 1: b.checkbalance(); break;
case 2: b.withdraw(); break;
case 3: b.deposit(); break;
case 4: break;
case 5: return 1; // This is the change
}
return 0;
The bank account b you declare in customer_action is just valid in function scope.
In addition:
customer_action manages given accounts by their interface, accounts should not "manage" customer_action (in your case, don't call it from deposit)
You could easily get a stack overflow the way you coded that.
Generally speaking you should try to avoid mixing up model (your accounts) view (the output) and controller (user input) - related code.
Create clean interfaces and call in in a structured manner.
In addition:
I first read your bold put question and afterwards attended the code.
The first thing I did then was was trying to find if there is local redefinition of double balance. There is none, but I should not even have had to do so, because there are means to avoid side-effects on instance member variables like balance.
Foremost - make them private, not public.
Then:
use a prefix like m_ so m_balance that would be or even m_dblBalance to indicate the type
or prefix all private variables with a _, so _balance that would be
and/or emphasize each usage of instance member vars by prefixing them with a redundant this->
Personally speaking I dislike 1. but use 2. for instance variables.
There are many more design and implementation issues, eg. I would discourage char[] for strings and recommend using std::string, but maybe you start by just ending the marriage of deposit() and customer_actions() you blessed.
I have been working on a trivial assignment to get used to coding. I am designing an ATM machine and at the moment it is composed of 2 classes:
BankAccount.cpp
Constructor for different types of account
Only has balance as a member
Transaction.cpp
Performs a method on the BankAccount (i.e make deposit, make withdrawl & get balance)
Problem: BankAccount is automatically initialized to a balance of 10 which is undesired. So for example, if I made a checking account and chose to deposit $10, balance would print out $20.
//BankAccount.h
//This class will simply take in a bank account
//with a balance, other classes will use a bank account object
//such as saving/checkings and perform operations on the
//balance
#ifndef BANK_ACCOUNT_H
#define BANK_ACCOUNT_H
class BankAccount {
private:
float balance;
public:
BankAccount ();
float getBalance ();
void makeDeposit ();
void makeWithdrawl ();
};
#endif
//BankAccount.cpp
#include "BankAccount.h"
#include <iostream> //remove once done *not to self
using namespace std; //remove once done *note to self
BankAccount::BankAccount() {
balance = 0.00;
}
float BankAccount::getBalance() {
return balance;
}
void BankAccount::makeDeposit() {
cout << "How much would you like to deposit: ";
float deposit_value;
cin >> deposit_value;
balance += deposit_value;
}
void BankAccount::makeWithdrawl() {
cout << "How much would you like to withdrawl: ";
float withdrawl_value;
cin >> withdrawl_value;
balance -= withdrawl_value;
}
//Transaction.h
#ifndef TRANSACTION_H
#define TRANSACTION_H
class Transaction {
private:
BankAccount m_bao;
public:
Transaction(BankAccount&);
void displayOptions();
void printReciept();
};
#endif
//Transaction.cpp
#include "BankAccount.h"
#include "Transaction.h"
#include <iostream>
using namespace std;
Transaction::Transaction(BankAccount& bao) {
m_bao = bao;
}
void Transaction::displayOptions() {
cout << "\nPlease make a choice\n\n";
cout << "1: Make Deposit\n";
cout << "2: Make Withdrawl\n";
cout << "3: Check Balance\n";
int choice;
cin >> choice;
switch (choice) {
case 1:
m_bao.makeDeposit();
break;
case 2:
m_bao.makeWithdrawl();
break;
case 3:
m_bao.getBalance();
break;
}
}
void Transaction::printReciept() {
cout << "Current balance is now: " << m_bao.getBalance() + '\n';
}
int main () {
BankAccount checking;
Transaction q(checking);
q.displayOptions();
q.printReciept();
}
I am sure the answer is right in front of my eyes, but my brain is just fried right now. I will continue to look for the solutions and let you guys know if my problem has been solved yet.
[EDIT]
Alright, now I am trying to make it so that the customer can choose to perform transactions on either Checking or Savings account. Currently I got it looking like this in my main():
int main () {
BankAccount checking(0.00);
BankAccount savings(0.00);
Transaction c(checking);
Transaction s(savings);
for(int i = 0; i < 10 ; i++) {
cout << "Make an option" << endl;
cout << "1. Checking " << endl;
cout << "2. Savings" << endl;
int choice;
cin >> choice;
if (choice == 1) {
c.prompt();
c.printReciept();
}
else {
s.prompt();
s.printReciept();
}
}
}
It works fine, but I would like to make this process more OOP-alized, if that makes sense :)
One option I was trying to look into was making a prompt function which would belong to Transaction.cpp. This would do everything that is done in main, except initializing the objects of course.
Your problem is this line:
cout << "Current balance is now: " << m_bao.getBalance() + '\n';
Which the compiler sees as:
cout << "Current balance is now: " << (m_bao.getBalance() + '\n');
'\n' is 10 as an int, so you get this:
cout << "Current balance is now: " << (m_bao.getBalance() + 10);
You probably meant to do this:
cout << "Current balance is now: " << m_bao.getBalance() << '\n';
Remember that in C++, + almost always means "add these two numbers".