I am currently working on just a miscellaneous Theater Seating program! I am just starting to progress through C++, but I can't figure out this error I keep on getting in my coding. What I am currently trying to code is a way to input "*"s character by character, ultimately to help me build a function to display a seating chart for a movie theater. I am pretty close to finishing the function called readSeating... Though there are some clear problems with it. Usually I can debug my programs pretty efficiently, but I just can't figure this one out. Please take a look at my code, get a feel for my program, and let me have any ideas. Please keep answers somewhat simple; again, I'm no master at C++
P.S.
Sorry for showing the whole code... This is my first post... I didn't really know what to include, what not to; and I wanted people to be able to get a feel for the program by even testing it themselves... Thanks again!
/*
* This is a program built by a team of students
* to help local movie theaters sell tickets
*
* File: main.cpp
* Author(s):
*
*
* Created on April 15, 2013, 11:10 AM
*/
#include <cstdlib>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;
//Function Prototypes
void Title_Card();
void seating_prices();
void seating_chart();
void pick_seating();
void purchase_history();
void quit();
void update_file();
void Show_Menu();
void is_digit(int&);
void Admin_Menu();
void edit_seating_prices();
void edit_seating();
void quit_to_original();
void purchase_history_admin();
int readSeating (char, vector<char>&); //Reads SeatingChart.txt
int readPrices(string, vector<double>&); //Reads SeatingPrices.txt
vector<double> prices(15); //For SeatPrices.txt
vector<char> seating(450); //For SeatingChart.txt
//Actual Program
int main() {
Title_Card(); //Calls Title Page
readSeating("SeatingChart.txt", seating); //Reads SeatingChart.txt
readPrices("SeatPrices.txt", prices); //Reads SeatPrices.txt
Show_Menu(); //Shows Introductory Menu
system ("pause");
return 0;
}
//**************************************************************
// Definition of the ShowMenu function. Shows introductory menu*
// and controls where user ends up in the program. *
//**************************************************************
void Show_Menu() {
int choice;
string password;
cout << "Welcome to the our theater program! Made for a person" << endl;
cout << "who is looking for seating, purchasing a ticket, and" << endl;
cout << "searching for other miscellaneous things... We hope" << endl;
cout << "you enjoy the program!" << endl << endl;
cout << "Below is a list of options the user can choose from:" << endl << endl;
cout << "1.\tSeating Prices" << endl;
cout << "2.\tPick Seating" << endl;
cout << "3.\tPurchase History" << endl;
cout << "4.\tQuit" << endl << endl;
cout << "Enter a choice... (1-4): ";
cin >> choice;
//Secret Administrator Access
if (choice == 219){
system ("cls");
cout << "Enter the administrator password: ";
cin >> password;
if (password == "sterilegorilla")
Admin_Menu();
else {
system ("cls");
Show_Menu();
}
}
is_digit(choice);//is_digit function doesn't work atm
while (choice < 1 || choice > 4){
cout << endl << "You have entered an invalid choice!" << endl;
cout << "Enter a choice... (1-4): ";
cin >> choice;
is_digit(choice); //is_digit function doesn't work atm
if (choice == 219){
system ("cls");
cout << "Enter the administrator password: ";
cin >> password;
if (password == "sterilegorilla")
Admin_Menu();
else {
system ("cls");
Show_Menu();
}
}
}
switch (choice) {
case 1:
seating_prices();
case 2:
pick_seating();
case 3:
purchase_history();
case 4:
quit();
}
}
//**************************************************************
// Definition of the seating_prices function. Displays to the *
// user, SeatPrices.txt *
//**************************************************************
void seating_prices(){
system ("cls");
cout << "The Current Seating Prices Are:" << endl << endl;
for (int count = 0; count < 4; count++){
cout << " " << setprecision(4) << showpoint << prices[count] << " | Row " << (count + 1) << endl;
}
for (int count = 4; count < prices.size(); count++){
cout << " " << setprecision(3) << showpoint << prices[count] << " | Row " << (count + 1) << endl;
}
cout << endl;
system ("pause");
system ("cls");
Show_Menu();
}
//**************************************************************
// Definition of the pick_seating function. Displays the *
// current seating chart and allows the user to pick their seat*
//**************************************************************
void pick_seating(){ //Not Finished
system ("cls");
int row_choice;
int seat_choice;
cout << "The Current Seating Chart Is:" << endl;
//Display Seating Chart
//Picking your seat
cout << "Enter the row number you would like to be in (1-15): ";
cin >> row_choice;
while (row_choice < 1 || row_choice > 15){
cout << endl << "You have entered an invalid row number!" << endl;
cout << "Enter the row number you would like to be in (1-15): ";
cin >> row_choice;
}
cout << "Enter the seat number you would like to have (1-30): ";
cin >> seat_choice;
while (seat_choice < 1 || seat_choice > 30){
cout << endl << "You have entered an invalid seat number!" << endl;
cout << "Enter the seat number you would like to have (1-30): ";
cin >> seat_choice;
}
cout << "Congratulations! You have picked your seat!" << endl << endl;
system ("pause");
system ("cls");
Show_Menu();
}
//**************************************************************
// Definition of the purchase_history function. Displays the *
// current the total sum of all movie ticket purchases *
//**************************************************************
void purchase_history(){ //Not finished
system ("cls");
system ("pause");
system ("cls");
Show_Menu();
}
//**************************************************************
// Definition of the quit function. Allows the user to quit the*
// program entirely *
//**************************************************************
void quit(){
update_file();
exit(0);
}
//**************************************************************
// Definition of the is_digit function. Designed to make it *
// possible to enter only integers when asked for integers *
//**************************************************************
void is_digit(int & input){ //Not working atm
if (isdigit(input)){
return;
}
if (isalpha(input)){
cout << endl << "You have entered an invalid data type!" << endl;
cout << "Enter a choice... (1-4): ";
cin >> input;
}
if (isspace(input)){
cout << endl << "You have entered an invalid data type!" << endl;
cout << "Enter a choice... (1-4): ";
cin >> input;
}
}
//**************************************************************
// Definition of the update_file function. Designed to update *
// the seating chart upon leaving the pick_seating function *
//**************************************************************
void update_file(){ //Not finished
//This function is supposed to
//Update the seating chart
//upon exit of the pick_seating function
}
//**************************************************************
// Definition of the Admin_Menu function. Displays the *
// administrator-based menu if user knows the code and password*
//**************************************************************
void Admin_Menu(){
system ("cls");
int choice = 0;
cout << "Hello! This is the administrator version";
cout << endl << "of the theatre program; mainly used for editing purposes." << endl << endl;
cout << "Below is a list of options the administrator can choose from:" << endl << endl;
cout << "1.\tEdit Seating Prices" << endl;
cout << "2.\tEdit Seating" << endl;
cout << "3.\tPurchase History" << endl;
cout << "4.\tQuit to Original" << endl;
cout << "5.\tQuit Program" << endl << endl;
cout << "Enter a choice... (1-5): ";
cin >> choice;
while (choice < 1 || choice > 5){
cout << endl << "You have entered an invalid choice!" << endl;
cout << "Enter a choice... (1-5): ";
cin >> choice;
}
switch (choice) {
case 1:
edit_seating_prices();
case 2:
edit_seating();
case 3:
purchase_history_admin();
case 4:
quit_to_original();
case 5:
quit();
}
}
//**************************************************************
// Definition of the edit_seating function. Administrator-only *
// function to edit the seating chart *
//**************************************************************
void edit_seating(){ //Not finished
system ("cls");
system ("pause");
system ("cls");
Admin_Menu();
}
//**************************************************************
// Definition of the edit_seating function. Administrator-only *
// function to edit the prices of the seats. *
// This will in turn, overwrite SeatPrices.txt *
//**************************************************************
void edit_seating_prices(){ //Not finished
system ("cls");
system ("pause");
system ("cls");
Admin_Menu();
}
//**************************************************************
// Definition of the purchase_history_admin function. *
// Administrator-only function designed to show admin the *
// total sum of ticket sales... NOT EDITABLE *
//**************************************************************
void purchase_history_admin(){
system ("cls");
system ("pause");
system ("cls");
Admin_Menu();
}
//**************************************************************
// Definition of the quit_to_original function. Administrator- *
// only function designed to return admin to original program *
//**************************************************************
void quit_to_original(){
system ("cls");
Show_Menu();
}
//**************************************************************
// Definition of the read_file function. Reads SeatPrices.txt *
// and stores the pre-determined prices into a vector named *
// prices. *
//**************************************************************
int readPrices(string myFile, vector<double>& vect) {
//input file
ifstream SeatPrices;
SeatPrices.open(myFile.c_str());
//if file cannot be found
if (!SeatPrices)
cout << "Cannot find the file!" << endl;
for (int index = 0; index < vect.size(); index++){
SeatPrices >> vect[index]; //Reading the file "SeatPrices.txt"
}
SeatPrices.close(); //Closes the file
return 0;
}
//**************************************************************
// Definition of the readSeating function. Reads a text file *
// with a seating chart in it. *
//**************************************************************
int readSeating(char myFile, vector<char>& vect){ //Not EVEN CLOSE TO FINISHING
//input file
ifstream SeatingChart;
SeatingChart.open(myFile);
//if file cannot be found
if (!SeatingChart)
cout << "Cannot find the file!" << endl;
for (int index = 0; index < vect.size(); index++){
SeatingChart >> vect[index]; //Reading the file "SeatingChart.txt"
}
SeatingChart.close(); //Closes the file
return 0;
}
//**************************************************************
// Definition of the Title_Card function. Starts the program *
// with a title card, showing a little introductory title *
//**************************************************************
void Title_Card(){
cout << endl << endl << endl << endl;
cout << "\t\t" << "************************************************\n";
cout << "\t\t" << "* THEATER SEATING! *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "* A program created by a team of three *\n";
cout << "\t\t" << "* students to help small theaters sell *\n";
cout << "\t\t" << "* more tickets *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "* Team of Students: *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "* ************* *\n";
cout << "\t\t" << "* *********** *\n";
cout << "\t\t" << "* ************* *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "************************************************\n";
cout << endl << endl;
system ("pause");
system ("cls");
}
First of all (I think this is what you intended to write)
int readSeating (char, vector<char>&);
should be
int readSeating (char*, vector<char>&);
^^^
expect an lvalue for the first argument. What that means is simply something that it can possibly assign to.
However you are calling it as
readSeating("SeatingChart.txt", seating);
by passing a temporary for the first argument. The complier wants to know that you will not modify it inorder to let you pass a temporary. You can tell it that you wnot modify it by declaring the function like.
int readSeating (const char*, vector<char>&);
^^^^
Same goes for
int readSeating(char myFile, vector<char>& vect)
to
int readSeating(const char* myFile, vector<char>& vect)
I suggest you should go ahead and change them to strings.
int readSeating (string, vector<char>&);
int readSeating(string myFile, vector<char>& vect)
Fixed it for you. A string literal (Ex:"somestring") is of type "char const *". It cannot be treated just as a char.
/*
* This is a program built by a team of students
* to help local movie theaters sell tickets
*
* File: main.cpp
* Author(s):
*
*
* Created on April 15, 2013, 11:10 AM
*/
#include <cstdlib>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;
//Function Prototypes
void Title_Card();
void seating_prices();
void seating_chart();
void pick_seating();
void purchase_history();
void quit();
void update_file();
void Show_Menu();
void is_digit(int&);
void Admin_Menu();
void edit_seating_prices();
void edit_seating();
void quit_to_original();
void purchase_history_admin();
int readSeating (const char*, vector<char>&); //Reads SeatingChart.txt
int readPrices(string, vector<double>&); //Reads SeatingPrices.txt
vector<double> prices(15); //For SeatPrices.txt
vector<char> seating(450); //For SeatingChart.txt
//Actual Program
int main() {
Title_Card(); //Calls Title Page
readSeating("SeatingChart.txt", seating); //Reads SeatingChart.txt
readPrices("SeatPrices.txt", prices); //Reads SeatPrices.txt
Show_Menu(); //Shows Introductory Menu
system ("pause");
return 0;
}
//**************************************************************
// Definition of the ShowMenu function. Shows introductory menu*
// and controls where user ends up in the program. *
//**************************************************************
void Show_Menu() {
int choice;
string password;
cout << "Welcome to the our theater program! Made for a person" << endl;
cout << "who is looking for seating, purchasing a ticket, and" << endl;
cout << "searching for other miscellaneous things... We hope" << endl;
cout << "you enjoy the program!" << endl << endl;
cout << "Below is a list of options the user can choose from:" << endl << endl;
cout << "1.\tSeating Prices" << endl;
cout << "2.\tPick Seating" << endl;
cout << "3.\tPurchase History" << endl;
cout << "4.\tQuit" << endl << endl;
cout << "Enter a choice... (1-4): ";
cin >> choice;
//Secret Administrator Access
if (choice == 219){
system ("cls");
cout << "Enter the administrator password: ";
cin >> password;
if (password == "sterilegorilla")
Admin_Menu();
else {
system ("cls");
Show_Menu();
}
}
is_digit(choice);//is_digit function doesn't work atm
while (choice < 1 || choice > 4){
cout << endl << "You have entered an invalid choice!" << endl;
cout << "Enter a choice... (1-4): ";
cin >> choice;
is_digit(choice); //is_digit function doesn't work atm
if (choice == 219){
system ("cls");
cout << "Enter the administrator password: ";
cin >> password;
if (password == "sterilegorilla")
Admin_Menu();
else {
system ("cls");
Show_Menu();
}
}
}
switch (choice) {
case 1:
seating_prices();
case 2:
pick_seating();
case 3:
purchase_history();
case 4:
quit();
}
}
//**************************************************************
// Definition of the seating_prices function. Displays to the *
// user, SeatPrices.txt *
//**************************************************************
void seating_prices(){
system ("cls");
cout << "The Current Seating Prices Are:" << endl << endl;
for (int count = 0; count < 4; count++){
cout << " " << setprecision(4) << showpoint << prices[count] << " | Row " << (count + 1) << endl;
}
for (int count = 4; count < prices.size(); count++){
cout << " " << setprecision(3) << showpoint << prices[count] << " | Row " << (count + 1) << endl;
}
cout << endl;
system ("pause");
system ("cls");
Show_Menu();
}
//**************************************************************
// Definition of the pick_seating function. Displays the *
// current seating chart and allows the user to pick their seat*
//**************************************************************
void pick_seating(){ //Not Finished
system ("cls");
int row_choice;
int seat_choice;
cout << "The Current Seating Chart Is:" << endl;
//Display Seating Chart
//Picking your seat
cout << "Enter the row number you would like to be in (1-15): ";
cin >> row_choice;
while (row_choice < 1 || row_choice > 15){
cout << endl << "You have entered an invalid row number!" << endl;
cout << "Enter the row number you would like to be in (1-15): ";
cin >> row_choice;
}
cout << "Enter the seat number you would like to have (1-30): ";
cin >> seat_choice;
while (seat_choice < 1 || seat_choice > 30){
cout << endl << "You have entered an invalid seat number!" << endl;
cout << "Enter the seat number you would like to have (1-30): ";
cin >> seat_choice;
}
cout << "Congratulations! You have picked your seat!" << endl << endl;
system ("pause");
system ("cls");
Show_Menu();
}
//**************************************************************
// Definition of the purchase_history function. Displays the *
// current the total sum of all movie ticket purchases *
//**************************************************************
void purchase_history(){ //Not finished
system ("cls");
system ("pause");
system ("cls");
Show_Menu();
}
//**************************************************************
// Definition of the quit function. Allows the user to quit the*
// program entirely *
//**************************************************************
void quit(){
update_file();
exit(0);
}
//**************************************************************
// Definition of the is_digit function. Designed to make it *
// possible to enter only integers when asked for integers *
//**************************************************************
void is_digit(int & input){ //Not working atm
if (isdigit(input)){
return;
}
if (isalpha(input)){
cout << endl << "You have entered an invalid data type!" << endl;
cout << "Enter a choice... (1-4): ";
cin >> input;
}
if (isspace(input)){
cout << endl << "You have entered an invalid data type!" << endl;
cout << "Enter a choice... (1-4): ";
cin >> input;
}
}
//**************************************************************
// Definition of the update_file function. Designed to update *
// the seating chart upon leaving the pick_seating function *
//**************************************************************
void update_file(){ //Not finished
//This function is supposed to
//Update the seating chart
//upon exit of the pick_seating function
}
//**************************************************************
// Definition of the Admin_Menu function. Displays the *
// administrator-based menu if user knows the code and password*
//**************************************************************
void Admin_Menu(){
system ("cls");
int choice = 0;
cout << "Hello! This is the administrator version";
cout << endl << "of the theatre program; mainly used for editing purposes." << endl << endl;
cout << "Below is a list of options the administrator can choose from:" << endl << endl;
cout << "1.\tEdit Seating Prices" << endl;
cout << "2.\tEdit Seating" << endl;
cout << "3.\tPurchase History" << endl;
cout << "4.\tQuit to Original" << endl;
cout << "5.\tQuit Program" << endl << endl;
cout << "Enter a choice... (1-5): ";
cin >> choice;
while (choice < 1 || choice > 5){
cout << endl << "You have entered an invalid choice!" << endl;
cout << "Enter a choice... (1-5): ";
cin >> choice;
}
switch (choice) {
case 1:
edit_seating_prices();
case 2:
edit_seating();
case 3:
purchase_history_admin();
case 4:
quit_to_original();
case 5:
quit();
}
}
//**************************************************************
// Definition of the edit_seating function. Administrator-only *
// function to edit the seating chart *
//**************************************************************
void edit_seating(){ //Not finished
system ("cls");
system ("pause");
system ("cls");
Admin_Menu();
}
//**************************************************************
// Definition of the edit_seating function. Administrator-only *
// function to edit the prices of the seats. *
// This will in turn, overwrite SeatPrices.txt *
//**************************************************************
void edit_seating_prices(){ //Not finished
system ("cls");
system ("pause");
system ("cls");
Admin_Menu();
}
//**************************************************************
// Definition of the purchase_history_admin function. *
// Administrator-only function designed to show admin the *
// total sum of ticket sales... NOT EDITABLE *
//**************************************************************
void purchase_history_admin(){
system ("cls");
system ("pause");
system ("cls");
Admin_Menu();
}
//**************************************************************
// Definition of the quit_to_original function. Administrator- *
// only function designed to return admin to original program *
//**************************************************************
void quit_to_original(){
system ("cls");
Show_Menu();
}
//**************************************************************
// Definition of the read_file function. Reads SeatPrices.txt *
// and stores the pre-determined prices into a vector named *
// prices. *
//**************************************************************
int readPrices(string myFile, vector<double>& vect) {
//input file
ifstream SeatPrices;
SeatPrices.open(myFile.c_str());
//if file cannot be found
if (!SeatPrices)
cout << "Cannot find the file!" << endl;
for (int index = 0; index < vect.size(); index++){
SeatPrices >> vect[index]; //Reading the file "SeatPrices.txt"
}
SeatPrices.close(); //Closes the file
return 0;
}
//**************************************************************
// Definition of the readSeating function. Reads a text file *
// with a seating chart in it. *
//**************************************************************
int readSeating(const char* myFile, vector<char>& vect){ //Not EVEN CLOSE TO FINISHING
//input file
ifstream SeatingChart;
SeatingChart.open(myFile);
//if file cannot be found
if (!SeatingChart)
cout << "Cannot find the file!" << endl;
for (int index = 0; index < vect.size(); index++){
SeatingChart >> vect[index]; //Reading the file "SeatingChart.txt"
}
SeatingChart.close(); //Closes the file
return 0;
}
//**************************************************************
// Definition of the Title_Card function. Starts the program *
// with a title card, showing a little introductory title *
//**************************************************************
void Title_Card(){
cout << endl << endl << endl << endl;
cout << "\t\t" << "************************************************\n";
cout << "\t\t" << "* THEATER SEATING! *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "* A program created by a team of three *\n";
cout << "\t\t" << "* students to help small theaters sell *\n";
cout << "\t\t" << "* more tickets *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "* Team of Students: *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "* ************* *\n";
cout << "\t\t" << "* *********** *\n";
cout << "\t\t" << "* ************* *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "************************************************\n";
cout << endl << endl;
system ("pause");
system ("cls");
}
Related
Assignment:
When you enter an incorrect number of digits( 3 or 5 digits pin number) a
message should display “You have entered the incorrect pin number!!...you
must enter a four digits pin number.”
By showing the message that was display previously the program should
allow you to re-enter the correct number of digits.
The number of attempts should be three times.
When you have entered the correct number of digits a message should display
“Your pin has been accepted!!”
Each time you enter any amount of pin number it must show in asterisk.
Code:
#include <conio.h>
#include <iostream>
//#include <cstdlib.h>
#include <cmath>
using namespace std;
int main() {
string pass = "";
int attempts = 3;
cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
cout << "< Welcome to The Bank >" << endl;
cout << "< >" << endl;
cout << "< Please Enter Pin Below >" << endl;
cout << " ^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
cout << "\nEnter Pin Number: " << endl;
cin >> pass;
// attempts= getch();
while (attempts <= 3) {
cout << "*";
getch();
attempts++; // take this out and it display to infinity
// }
if (pass == "1718") {
cout << " lOGIN IN..." << endl << endl;
attempts = -1;
}
else {
cout << "\nWRONT PIN-TRY AGAIN: " << endl << endl;
attempts--;
cout << " REMAINING ATTEMPTS: " << attempts << endl << endl;
}
if (attempts == 0) {
cout << "Exceed the Pin attempts. Try Later. " << endl << endl;
}
if (attempts == -1) {
cout << "********************************" << endl;
cout << "* Welcome to Magali's Bank *\n";
cout << "* Select Option: *\n";
cout << "* 1. Check Balance *\n";
cout << "* 2. Withdraw *\n";
cout << "* 3. Deposit *\n";
cout << "* 4. Exit *\n";
cout << "********************************\n";
int balance = 500;
float withdraw;
float deposit;
int user;
cout << "Enter Number: ";
cin >> user;
while (user != 4) {
switch (user) {
case 1:
cout << " Your balance is: " << balance << endl;
break;
case 2:
cout << "Enter the amount you want withdraw: ";
cin >> withdraw;
balance = balance - withdraw;
break;
case 3:
cout << "Enter the amount you want to deposit: ";
cin >> deposit;
balance = balance + deposit;
break;
default:
cout << " Need to type 1 for Balance, 2 to Withdraw, 3 to Deposit "
"and 4 to Exit. ";
}
cout << "Enter Number: ";
cin >> user;
}
cout << "\nTHANKS FOR USING THE SYSTEM!\n";
}
}
return 0;
}
Your code has lots of inefficiencies but as you requested. I'm only debugging only a part of it where you need to take input and display error.
string pass="";
int attempts=3;
const string pin = "1718";
//DO whatever you want
//while loop to take input
while (attempts >0)
{
cout << "\nEnter Pin Number: " << endl;
cin >> pass;
for(auto i : pass)
cout<<"*";
if(pass == pin){
attempts = 3;
cout<<"Your Pin has been accepted.\n";
break;}
else{
cout <<"\nWRONT PIN-TRY AGAIN: " << endl << endl;
attempts--;
cout << " REMAINING ATTEMPTS: " << attempts << endl << endl;
}
}
if (attempts == 0)
{
cout << "Exceed the Pin attempts. Try Later. "<< endl << endl;
cout<<"Your account has been locked for a day .\n";
exit(0);
}
Comment out if you didn't understand any part.
This is a Bank project I had in mind while learning C++ and I
have been adding to it as I learned inheritance and pointers. The
user is a bank teller who can create a new customer or process
transactions of an existing customer.
A Customer has Savings and Checking classes as private fields that inherit
from Account. The Bank class adds/gets customers from its private static vector<Customer> field.
The unexpected result is that in the 'process_transaction' method I can fetch
a customer from vector and deposit/withdrawl from it's accounts, but once I leave the method and come back to it the accounts data hasn't changed from the time I initialized them.
Need help. It's a reference to customer issue? When should I return by
reference or pointer?
Here is the code. In the driver class I have a method to create a customer
/*
* Create customer from teller input then add the Customer
* to Bank's private static vector<Customer> field.
*/
int create_customer(){
cout << "** Create New Customer **\n";
cout << "Customer Name: ";
string n = "";
cin >> n;
Customer* d = new Customer(n, gen_acct_number());
Customer c(*d);
// get opening balances
double open_ck = 0;
double open_sv = 0;
cout << "Opening Balance For Checking: ";
cin >> open_ck;
cout << "Opening Balance For Savings: ";
cin >> open_sv;
cout << "\n";
// create and set accounts
Checking ck(open_ck);
Savings sv(open_sv);
c.set_checking(ck);
c.set_savings(sv);
// add customer to bank
Bank b;
b.add_customer(c);
cout << "New Customer: " << c.get_name() << endl;
cout << "Account Number: " << c.get_acct_number() << endl;
cout << "\n";
return 0;
}
I have another method in the driver class to process a customer. Once I
leave this method a customer's accounts are unchanged, but works otherwise.
/*
* Fetch customer by account number from Bank's private static
* vector<Customer> and perform transactions on Customer's accounts
* until teller has finished processing the customer.
*/
int process_customer(){
cout << "** Process Transaction **\n";
cout << "Account Number: ";
int acctNum = 0;
cin >> acctNum;
cout << "\n";
// get customer
Bank b;
Customer c = b.get_customer(acctNum);
//if(c* == NULL){
// cout << "Error: Customer Not Found.\n";
// return 0;
//}
bool flag = true;
while(flag){
cout << c.get_name() << " #" << c.get_acct_number() << ": ";
cout << "Select a transaction.\n";
cout << "1. Withdrawl\n";
cout << "2. Deposit\n";
cout << "3. Check Balance\n";
cout << "4. Quit\n";
cout << "> ";
int choice = 0;
cin >> choice;
cout << "\n";
double amt = 0;
int which = 0;
switch(choice){
case 1:{ // WITHDRAWL
cout << "Withdrawl From: \n";
cout << "1. Checking \n2. Savings \n";
cout << "> ";
cin >> which;
cout << "\n";
cout << "Amount: ";
cin >> amt;
cout << "\n";
if(which == 1){
cout << "Old Balance: " << c.get_checking().get_balance() << endl;
c.get_checking().withdrawl(amt);
cout << "New Balance: " << c.get_checking().get_balance() << endl;
cout << "\n";
}else if (which == 2){
cout << "Old Balance: " << c.get_savings().get_balance() << endl;
c.get_savings().withdrawl(amt);
cout << "New Balance: " << c.get_savings().get_balance() << endl;
cout << "\n";
}else{
break;
}
break;
}
case 2:{ // DEPOSIT
cout << "Deposit Into: \n";
cout << "1. Checking \n2. Savings \n";
cout << "> ";
cin >> which;
cout << "\n";
cout << "Amount: ";
cin >> amt;
cout << "\n";
if(which == 1){
cout << "Old Balance: " << c.get_checking().get_balance() << endl;
c.get_checking().deposit(amt);
cout << "New Balance: " << c.get_checking().get_balance() << endl;
cout << "\n";
}else if (which == 2){
cout << "Old Balance: " << c.get_savings().get_balance() << endl;
c.get_savings().deposit(amt);
cout << "New Balance: " << c.get_savings().get_balance() << endl;
cout << "\n";
}else{
break;
}
break;
}
case 3:{ // CHECK BALANCE
cout << "Checking " << c.get_checking().get_balance() << endl;
cout << "Savings " << c.get_savings().get_balance() << endl;
cout << "\n";
break;
}
default:{ // EXIT
flag = false;
break;
}
}
}
return 0;
}
The Bank class.
Bank::Bank(){}
Customer& Bank::get_customer(int acct_number) {
for(unsigned i = 0; i < cus.size(); i++){
if(cus[i].get_acct_number() == acct_number){
return cus[i];
}
}
return cus[0];
}
void Bank::add_customer(Customer c){
cus.push_back(c);
}
/* Disabled. I want to pass an account to these methods.
*
* void Bank::deposit(double amt, Account& a){
* a.deposit(amt);
* }
* void Bank::withdrawl(double amt, Account& a){
* a.withdrawl(amt);
* }
* double Bank::check_balance( Account& a){
* return a.get_balance();
* }
*/
vector<Customer> Bank::cus;
Bank.h
#ifndef BANK_H_
#define BANK_H_
#include "../include/Customer.h"
#include <string>
#include <vector>
using namespace std;
class Bank{
public:
Bank();
Customer& get_customer(int acct_number);
void add_customer(Customer c);
void deposit(double amt, Account& a);
void withdrawl(double amt, Account& a);
double check_balance( Account& a);
private:
static vector<Customer> cus;
};
#endif /* BANK_H_ */
It does indeed appear to be a reference issue. In your process_customer function, change:
// get customer
Bank b;
Customer c = b.get_customer(acctNum);
to:
// get customer
Bank b;
Customer& c = b.get_customer(acctNum);
Without this change, you're making a copy of the customer and then modify this copy, instead of modifying the original customer.
I am creating a program that acts as a payroll system in which I create a file name and enter in: ID (int), pay (double), hours (int) and gross wage (double). How would I take the data I enter to the file and put them in arrays and display them. Later on, I will need to sort through them as I have cases for in my switch statement.
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//Function prototypes
void getEmployeeInfo(int&, double&, int&); //EmployeeInfo prototype
double calcWage(double, int, double&); //calcWage prototype
void printWages(ofstream &, int, double, int , double); //print wages prototype
//Main function
int main(){
int ID, hours, caseInput;
double pay, gross;
char input;
ofstream outputFile;
string filename;
do {
cout << " Menu " << endl;
cout << "1. Calculate gross wages for employees" << endl;
cout << "2. Display employees information to screen" << endl;
cout << "3. Display information in order of ID" << endl;
cout << "4. Display information in order of hourly rate" << endl;
cout << "5. Display information in order of hours worked" << endl;
cout << "6. Display information in order of wage" << endl;
cout << "7. Quit the system" << endl;
cout << "Enter your option --> ";
cin >> caseInput;
switch (caseInput) {
case 1: //Create file
cout << "Enter the filename: ";
cin >> filename;
//open file
outputFile.open(filename.c_str());
outputFile << setw(10) << "ID" << setw (15) << "Pay" << setw(17) << "Hours"
<< setw(11) << "Gross"<<endl;
outputFile << "---------------------------------------------------------\n";
do{
getEmployeeInfo(ID, pay, hours);
calcWage(pay, hours, gross);
printWages(outputFile, ID, pay, hours, gross);
cout << "Do you want to enter another employee's information? ";
cin >> input;
}
while(input == 'y' || input == 'Y');
//If user does not enter y, close file
outputFile.close();
cout << "The result is reported to the file " <<
filename << "." << endl;
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5: cout << "Thank you for using Math Tutor." << endl;
break;
case 6: cout << "Thank you for using Math Tutor." << endl;
break;
case 7: cout << "Thank you for using the Payroll System." << endl;
break;
default: cout << "Error. Enter a number 1-7." << endl;
}
}
while(caseInput != 7);
return 0;
}
//Function to input employee info
void getEmployeeInfo(int &ID, double &pay, int &hours){
cout << "Enter an employee's information by the order of ID number, rate, hours: ";
cin >> ID >> pay >> hours;
while(ID < 0){
cout << "You must enter a non negative value. Try again!" << endl;
cin >> ID;
}
while(pay < 0){
cout << "You must enter a non negative value. Try again!" << endl;
cin >> pay;
}
while(hours < 0){
cout << "You must enter a non negative value. Try again!" << endl;
cin >> hours;
}
}
//Function calculates gross pay
double calcWage(double pay, int hours, double &gross){
gross = pay * hours;
return gross;
}
//Print function
void printWages(ofstream &outputFile, int ID, double pay, int hours, double gross){
outputFile << setw(10)<< ID << setw(12) << "$" << setprecision(2)
<< fixed << pay << setw(13) << hours
<< setw(10) << "$" << fixed << gross << endl;
}
ifstream input("filename.txt");
copy(istreambuf_iterator<char>(input), istreambuf_iterator<char>(), ostreambuf_iterator<char>(cout));
This code will read data from a txt file and print it to screen. You may refer to cppreference.com to see how all the components work.
Make sure you include the appropriate headers. The reference will tell you that as well.
I think you can store your data in a New class, and save this data in a vecotr at first. Like this:
// declare a class with all your employee info in.
class Info
{
public:
Info(int id, int hours, double pay)
{
this.ID = id;
this.hours = hours;
this.pay = pay;
this.gross = pay*hours;
}
int ID;
int hours;
double pay;
double gross;
}
vector<Info> vInfo; // save your data from your input or file
// if you want to sort your data
std::sort(vInfo.begin(), vInfo.end(), [ ](Info &a, Info &b){
// you can change this depend on your order
// return a.xxx < b.xxxx
return a.ID < b.ID;
});
// then use the printWagesVec to print the data in your case 2,3,4,5,6
// change print function
int printWagesVec(vector<Info>& vInfo)
{
cout << setw(10) << "ID" << setw (15) << "Pay" << setw(17) << "Hours"
<< setw(11) << "Gross"<<endl;
cout << "---------------------------------------------------------\n";
vector<Info>::iterator it = vInfo.begin();
for(; it != vInfo.end(); it++)
{
cout << setw(10)<< it->ID << setw(12) << "$" << setprecision(2)
<< fixed << it->pay << setw(13) << it->hours
<< setw(10) << "$" << fixed << it->gross << endl;
}
return 0;
}
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am currently working on just a miscellaneous Theater Seating program! I am just starting to progress through C++, but I can't figure out these errors. I am trying to make the program store user entered row numbers and row seats (only if it is multiple seats) in two vectors; vector seat_row() and vector seat_number(). I know what the error is stating, but I don't know how to approach the situation otherwise. Sorry if I am being too unclear, but to be honest, I don't know what else to include. Please take a look at my code, get a feel for my program, and let me have any ideas. Please keep answers somewhat simple; again, I'm no master at C++
P.S.
Sorry for showing the whole code... I didn't really know what to include, what not to; and I wanted people to be able to get a feel for the program by even testing it themselves... Thanks again!
/*
* This is a program built by a team of students
* to help local movie theaters sell tickets
*
* File: main.cpp
* Author(s):
*
*
* Created on April 15, 2013, 11:10 AM
*/
#include <cstdlib>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;
//Function Prototypes
void Title_Card();
void seating_prices();
void seating_chart();
void pick_seating();
void purchase_history();
void quit();
void update_file();
void Show_Menu();
void picking_seats();
void display_seating();
void display_name();
void display_number_of_seats();
void display_correct1(string, int);
void display_seats();
void display_correct2(string, int, int, int);
void display_correct2_alt(string, int, vector<int>, vector<int>);
void display_correct3(string);
void multiple_seats(string, int, vector<int>, vector<int>);
void display_rows_seats(string, int);
void display_finished(string);
void display_purchase_seats(string, int, int);
void display_purchase_seats_alt(string, int, vector<int>, vector<int>);
int readSeating (const char*, vector<char>&); //Reads SeatingChart.txt
int readPrices(string, vector<double>&); //Reads SeatingPrices.txt
vector<double> prices(15); //For SeatPrices.txt
vector<char> seating(450); //For SeatingChart.txt
vector<int> seat_row(); //For storing multiple seat rows
vector<int> seat_number(); //For storing multiple seat numbers
//Actual Program
int main() {
Title_Card(); //Calls Title Page
readSeating("SeatingChart.txt", seating); //Reads SeatingChart.txt
readPrices("SeatPrices.txt", prices); //Reads SeatPrices.txt
Show_Menu(); //Shows Introductory Menu
system ("pause");
return 0;
}
//**************************************************************
// Definition of the ShowMenu function. Shows introductory menu*
// and controls where user ends up in the program. *
//**************************************************************
void Show_Menu() {
int choice;
string password;
cout << "Welcome to the our theater program! Made for a person" << endl;
cout << "who is looking for seating, purchasing a ticket, and" << endl;
cout << "searching for other miscellaneous things... We hope" << endl;
cout << "you enjoy the program!" << endl << endl;
cout << "Below is a list of options the user can choose from:" << endl << endl;
cout << "1.\tSeating Prices" << endl;
cout << "2.\tSeating Chart" << endl;
cout << "3.\tPick Seating" << endl;
cout << "4.\tPurchase History" << endl;
cout << "5.\tQuit" << endl << endl;
cout << "Enter a choice... (1-5): ";
cin >> choice;
while (choice < 1 || choice > 5){
cout << endl << "You have entered an invalid choice!" << endl;
cout << "Enter a choice... (1-5): ";
cin >> choice;
}
switch (choice){
case 1:
seating_prices();
case 2:
seating_chart();
case 3:
pick_seating();
case 4:
purchase_history();
case 5:
quit();
}
}
//**************************************************************
// Definition of the seating_prices function. Displays to the *
// user, SeatPrices.txt *
//**************************************************************
void seating_prices(){
system ("cls");
cout << "The Current Seating Prices Are:" << endl << endl;
for (int count = 0; count < 4; count++){
cout << " " << setprecision(4) << showpoint << prices[count] << " | Row " << (count + 1) << endl;
}
for (int count = 4; count < prices.size(); count++){
cout << " " << setprecision(3) << showpoint << prices[count] << " | Row " << (count + 1) << endl;
}
cout << endl;
system ("pause");
system ("cls");
Show_Menu();
}
//**************************************************************
// Definition of the seating_chart function. Function for *
// displaying the seating chart by itself *
//**************************************************************
void seating_chart(){
system ("cls");
int counter = 30;
int row_counter = 0;
cout << "The Current Seating Chart Is:" << endl << endl << endl << " 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0" << endl << endl;
cout << " ---------------------------------------------------------------" << endl;
cout << " Row " << (row_counter + 1) << " ";
//Displaying Seating Chart
for (int index = 0; index < 270; index++){
if (index == counter){
row_counter = (row_counter + 1);
counter = (counter + 30);
cout << "" << endl << " Row " << (row_counter + 1) << " ";
}
cout << seating[index] << " ";
}
for (int index = 270; index < seating.size(); index++){
if (index == counter){
row_counter = (row_counter + 1);
counter = (counter + 30);
cout << "" << endl << " Row " << (row_counter + 1) << " ";
}
cout << seating[index] << " ";
}
cout << endl << " ---------------------------------------------------------------" << endl;
cout << endl << endl;
system ("pause");
system ("cls");
Show_Menu();
}
//**************************************************************
// Definition of the pick_seating function. Displays the *
// current seating chart and allows the user to pick their seat*
//**************************************************************
void pick_seating(){ //Not Finished
system ("cls");
display_seating();
}
//**************************************************************
// Definition of the purchase_history function. Displays the *
// current the total sum of all movie ticket purchases *
//**************************************************************
void purchase_history(){ //Not finished
system ("cls");
system ("pause");
system ("cls");
Show_Menu();
}
//**************************************************************
// Definition of the quit function. Allows the user to quit the*
// program entirely *
//**************************************************************
void quit(){
update_file();
exit(0);
}
//**************************************************************
// Definition of the update_file function. Designed to update *
// the seating chart upon leaving the pick_seating function *
//**************************************************************
void update_file(){ //Not finished
//This function is supposed to
//Update the seating chart
//upon exit of the pick_seating function
}
//**************************************************************
// Definition of the read_Prices function. Reads SeatPrices.txt *
// and stores the pre-determined prices into a vector named *
// prices. *
//**************************************************************
int readPrices(string myFile, vector<double>& vect) {
//input file
ifstream SeatPrices;
SeatPrices.open(myFile.c_str());
//if file cannot be found
if (!SeatPrices)
cout << "Cannot find the file!" << endl;
for (int index = 0; index < vect.size(); index++){
SeatPrices >> vect[index]; //Reading the file "SeatPrices.txt"
}
SeatPrices.close(); //Closes the file
return 0;
}
//**************************************************************
// Definition of the readSeating function. Reads a text file *
// with a seating chart in it. *
//**************************************************************
int readSeating(const char* myFile, vector<char>& vect){
//input file
ifstream SeatingChart;
SeatingChart.open(myFile);
//if file cannot be found
if (!SeatingChart)
cout << "Cannot find the file!" << endl;
for (int index = 0; index < vect.size(); index++){
SeatingChart >> vect[index]; //Reading the file "SeatingChart.txt"
}
SeatingChart.close(); //Closes the file
return 0;
}
//**************************************************************
// Definition of the display_seating function. Function for *
// simplifying the pick_seating function *
//**************************************************************
void display_seating(){
int counter = 30;
int row_counter = 0;
//Displaying Seating Chart
cout << " 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0" << endl << endl;
cout << " ---------------------------------------------------------------" << endl;
cout << " Row " << (row_counter + 1) << " ";
//Displaying Seating Chart
for (int index = 0; index < 270; index++){
if (index == counter){
row_counter = (row_counter + 1);
counter = (counter + 30);
cout << "" << endl << " Row " << (row_counter + 1) << " ";
}
cout << seating[index] << " ";
}
for (int index = 270; index < seating.size(); index++){
if (index == counter){
row_counter = (row_counter + 1);
counter = (counter + 30);
cout << "" << endl << " Row " << (row_counter + 1) << " ";
}
cout << seating[index] << " ";
}
cout << endl << " ---------------------------------------------------------------" << endl << endl;
cout << "In the seating chart... All hashtags (#'s) are seats already taken" << endl;
cout << "and all stars (*'s) are available seats..." << endl << endl;
system ("pause");
display_name(); //Continues the program, helps loop if necessary
}
//**************************************************************
// Definition of the display_name function. Function for *
// simplifying the pick_seating function *
//**************************************************************
void display_name(){
string name;
//Picking your seat
cout << endl << endl << "To pick your own seat(s), follow the instructions below:" << endl << endl;
cout << "What is the name of the recipient of the seat(s)? ";
cin >> name;
display_correct3(name);
}
//**************************************************************
// Definition of the display_number_of_seats function. Function*
// for simplifying the pick_seating function *
//**************************************************************
void display_number_of_seats(string name){
int number_of_seats;
int available_seats = 450; //Amount of remaining seats out of 450
cout << "Alright " << name << "!" << endl;
cout << "How many seats are you purchasing today? ";
cin >> number_of_seats;
while (number_of_seats < 1 || number_of_seats > available_seats){
cout << endl << endl << "You have entered an invalid number of seats!" << endl;
cout << "This might be because your number is zero or less," << endl;
cout << "or that the number you entered is more than the amount" << endl;
cout << "of remaining seats! Try again!" << endl << endl;
cout << "How many seats are you purchasing today? ";
cin >> number_of_seats;
}
display_correct1(name, number_of_seats);
}
//**************************************************************
// Definition of the display_correct1 function. Function for *
// simplifying the pick_seating function *
//**************************************************************
void display_correct1(string name, int number_of_seats){
int correct;
cout << endl << "Alright " << name << ", you are purchasing " << number_of_seats << " seat(s)?" << endl;
cout << "Is this correct? Enter a '0' for 'no', or a '1' for yes: ";
cin >> correct;
while (correct < 0 || correct > 1){
cout << "You have entered an invalid number!" << endl;
cout << "Enter a '0' for 'no', or a '1' for yes: ";
cin >> correct;
}
if (correct == 0){
cout << endl << endl;
display_number_of_seats(name);
}
if (correct == 1){
cout << endl << endl;
if (number_of_seats > 1)
multiple_seats(name, number_of_seats, seat_row, seat_number);
display_rows_seats(name, number_of_seats);
}
}
//**************************************************************
// Definition of the multiple_seats function. Function only *
// used if user chooses to purchase multiple seats *
//**************************************************************
void multiple_seats(string name, int number_of_seats, vector<int> vect, vector<int> vect2){
for (int index = 1; index <= number_of_seats; index++){
for (int count = 0; count < number_of_seats; count++){
cout << "For Seat #" << index << "..." << endl;
cout << "Enter the row number you would like to be in: (1-15): ";
cin >> vect[count];
while (vect[count] < 1 || vect[count] > 15){
cout << endl << "You have entered an invalid row number!" << endl;
cout << "Enter the row number you would like to be in (1-15): ";
cin >> vect[count];
}
cout << "Enter the seat number you would like to have (1-30): ";
cin >> vect2[count];
while (vect2[count] < 1 || vect2[count] > 30){
cout << endl << "You have entered an invalid seat number!" << endl;
cout << "Enter the seat number you would like to have (1-30): ";
cin >> vect2[count];
}
cout << endl;
}
}
display_correct2_alt(name, number_of_seats, seat_row, seat_number);
}
//**************************************************************
// Definition of the display_rows_seats function. Function for *
// simplifying the pick_seating function *
//**************************************************************
void display_rows_seats(string name, int number_of_seats){
int seat_choice;
int row_choice;
cout << "Enter the row number you would like to be in: (1-15): ";
cin >> row_choice;
while (row_choice < 1 || row_choice > 15){
cout << endl << "You have entered an invalid row number!" << endl;
cout << "Enter the row number you would like to be in (1-15): ";
cin >> row_choice;
}
cout << "Enter the seat number you would like to have (1-30): ";
cin >> seat_choice;
while (seat_choice < 1 || seat_choice > 30){
cout << endl << "You have entered an invalid seat number!" << endl;
cout << "Enter the seat number you would like to have (1-30): ";
cin >> seat_choice;
}
display_correct2(name, number_of_seats, row_choice, seat_choice); //Helps looping if necessary
}
//**************************************************************
// Definition of the display_correct2_alt function. Alternate *
// function if user enters multiple seats *
//**************************************************************
void display_correct2_alt(string name, int number_of_seats, vector<int> vect, vector<int> vect2){
int correct;
int counter = 1;
int counter_2 = 1; //For minor details for looks
for (int index = 1; index <= number_of_seats; index++){
for (int count = 0; count < number_of_seats; count++){
if (counter_2 != number_of_seats && counter_2 == 1){ //For first seat
cout << endl << endl << "Alright " << name << ";" << endl;
cout << "For Seat #" << index << "; you chose " << "Row #" << vect[count];
cout << " and Seat #" << vect2[count] << "?" << endl;
cout << "Is this correct? Enter a '0' for 'no', or a '1' for yes: ";
cin >> correct;
}
if (counter_2 != number_of_seats && counter_2 > 1){ //For all seats after first, except last seat
cout << endl << endl;
cout << "Next, for Seat #" << index << "; you chose " << "Row #" << vect[count];
cout << " and Seat #" << vect2[count] << "?" << endl;
cout << "Is this correct? Enter a '0' for 'no', or a '1' for yes: ";
cin >> correct;
}
if (counter_2 == number_of_seats){ //For last seat
cout << endl << endl;
cout << "And for your last seat, Seat #" << index << "; you chose " << "Row #" << vect[count];
cout << " and Seat #" << vect2[count] << "?" << endl;
cout << "Is this correct? Enter a '0' for 'no', or a '1' for yes: ";
cin >> correct;
}
}
while (correct < 0 || correct > 1){
cout << "You have entered an invalid number!" << endl;
cout << "Enter a '0' for 'no', or a '1' for yes: ";
cin >> correct;
}
if (correct == 0){
cout << endl << endl;
if (number_of_seats > 1)
multiple_seats(name, number_of_seats, seat_row, seat_number);
display_rows_seats(name, number_of_seats);
}
if (correct == 1){
if (counter == number_of_seats)
display_purchase_seats_alt(name, number_of_seats, seat_row, seat_number);
}
counter = (counter + 1);
counter_2 = (counter_2 + 1);
}
}
//**************************************************************
// Definition of the display_correct2 function. Function for *
// simplifying the pick_seating function *
//**************************************************************
void display_correct2(string name, int number_of_seats, int row_choice, int seat_choice){
int correct;
cout << endl << endl << "Alright " << name << ", you chose " << "Row #" << row_choice;
cout << " and Seat #" << seat_choice << "?" << endl;
cout << "Is this correct? Enter a '0' for 'no', or a '1' for yes: ";
cin >> correct;
while (correct < 0 || correct > 1){
cout << "You have entered an invalid number!" << endl;
cout << "Enter a '0' for 'no', or a '1' for yes: ";
cin >> correct;
}
if (correct == 0){
cout << endl << endl;
if (number_of_seats > 1)
multiple_seats(name, number_of_seats, seat_row, seat_number);
display_rows_seats(name, number_of_seats);
}
if (correct == 1)
display_purchase_seats(name, row_choice, seat_choice);
}
//**************************************************************
// Definition of the display_purchase_seats function. Function *
// user to purchase his chosen seats *
//**************************************************************
void display_purchase_seats(string name, int row_choice, int seat_choice){
int total_cost = 0; //Not set up yet; supposed to calculate the row price
system ("cls");
cout << name << ", now it is time to pay for your chosen seats!" << endl;
cout << "Since you chose Row #" << row_choice << ", and Seat# " << seat_choice << "..." << endl;
cout << "Your total cost is: S" << total_cost << endl << endl;
system ("pause");
display_finished(name);
}
//**************************************************************
// Definition of the display_purchase_seats_alt function. *
// Alternate function used for purchasing multiple seats *
//**************************************************************
void display_purchase_seats_alt(string name, int number_of_seats, vector<int> vect, vector<int> vect2){
int total_cost = 0; //Not set up yet; supposed to calculate the row price
system ("cls");
cout << name << ", now it is time to pay for your chosen seats!" << endl;
cout << "Since you chose " << number_of_seats << " seats:" << endl << endl;
for (int index = 0; index <= number_of_seats; index++){
cout << "Seat #" << index << " being in Row #" << seat_row[index] << ";" << endl;
}
cout << endl << "Your total cost is: $" << total_cost << endl << endl;
system ("pause");
display_finished(name);
}
//**************************************************************
// Definition of the display_correct3 function. Function for *
// simplifying the pick_seating function *
//**************************************************************
void display_correct3(string name){
int correct;
cout << endl << "Alright, you chose the name " << name << "?" << endl;
cout << "Is this correct? Enter a '0' for 'no', or a '1' for yes: ";
cin >> correct;
while (correct < 0 || correct > 1){
cout << "You have entered an invalid number!" << endl;
cout << "Enter a '0' for 'no', or a '1' for yes: ";
cin >> correct;
}
if (correct == 0){
system ("cls");
display_seating();
}
if (correct == 1){
cout << endl;
display_number_of_seats(name); //Helps if looping is necessary
}
}
//**************************************************************
// Definition of the display_finished function. Function for *
// simplifying the pick_seating function *
//**************************************************************
void display_finished(string name){
system ("cls");
cout << "Congratulations " << name << "! You have picked your seat!" << endl;
cout << "The Seating Chart will update momentarily..." << endl << endl;
update_file();
system ("pause");
system ("cls");
Show_Menu();
}
//**************************************************************
// Definition of the Title_Card function. Starts the program *
// with a title card, showing a little introductory title *
//**************************************************************
void Title_Card(){
cout << endl << endl << endl << endl;
cout << "\t\t" << "************************************************\n";
cout << "\t\t" << "* THEATER SEATING! *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "* A program created by a team of three *\n";
cout << "\t\t" << "* students to help small theaters sell *\n";
cout << "\t\t" << "* more tickets *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "* Team of Students: *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "************************************************\n";
cout << endl << endl;
system ("pause");
system ("cls");
}
Your error is here
vector<int> seat_row(); //For storing multiple seat rows
vector<int> seat_number(); //For storing multiple seat numbers
it should be
vector<int> seat_row; //For storing multiple seat rows
vector<int> seat_number; //For storing multiple seat numbers
It's a very common error, you were trying to declare two vectors called seat_row and seat_number, but because you used empty parens instead you declared two functions which return vectors.
Here's a summary
vector<int> a(10); // vector, size 10
vector<int> b(0); // vector, size 0
vector<int> c; // also vector size 0
vector<int> d(); // FUNCTION!! taking no arguments and returning a vector
I am making a program with which to save banking information (i.e., account, password, balance). I am having trouble with a certain error but I'm not sure of the cause.
Here is the entire code.
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
int x = 0;
void addUser();
void login();
void deposit();
void withdrawl();
struct bankUser
{
double balance;
string account, password;
};
bankUser user[20];
int menu()
{
ofstream myfile("bankmachine.txt", ios::app);
char choice;
cout << "1) Add account" << endl;
cout << "2) Log in" << endl;
cout << "3) Make deposit" << endl;
cout << "4) Make withdrawl" << endl;
cout << "5) Quit" << endl << endl;
cout << "What would you like to do?: ";
cin >> choice;
cout << endl;
switch (choice)
{
case '1':
addUser();
break;
case '2':
login();
break;
case '3':
deposit();
break;
case '4':
withdrawl();
break;
case '5':
myfile << user[x].balance << endl;
myfile.close();
cout << "Thank you for using the banking system." << endl << endl;
system ("pause");
return 0;
break;
default:
cout << "That is not a valid option. Please choose again." << endl << endl;
menu();
}
}
void addUser()
{
if ((x >= 0) && (x < 20))
{
cout << "Please enter your desired account name: ";
cin >> user[x].account; // Account name.
cout << "Thank you, now please enter your desired password: ";
cin >> user[x].password; // Account password.
cout << "\nAccount created. You may now log in." << endl << endl;
ofstream myfile("bankmachine.txt", ios::app); // Opens the text file at the end of the file (if data is already present).
myfile << user[x].account << endl; // Writes to text file.
myfile << user[x].password << endl; // ^
myfile.close(); // Close text file (important).
x++; // Increases to simulate the addition of another user.
menu();
}
else // Will display if user has entered 20 users.
{
cout << "You have entered the maximum number of users." << endl;
menu();
}
}
void deposit()
{
int deposit;
string answer;
do
{
cout << "Please enter the amount of money that you would like to deposit: ";
cin >> deposit;
user[x].balance += deposit;
cout << "Thank you. Your new balance is " << user[x].balance << "." << endl << endl;
cout << "Would you like to make another deposit? (Y/N): ";
cin >> answer;
} while ((answer != "N") && (answer == "Y"));
cout << endl;
menu();
}
void withdrawl()
{
int withdraw;
string answer;
do
{
cout << "Please enter the amount of money that you would like to withdraw: ";
cin >> withdraw;
if (withdraw <= user[x].balance)
{
user[x].balance -= withdraw;
}
else
{
cout << "\nSorry, you do not have sufficient funds to complete this withdrawl.\nPlease try again." << endl << endl;
withdrawl();
}
cout << "Thank you. Your new balance is " << user[x].balance << "." << endl << endl;
cout << "Would you like to make another withdrawl? (Y/N): ";
cin >> answer;
} while (answer != "N" && answer == "Y");
cout << endl;
menu();
}
void login() // Function to log in.
{
string user, pw, usernameCheck, passwordCheck;
double balance;
cout << "Please enter your login information." << endl << endl;
cout << "Account name: ";
cin >> user;
cout << "Password: ";
cin >> pw;
cout << endl;
ifstream myfile("bankmachine.txt", ios::app);
while (!myfile.eof()) // Loops until end of file.
{
getline(myfile, usernameCheck);
if (usernameCheck == user)
{
getline(myfile, passwordCheck);
if (passwordCheck == pw)
{
myfile >> balance;
cout << "Login successful." << endl;
cout << "Your balance is " << balance << "." << endl << endl;
user[x].balance = balance;
}
else // If not, display:
{
cout << "Password incorrect." << endl << endl;
}
}
}
myfile.close(); // Close text file (important).
menu();
}
int main()
{
cout << "Welcome to the banking system." << endl << endl;
menu();
}
I keep getting this error (on line 172):
request for member 'balance' in 'user.std::basic_string<_CharT, _Traits,
_Alloc>::operator[] [with _CharT = char, _Traits = std::char_traits<char>, _Alloc =
std::allocator<char>](((unsigned int)x))', which is of non-class type 'char'|
What is this caused by? How can I fix it? Any answers are appreciated.
Judging by the error provided it seems as if user is not of type struct bankUser, but a std::string.
You are trying to assign a std::string (balance) to character at offset x of your std::string named user, which is doomed to fail.
TL;DR user is not declared to be a struct bankUser.