I am working on a simple hotel reservsation Project but I couldn't print the customer name and payment type after entering their names'. Here is the piece of code that I typed.
cout << "Please enter the customer name: " << endl;
cin >> customername1;
cin.getline(customername1,100,'\n');
fflush(stdin);
cout << "Please enter the payment type: " << endl;
cin >> paymenttype;
cin.getline(paymenttype,100,'\n');
fflush(stdin);
cout << "How many days would you like to stay: " << endl;
cin >> days;
room_income = days * 30;
count_room++ ;
count_customer1++ ;
customer_name1[single]= new char [strlen(customername1)+1];
strcpy(customer_name1[single],customername1);
payment_type[single]= new char [strlen(paymenttype)+1];
strcpy(payment_type[single],paymenttype);
cout << "Room number : " << single << endl<< endl;
cout << "Customer name : "<< customer_name1 << endl << endl;
cout << "Payment type : "<< payment_type << endl<< endl;
cout << "Number of day for accomodation :"<< days << endl<< endl;
cout << "Income for this room : "<< room_income<< endl<< endl;
Some random numbers and letters are displayed for the customer name and payment type. How can I write them properly?
For the customer name and payment type, you try to print the array, instead of one element. Since the array is basically just a pointer to the first element, you will get a 'random number', which is the memory address.
Try:
cout << "Customer name : "<< customername1 << endl << endl;
cout << "Payment type : "<< paymenttype << endl<< endl;
And when you've got that working, follow the comments and look into std::string and vectors...
Related
I am a beginner in C++ programming. I need to create a program for a movie reservation system. I have done it until halfway through, but I am stuck at selecting the seats. I need to program it like the user must select a seat. The user can select seats for about 10 times in a loop. After selecting it, all the data should be stored in the text file, and in the line which I have stored the information about the particular movie. I am really hoping for a good suggestion to this problem. This is how I arrange the text file:
Avengers|1600|40|16
LoST World|3200|40|16
Titanic|6897|87|16
Avengers|1600|40|16
#include<iostream>
#include<cmath>
#include<fstream>
using namespace std;
display_all()
{
char name [25];
char hours[25];
char seats[25];
fstream movie;
movie.open("Movies.txt",ios::in);
int count = 0;
string line;
ifstream file("Movies.txt");
while (getline(file, line))
count++;
int i = 0;
while(!movie.eof(), i++ , i<= count)
{
movie.getline(name,25,'|');
movie.getline(seats,25,'|');
movie.getline(hours,25);
cout<< "(" << i << ") " << name << "\n ";
}
return 0;
}
main()
{
char name [25];
char hours[25];
char seats[25];
char price[25];
char seats1[25];
char seats2[25];
char seats3[25];
char seats4[25];
char seats5[25];
char response = 'Y';
string customer, phone_number;
fstream movie;
movie.open("Movies.txt",ios::in);
char selection_role ;
int selection_movie ;
cout << " *************************************************************** " << endl;
cout << " MOVIE TICKET RESERVATION SYSTEM (MTRS) " << endl;
cout << " *************************************************************** " << endl;
cout << endl << endl ;
cout << " Please specify the selection : (1) CUSTOMER" << endl;
cout << " (2) MANAGEMENT" << endl;
cout << endl;
cout << " INPUT : ";
cin >> selection_role;
if (selection_role == 1);
cout << endl;
cout << "MOVIES AVAILABLE : ";
display_all();
cout << endl;
cout << "Please select a movie: ";
cin >> selection_movie ;
int i = 0;
while(i++ , i <= selection_movie)
{
movie.getline(name,25,'|');
movie.getline(seats,25,'|');
movie.getline(hours,25,'|');
movie.getline(price,25,'|');
}
cout << endl;
cout << "======You have selected " << name <<".=========" <<endl;
cout << endl;
cout << "Movie hours: " << seats << endl;
cout << "Number of seats available: " << hours << endl;
cout << " SCREEN "<<endl << endl;
cout << "1 2 3 4 5 6 7 8" <<endl << endl;
cout << "9 10 11 12 13 14 15 16"<<endl << endl;
cout << "17 18 19 20 21 22 23 24"<<endl << endl;;
cout << "25 26 27 28 29 30 31 32"<<endl << endl;
cout << "Enter the number of seat you would like to book for: ";
cin >> seats1 ;
cout << endl;
cout << "Price for the ticket: RM " << price << endl << endl;
cout << "Enter you name: " ;
cin >> customer ;
cout << "Enter your phone number: ";
cin >> phone_number;
cout << endl;
cout << " MOVIE TICKET " << endl;
cout << "============================" << endl;
cout << "Name :" << customer << endl;
cout << "Handphone number :" << phone_number <<endl;
cout << "Movie selected :" << name << endl;
cout << "Seat selected :" << seats1 <<"," << seats2 << endl;
cout << "Price for the movie(RM):" << price << endl;
}
i'm trying to set up a simple flight booking system program, but my second bit of cin code is not calling for input when i run the program. Unlike the initial cin that requires your name initially. the program just runs and returns 0. I'm a beginner at c++ and i know this is a simple fix so please be understanding . Thank you any guidance will be greatly appreciated.
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
int name;
int Seatnumber;
int optionnumber = 1-5 ;
std::string out_string;
std::stringstream ss;
ss << optionnumber;
out_string = ss.str();
cout << "Welcome to the COS1511 Flight Booking System" "\n" << endl;
cout << "Enter full name : " << endl;
cin >> name ; "\n";
cout << "\n" "The Available travel times for flights are:" << endl;
cout << " Depart Arrive" << endl;
cout << "1. 7.00 9.30" << endl;
cout << "2. 9.00 11.30" << endl;
cout << "3. 11.00 13.30" << endl;
cout << "4. 13.00 15.30" << endl;
cout << "5. 15.00 17.30" << endl;
cout << "Choose the time by entering the option number from the displayed list : " << endl;
cin >> optionnumber ;
if (optionnumber == 1-5){
cout << "\n" "The available seats for are as follows " << endl;
}
else
cout << "Incorrect option! Please Choose from 1-5 " << endl;
cout << "First Class(1920)" << endl;
cout << "|A1||A2||A3|----|A4||A5||A6|" << endl;
cout << "|B1||B2||B3|----|B4||B5||B6|" << endl;
cout << "|C1||C2||C3|----|C4||C5||C6|" << endl;
cout << "|D1||D2||D3|----|D4||D5||D6|" << endl;
cout << "| Economy Class(1600)" << endl;
cout << "|E1||E2||E3|----|E4||E5||E6|" << endl;
cout << "|F1||F2||F3|----|F4||F5||F6|" << endl;
cout << "|G1||G2||G3|----|G4||G5||G6|" << endl;
cout << "|H1||H2||H3|----|H4||H5||H6|" << endl;
cout << "|I1||I2|" << endl;
cout << "Please Key in a seat number to choose a seat(eg: A2)" << endl;
cin >> Seatnumber;
}
prompt the user to enter their name.
Then display a menu showing the available times for the flight.
the user can choose a preferred departure time(option 1-5)
the option selected should be validated for 1-5
if the user entered the correct time a seating arrangement for that particular flight time should be displayed to the next user for the user to choose a seat.
Warning
int optionnumber = 1-5 ;
does
int optionnumber = -4 ;
and
if (optionnumber == 1-5){
does
if (optionnumber == -4){
but you wanted if ((optionnumber >= 1) && (optionnumber <= 5))
if the user entered the correct time a seating arrangement for that particular flight time should be displayed to the next user for the user to choose a seat.
No, whatever the result of the test above you continue and write "First Class(1920)" etc so even when the choice is invalid
in
cin >> name ; "\n";
what did you expect about the "\n" ?
I encourage you to check the read success, currently if the user does not enter an integer you do not know that
But are you sure the name must be an integer ? Probably it must be s string
out_string is unused, it can be removed
Visibly Seatnumber is not an int but a string (A1 ...)
you probably want to loop until a valid time is enter, also fixing the other problems a solution can be :
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
string Seatnumber;
int optionnumber;
cout << "Welcome to the COS1511 Flight Booking System" "\n" << endl;
cout << "Enter full name : " << endl;
if (!(cin >> name))
// EOF (input from a file)
return -1;
cout << "\n" "The Available travel times for flights are:" << endl;
cout << " Depart Arrive" << endl;
cout << "1. 7.00 9.30" << endl;
cout << "2. 9.00 11.30" << endl;
cout << "3. 11.00 13.30" << endl;
cout << "4. 13.00 15.30" << endl;
cout << "5. 15.00 17.30" << endl;
cout << "Choose the time by entering the option number from the displayed list : " << endl;
for (;;) {
if (!(cin >> optionnumber)) {
// not an int
cin.clear(); // clear error
string s;
// flush invalid input
if (!(cin >> s))
// EOF (input from a file)
return -1;
}
else if ((optionnumber >= 1) && (optionnumber <= 5))
// valid choice
break;
cout << "Incorrect option! Please Choose from 1-5 " << endl;
}
cout << "\n" "The available seats for are as follows " << endl;
cout << "First Class(1920)" << endl;
cout << "|A1||A2||A3|----|A4||A5||A6|" << endl;
cout << "|B1||B2||B3|----|B4||B5||B6|" << endl;
cout << "|C1||C2||C3|----|C4||C5||C6|" << endl;
cout << "|D1||D2||D3|----|D4||D5||D6|" << endl;
cout << "| Economy Class(1600)" << endl;
cout << "|E1||E2||E3|----|E4||E5||E6|" << endl;
cout << "|F1||F2||F3|----|F4||F5||F6|" << endl;
cout << "|G1||G2||G3|----|G4||G5||G6|" << endl;
cout << "|H1||H2||H3|----|H4||H5||H6|" << endl;
cout << "|I1||I2|" << endl;
cout << "Please Key in a seat number to choose a seat(eg: A2)" << endl;
cin >> Seatnumber;
return 0;
}
Compilation and execution :
pi#raspberrypi:/tmp $ g++ -pedantic -Wextra -Wall cc.cc
pi#raspberrypi:/tmp $ ./a.out
Welcome to the COS1511 Flight Booking System
Enter full name :
bruno
The Available travel times for flights are:
Depart Arrive
1. 7.00 9.30
2. 9.00 11.30
3. 11.00 13.30
4. 13.00 15.30
5. 15.00 17.30
Choose the time by entering the option number from the displayed list :
aze
Incorrect option! Please Choose from 1-5
7
Incorrect option! Please Choose from 1-5
2
The available seats for are as follows
First Class(1920)
|A1||A2||A3|----|A4||A5||A6|
|B1||B2||B3|----|B4||B5||B6|
|C1||C2||C3|----|C4||C5||C6|
|D1||D2||D3|----|D4||D5||D6|
| Economy Class(1600)
|E1||E2||E3|----|E4||E5||E6|
|F1||F2||F3|----|F4||F5||F6|
|G1||G2||G3|----|G4||G5||G6|
|H1||H2||H3|----|H4||H5||H6|
|I1||I2|
Please Key in a seat number to choose a seat(eg: A2)
qsd
pi#raspberrypi:/tmp $
Note entering the name with cin >> name does not allow it to contain several names separated by a space, to allow composed name getline can be used
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 5 years ago.
Improve this question
// STRUCTURE FOR ADD NEW DRIVER'S RECORD
struct driver
{
int long reg;
string name;
string fhname;
string peraddress;
string pesentaddress;
string dob;
string cnic;
string qualification;
string occupation;
string phone;
string mobile;
string licnum;
string email;
string city;
string gender;
string province;
string lictype;
string vitype;
}e[100];
// FUNCTION FOR ADD NEW RECORD OF DRIVER
void add_driver_record()
{
system("CLS");
char option;
fstream driverTextFile, addrecord;
addrecord.open("rcd.data", ios::out | ios::app | ios::binary);
cout << "Do you Want to Enter New Record (Y/N) ";
option = _getch();
system("cls");
if (option == 'Y' || option == 'y')
{
for (int i = 1; i == 1; i++)
{
while (option != 'N')
{
cout << "\t\t Driver Information " << endl;
cout << "\t\t++++++++++++++++++++" << endl;
cout << "\t\tEnter Token No :";
cin >> e[i].reg;
cout << "\t\tEnter License Number :";
cin.ignore();
getline(cin, e[i].licnum);
cout << "\t\tEnter Full Name :";
getline(cin, e[i].name);
cout << "\t\tEnter Father/Husband Name :";
getline(cin, e[i].fhname);
cout << "\t\tEnter Permanent Address :";
getline(cin, e[i].peraddress);
cout << "\t\tEnter Present Address :";
getline(cin, e[i].pesentaddress);
cout << "\t\tEnter Date of Birth :";
getline(cin, e[i].dob);
cout << "\t\tEnter CNI :";
getline(cin, e[i].cnic);
cout << "\t\tEnter city :";
getline(cin, e[i].city);
cout << "\t\tEnter Province :";
getline(cin, e[i].province);
cout << "\t\tEnter Gender :";
cin >> e[i].gender;
cout << "\t\tEnter Occupation :";
cin >> e[i].occupation;
cout << "\t\tEnter Qaulification :";
cin.ignore();
getline(cin, e[i].qualification);
cout << "\t\tEnter E-mail :";
cin.ignore();
getline(cin, e[i].email);
cout << "\t\tEnter Phone No :";
cin >> e[i].phone;
cout << "\t\tEnter Mobile No :";
cin >> e[i].mobile;
cout << "\t\tEnter License Type :";
cin.ignore();
getline(cin, e[i].lictype);
cout << "\t\tEnter Vehicle Type :";
cin.ignore();
getline(cin, e[i].vitype);
addrecord.write((char *)&e, sizeof(driver));
driverTextFile << "========================================================\n";
driverTextFile << "\t\t Driver Information " << endl;
driverTextFile << "\t\t+++++++++++++++++++++++" << endl;
driverTextFile << "\t\t Token No :" << e[i].reg << endl;
driverTextFile << "\t\t License Number :" << e[i].licnum << endl;
driverTextFile << "\t\t Full Name :" << e[i].name << endl;
driverTextFile << "\t\t Father/Husband Name :" << e[i].fhname << endl;
driverTextFile << "\t\t Permanent Address :" << e[i].peraddress << endl;
driverTextFile << "\t\t Present Address :" << e[i].pesentaddress << endl;
driverTextFile << "\t\t Date of Birth :" << e[i].dob << endl;
driverTextFile << "\t\t CNIC :" << e[i].cnic << endl;
driverTextFile << "\t\t City :" << e[i].city << endl;
driverTextFile << "\t\t Province :" << e[i].province << endl;
driverTextFile << "\t\t Gender :" << e[i].gender << endl;
driverTextFile << "\t\t Occupation :" << e[i].occupation << endl;
driverTextFile << "\t\t Qaulification :" << e[i].qualification << endl;
driverTextFile << "\t\t E-mail :" << e[i].email << endl;
driverTextFile << "\t\t Phone No :" << e[i].phone << endl;
driverTextFile << "\t\t Mobile No :" << e[i].mobile << endl;
driverTextFile << "\t\t Enter License Type :" << e[i].lictype << endl;
driverTextFile << "\t\t Enter Vehicle Type :" << e[i].vitype << endl;
driverTextFile << "========================================================\n";
addrecord.close();
cout << "Do you Want to Enter New Record (Y/N) ";
cin >> option;
switch (option) {
case 'Y':
case 'y':
add_driver_record();
break;
case 'N':
case 'n':
exit(1);
break;
default:
printf("Please select suitable option \n");
add_driver_record();
break;
}
} // while Loop ends
}// for loop ends
} // if condition ends
else if (option == 'N' || option == 'n')
{
printf("Please Select Suitable Option \n");
}
}
// FUNCTION FOR DISPLAY ALL RECORDS OF DRIVER'S
void display_all_driver()
{
system("CLS");
system("color f5");
fstream myfile, driverTextFile;
myfile.open("rcd.data", ios::in | ios::out | ios::binary);
int idx = 1;
while (idx == 1)
{
myfile.read((char*)&e[idx].reg, sizeof(e[idx].reg));
myfile.read((char*)&e[idx].licnum, sizeof(e[idx].licnum));
myfile.read((char*)&e[idx].name, sizeof(e[idx].name));
myfile.read((char*)&e[idx].fhname, sizeof(e[idx].fhname));
myfile.read((char*)&e[idx].peraddress, sizeof(e[idx].peraddress));
myfile.read((char*)&e[idx].pesentaddress, sizeof(e[idx].pesentaddress));
myfile.read((char*)&e[idx].dob, sizeof(e[idx].dob));
myfile.read((char*)&e[idx].cnic, sizeof(e[idx].cnic));
myfile.read((char*)&e[idx].city, sizeof(e[idx].city));
myfile.read((char*)&e[idx].province, sizeof(e[idx].province));
myfile.read((char*)&e[idx].gender, sizeof(e[idx].gender));
myfile.read((char*)&e[idx].occupation, sizeof(e[idx].occupation));
myfile.read((char*)&e[idx].qualification, sizeof(e[idx].qualification));
myfile.read((char*)&e[idx].email, sizeof(e[idx].email));
myfile.read((char*)&e[idx].phone, sizeof(e[idx].phone));
myfile.read((char*)&e[idx].mobile, sizeof(e[idx].mobile));
myfile.read((char*)&e[idx].lictype, sizeof(e[idx].lictype));
myfile.read((char*)&e[idx].vitype, sizeof(e[idx].vitype));
cout << "========================================================\n";
cout << "\t\t Driver Information " << endl;
cout << "\t\t+++++++++++++++++++++++" << endl;
cout << "\t\t Token No :" << e[idx].reg << endl;
cout << "\t\t License Number :" << e[idx].licnum << endl;
cout << "\t\t Full Name :" << e[idx].name << endl;
cout << "\t\t Father/Husband Name :" << e[idx].fhname << endl;
cout << "\t\t Permanent Address :" << e[idx].peraddress << endl;
cout << "\t\t Present Address :" << e[idx].pesentaddress << endl;
cout << "\t\t Date of Birth :" << e[idx].dob << endl;
cout << "\t\t CNIC :" << e[idx].cnic << endl;
cout << "\t\t City :" << e[idx].city << endl;
cout << "\t\t Province :" << e[idx].province << endl;
cout << "\t\t Gender :" << e[idx].gender << endl;
cout << "\t\t Occupation :" << e[idx].occupation << endl;
cout << "\t\t Qaulification :" << e[idx].qualification << endl;
cout << "\t\t E-mail :" << e[idx].email << endl;
cout << "\t\t Phone No :" << e[idx].phone << endl;
cout << "\t\t Mobile No :" << e[idx].mobile << endl;
cout << "\t\t Enter License Type :" << e[idx].lictype << endl;
cout << "\t\t Enter Vehicle Type :" << e[idx].vitype << endl;
cout << "========================================================\n";
idx++;
}
myfile.close();
}
I am so sorry if somebody else had already asked this question. But I am unable to rectify the problem that I "can not" read the binary file on console. Let me tell what my code is doing. I took structure with bunch of attribute and taking the input in that and saving that data in Binary file. And i want to read that in second method. But I am completely unable to read the data. This is my code please provide me solution. I have already tried several ways but I am still unable. Your help will save me semester project.
Your struct driver consists of std::strings, which internally represent the actual content trough a pointer to dynamically allocated memory. When you write such a struct (or an array of such structs) as plain bytes to a file, you actually write out "memory addresses" and not the actual content of the strings. So you will loose the actual content, and reading in the file will give you plain garbage.
Try the following program illustrating this:
struct TestStruct {
string testString;
};
int main(){
TestStruct ts;
cout << "sizeof ts with empty string: " << sizeof(ts) << endl;
ts.testString = "Will not get larger, right?";
cout << "sizeof ts with string set to some content: " << sizeof(ts) << endl;
}
Output:
sizeof ts with empty string: 24
sizeof ts with string set to some content: 24
Do you have read permissions for the file? That is my first thought that it is a permissions issue.
I am having trouble getting my while loop to run. I am a very beginner coder and I have made many attempts with no success to make this work. I need help PLEASE!! Please be very specific and with laymen terms with your help since I am new to this.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string mystr1;
string mystr2;
cout << "Please provide your first and last name" << endl;
getline (cin, mystr1);
cout << endl;
cout << "Please provide your shipping address" << endl;
getline (cin, mystr2);
cout << endl;
cout << "Hello " << mystr1 << " and welcome to Faulk Couture
Handbags Boutique" << endl; // prints Hello and welcome to Faulk
Couture Handbags Boutique
cout << endl;
cout << "We have a variety of specialty and fashionable handbags to
select from. Please see below for the available products and their
descriptions." << endl;
cout << endl;
cout << "Product 1: Crosby Carryall in black priced at $395. This
sophisticated and spacious Crosby Carryall is a work-to-weekend favorite
and is finished with " << endl;
cout << "bound leather edges, a detachable leather strap and petite
brass turnlocks securing its two zippered compartments."<< endl;
cout << endl;
cout << "Product 2: Prairie satchel with chain nude priced at $450.
Crafted in lightweight pebble leather with a bit of sheen, this
gracefully curved shape distills" <<endl;
cout << "the satchel to its purest form. The simple design is finished
with a slender strap and an elegant chain detail that detaches for a
different look." << endl;
cout << endl;
cout << "Product 3: Faulk Swagger 20 brown priced at $325. This
Statement belting with double-turnlock hardware is one of our most
popular designs with a little bit of “swagger.” "<< endl;
cout << "Named for a bold, brass-trimmed Bonnie Cashin design from 1967,
this very modern carryall in refined pebble leather comes finished with
a detachable strap for crossbody wear." << endl;
cout << endl;
cout << "Product 4: Zip top tote in brown priced at $285. This
sophisticated and light weight in signature canvas with hand-finished
leather trim, this aptly named tote is made for one-the-go ease." <<
endl;
cout << "A modern, flared shape and oversized strap anchors add playful
proportions to its spacious, brightly lined design." << endl;
cout << endl;
cout << "Product 5: Wristlet 24 priced at $175. This striking, feminine
design in polished pebble leather has space enough for a tablet and an
elegant chain that converts it from wristlet to top handle." << endl;
cout << "A dog-leash clip on the strap and an embossed hangtag charm
finish it with signature Faulk Couture Style." << endl;
cout << endl;
double cost, total, amount;
int product;
cout << "Please enter the product number for your bag choice" << endl;
cin >> product;
cout <<"The respective price for this bag is: " << endl;
cin>>cost;
cout<<"Please enter the quantity you would like to purchase for this
bag choice" << endl;
cin>>amount;
total = cost*amount;
cout <<"Your total purchase price for " <<amount<< " qty of product
number " <<product<< " is " <<total<<"."<< endl;
int choice=1;
while (choice==1);
{
cout << "To purchase another bag, please enter 1 (anything else to
quit)" << endl;
cin >> choice;
cout << "Please enter the product number for your next bag choice" <<
endl;
cin >> product;
cout << "The respective price for this bag is: " << endl;
cin >> cost;
cout << "Please enter the quantity you would like to purchase for this
bag choice" << endl;
cin >> amount;
total = cost*amount;
cout <<"Your total purchase price for " <<amount<< " qty of product
number " <<product<< " is " <<total<<"."<< endl;
}
cout << endl;
return 0;
}
You're ending your while loop with a semicolon that's why it isn't entering the while loop
Here is the while loop without the semicolon
while (choice==1) {
cout << "To purchase another bag, please enter 1 (anything else to
quit)" << endl;
cin >> choice;
cout << "Please enter the product number for your next bag choice" <<
endl;
cin >> product;
cout << "The respective price for this bag is: " << endl;
cin >> cost;
cout << "Please enter the quantity you would like to purchase for this
bag choice" << endl;
cin >> amount;
total = cost*amount;
cout <<"Your total purchase price for " <<amount<< " qty of product
number " <<product<< " is " <<total<<"."<< endl;
}
I'm sure I'm overlooking something very basic. If anyone could offer some help, or point me to a relevant thread, I'd be extremely grateful. In addition, if you need more of my code, I'd be happy to provide it.
I have a simple Bank Account program. In the main() class, I have the following function:
void deposit(const Bank bank, ofstream &outfile)
{
int requested_account, index;
double amount_to_deposit;
const Account *account;
cout << endl << "Enter the account number: "; //prompt for account number
cin >> requested_account;
index = findAccount(bank, requested_account);
if (index == -1) //invalid account
{
outfile << endl << "Transaction Requested: Deposit" << endl;
outfile << "Error: Account number " << requested_account << " does not exist" << endl;
}
else //valid account
{
cout << "Enter amount to deposit: "; //prompt for amount to deposit
cin >> amount_to_deposit;
if (amount_to_deposit <= 0.00) //invalid amount to deposit
{
outfile << endl << "Transaction Requested: Deposit" << endl;
outfile << "Account Number: " << requested_account << endl;
outfile << "Error: " << amount_to_deposit << " is an invalid amount" << endl;
}
else //valid deposit
{
outfile << endl << "Transaction Requested: Deposit" << endl;
outfile << "Account Number: " << requested_account << endl;
outfile << "Old Balance: $" << bank.getAccount(index).getAccountBalance() << endl;
outfile << "Amount to Deposit: $" << amount_to_deposit << endl;
bank.getAccount(index).makeDeposit(&amount_to_deposit); //make the deposit
outfile << "New Balance: $" << bank.getAccount(index).getAccountBalance() << endl;
}
}
return;
} // close deposit()
The issue is with the makeDeposit(&amount_to_deposit). The output file shows:
Transaction Requested: Deposit
Account Number: 1234
Old Balance: $1000.00
Amount to Deposit: $168.00
New Balance: $1000.00
Within the Account class, here is the function makeDeposit:
void Account::makeDeposit(double *deposit)
{
cout << "Account balance: " << accountBalance << endl;
accountBalance += (*deposit);
cout << "Running makeDeposit of " << *deposit << endl;
cout << "Account balance: " << accountBalance << endl;
return;
}
The console output, from the cout calls, is:
Enter the account number: 1234
Enter amount to deposit: 169
Account balance: 1000
Running makeDeposit of 169
Account balance: 1169
So, within the makeDeposit() function, it's correctly updating the accountBalance variable. But once the function ends, it reverts back to the initial value.
I'm sure that this is elementary for more experienced programmers. Your insight would be very much appreciated.
Thanks,
Adam
It's because you are passing Bank by value not by reference and as const. Changing
void deposit(const Bank bank, ofstream &outfile)
to
void deposit(Bank& bank, ofstream &outfile)
should fix it