Problems finding out total sum using data structures and arrays - c++

I have this code that has several funtions and am almost done, I just am having trouble finding the rental cost on my program.
My program reads a text file of cars and the rental cost as shown here:
2014 Toyota Tacoma 115.12 1
2012 Honda CRV 85.10 0
2015 Ford Fusion 90.89 0
2013 GMC Yukon 110.43 0
2009 Dodge Neon 45.25 1
2011 Toyota Rav4 65.02 1
2012 Mazda CX5 86.75 1
2016 Subaru Outback 71.27 0
2015 Ford F150 200.83 1
2010 Toyota Corolla 50.36 1
The float character is the price ( rental cost )
However I want the user to input the car number ( 1-10) choose how many days and output the rental cost. I am just having trouble how it would read the input of the car the user wants. This is my main code, but what I need is to tell if case 3 needs work.
#include <iostream>
#include <fstream>
using namespace std;
struct car {
int year;
char make[10];
char model[10];
float price;
int available;
} ;
void menu();
// Main Function
int main ()
{
// declare variables
int carAmount = 10;
int choice;
car carLib[carAmount];
char filename[10];
ifstream carInData;
float mostExpensive = 0;
int MostExpensiveIndex;
int count = 0;
int days;
int rentalCost = 0;
bool menu1 = false;
//prompt user for input file
cout << " Enter file name: ";
cin >> filename;
// Start loop menu
while(menu1 = true){
menu();
carInData.open(filename);
cin >> choice;
if (carInData.is_open()) {
// read list of names into array
for (; count < carAmount; count++) {
carInData >> carLib[count].year >> carLib[count].make >> carLib[count].model >> carLib[count].price >> carLib[count].available;
}
}
switch (choice) {
// Case 1 closes menu
case 1:
return 0;
break;
// Case 2 displays if car is available if 1, unavailable if 0
case 2:
// itterate through car array
for(count = 0; count < carAmount; count++){
// Displays if car is available or not
if (carLib[count].available == 1)
cout << " Available ";
else
cout << " Unavailable ";
// Display Cars
cout << carLib[count].year << " " << carLib[count].make << " " << carLib[count].model << " " << carLib[count].price << " " << "\n";
}
break;
// Display only available cars
case 3:
// itterate through car array
for(count = 0; count < carAmount; count++){
// Displays only available cars
if (carLib[count].available == 1){
cout << " Available ";
// Display Cars
cout << carLib[count].year << " " << carLib[count].make << " " << carLib[count].model << " " << carLib[count].price << " " << "\n";
}
}
break;
// Calculates rental cost
case 4:
cout << " Enter car number and how many days " << "\n";
cout << " Days: ";
cin >> days;
cout << "\n" << "Car: ";
cin >> carLib[count].price;
rentalCost += days*count;
cout << " Rental Cost for " << days << " days is " << rentalCost << "\n";
break;
// Finds most expensive car
case 5:
MostExpensiveIndex = count;
for (size_t carIndex = 0; carIndex < carAmount; ++carIndex) {
if (carLib[carIndex].price <= mostExpensive) continue;
mostExpensive = carLib[carIndex].price;
MostExpensiveIndex = carIndex;
}
const car & carI = carLib[MostExpensiveIndex];
cout << " Most Expensive car is: " << " " << carI.year << " " << carI.make << " " << carI.model << " " << carI.price << "\n";
break;
}
}
return 0;
}
void menu()
{
cout << " 1 - Exit program.\n";
cout << " 2 - Show Cars\n";
cout << " 3 - Show only available cars.\n";
cout << " 4 - Rental Cost\n";
cout << " 5 - Most Expensive Car\n";
}

Unless I'm misunderstanding something with how your code is suppose to work, I think case 3 works fine. HOWEVER, it's case 4 that you should be worried about
case 4:
cout << " Enter car number and how many days " << "\n";
cout << " Days: ";
cin >> days;
cout << "\n" << "Car: ";
cin >> carLib[count].price; // WHY IS THE USER CHANGING THE PRICE?
rentalCost += days*count; // WHY IS THE PRICE "DAYS * (CAR ID #)"
cout << " Rental Cost for " << days << " days is " << rentalCost << "\n";
break;
You can see my comments there. I would change this to
case 4:
cout << " Enter car number and how many days " << "\n";
cout << " Days: ";
cin >> days;
cout << "\n" << "Car: ";
cin >> count ;
// Note the decrement of 'count' by one, since you expect the user
// to enter a number 1-10
// Should probably include a check that the 'count' is valid
rentalCost += days*carLib[count-1].price;
cout << " Rental Cost for " << days << " days is " << rentalCost << "\n";
break;
Note that carLib increments from 0->9, but your user might think it counts from 1-10. It might help when you print option 2 (all car information) that you include the car ID number that you're expecting from the user.
Also a few more questions for you
You're storing the rental cost as an int (not sure if that's by design), so just remember that the days*price computation will get rounded.
The rentalCost is incrementing to be bigger and bigger, so I'm guessing the user is renting multiple cars?
You should maybe do a check as to whether the user can actually rent a car before increasing their rental cost.

Related

Is there any way I can store multiple int's from a user input into a vector?

I am trying to store multiple int into a vector by having the user input the grade they have received. However I do believe there is a more simplified version of trying to do this. Would it be better to have separate functions? Is there anyone who can guide me in the right direction?
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main() {
cout << "Welcome to the Grade Calculator! \n"
<< "What is the student's full name? ";
string student_name;
getline(cin,student_name);
cout << "\nPlease input the points earned for each assignment.";
vector<int> student_grades(8);
cout << "\nLab 2: ";
cin >> student_grades[0];
cout << "Lab 3: ";
cin >> student_grades[1];
cout << "Lab 4: ";
cin >> student_grades[2];
cout << "Lab 5: ";
cin >> student_grades[3];
cout << "Lab 6: ";
cin >> student_grades[4];
cout << "Lab 7: ";
cin >> student_grades[5];
cout << "Lab 8: ";
cin >> student_grades[6];
cout << "Lab 9: ";
cin >> student_grades[7];
cout << "Lab 10: ";
cin >> student_grades[8];
//Finding sum of the grades
int sum_of_grades = accumulate(student_grades.begin(),student_grades.end(),0);
//Console Output
cout << '\n'<< student_name << " has earned " << sum_of_grades << " points in the course, so far." << endl;
cout << "\nThere are 2000 points possible in this course." << endl;
cout << "\nInput the anticpated score for the final project (200 points possible): ";
int anticipated_score;
cin >> anticipated_score;
int total_score = sum_of_grades+anticipated_score;
cout << "Total Projected Assignment Points: " << total_score << endl;
cout << "\nFinal Exam Score Needed for Final Course Grade (1000 Points Possible)"
<< "\nTo Earn an A, need at least: " << 1800 - total_score << " points, for a total of: 1800"
<< "\nTo Earn a B, need at least: " << 1600 - total_score << " points, for a total of: 1600"
<< "\nTo Earn a C, need at least: " << 1400 - total_score << " points, for a total of: 1400"
<< "\n To Earn a D, need at least: " << 1200 - total_score << " points, for a total of: 1200" << endl;
return 0;
}
You can use a for-loop:
for(int i = 0; i < 8; ++i){
cout << "\nLab " << i+2 << ": ";
cin >> student_grades[i];
}
This code is essentially equivalent to what you wrote in your program; it loops through all possible indices, prompting the user to input numbers into the vector.

Finding the max in an array of Data-Structures

I have a text file of cars:
2014 Toyota Tacoma 115.12 1
2012 Honda CRV 85.10 0
2015 Ford Fusion 90.89 0
2013 GMC Yukon 110.43 0
2009 Dodge Neon 45.25 1
2011 Toyota Rav4 65.02 1
2012 Mazda CX5 86.75 1
2016 Subaru Outback 71.27 0
2015 Ford F150 190.83 1
2010 Toyota Corolla 50.36 1
I am trying to find the max which is the float but I am having trouble finding it as well as finding the rental cost of the cars. I have this so far but am still having trouble.
#include <iostream>
#include <fstream>
using namespace std;
struct car
{
int year;
char make[10];
char model[10];
float price;
int available;
} ;
void menu();
// Main Function
int main ()
{
// declare variables
int carAmount = 10;
int choice;
car carLib[carAmount];
char filename[10];
ifstream carInData;
float mostExpensive = 0;
int MostExpensiveIndex;
int count = 0;
int days;
int rentalCost = 0;
//prompt user for input file
cout << " Enter file name: ";
cin >> filename;
menu();
carInData.open(filename);
cin >> choice;
if(carInData.is_open());
{
// read list of names into array
for(cout; count < carAmount; count++){
carInData >> carLib[count].year >> carLib[count].make >> carLib[count].model >> carLib[count].price >> carLib[count].available;
switch (choice){
case 1:
if(carLib[count].available == 1)
cout << " Available ";
else
cout << " Unavailable ";
cout << carLib[count].year << " " << carLib[count].make << " " << carLib[count].model << " " << carLib[count].price << " " << "\n";
break;
case 2:
cout << " Enter car number and how many days " << "\n";
cout << " Days: ";
cin >> days;
cout << "\n" << "Car: ";
cin >> carLib[count].price;
rentalCost += days * count;
cout << " Rental Cost for " << days << " days is " << rentalCost;
break;
case 3:
MostExpensiveIndex = count;
for(size_t carIndex = 0; carIndex < count; ++carIndex){
if(carLib[carAmount].price <= mostExpensive) continue;
mostExpensive = carLib[carIndex].price;
MostExpensiveIndex = carIndex;
}
const car & carI = carLib[MostExpensiveIndex];
cout << " Most Expensive car is: " << MostExpensiveIndex << " " << carI.year << " " << carI.make << " " << carI.model << " " << carI.price << "\n";
break;
}
}
}
return 0;
}
void menu(){
cout << " 1 - Show Cars\n";
cout << " 2 - Rental Cost\n";
cout << " 3 - Most Expensive Car\n";
}
My output is displaying all my cars instead of the maximum one. This is the screenshot of the output
Replace:
if(carLib[carAmount].price <= mostExpensive) continue;
with
if(carLib[carIndex].price <= mostExpensive) continue;
Correct code is:
#include <iostream>
#include <fstream>
using namespace std;
struct car {
int year;
char make[10];
char model[10];
float price;
int available;
} ;
void menu();
// Main Function
int main ()
{
// declare variables
int carAmount = 10;
int choice;
car carLib[carAmount];
char filename[10];
ifstream carInData;
float mostExpensive = 0;
int MostExpensiveIndex;
int count = 0;
int days;
float rentalCost = 0;
//prompt user for input file
cout << " Enter file name: ";
cin >> filename;
menu();
carInData.open(filename);
cin >> choice;
if (carInData.is_open()) {
// read list of names into array
for (; count < carAmount; count++) {
carInData >> carLib[count].year >> carLib[count].make >> carLib[count].model >> carLib[count].price >> carLib[count].available;
}
}
switch (choice) {
case 1:
for (int carIndex = 0; carIndex < carAmount; ++carIndex){
if (carLib[carIndex].available == 1)
cout << " Available ";
else
cout << " Unavailable ";
cout << carLib[carIndex].year << " " << carLib[carIndex].make << " " << carLib[carIndex].model << " " << carLib[carIndex].price << " " << "\n";
}
break;
case 2:
cout << " Enter car number and how many days " << "\n";
cin >> count;
cout << " Days: ";
cin >> days;
cout << "\n" << "Car: ";
rentalCost = days * carLib[count].price;
cout << " Rental Cost for " << days << " days is " << rentalCost << endl;
break;
case 3:
MostExpensiveIndex = 0;
for (size_t carIndex = 0; carIndex < carAmount; ++carIndex) {
if (carLib[carIndex].price <= mostExpensive) continue;
mostExpensive = carLib[carIndex].price;
MostExpensiveIndex = carIndex;
}
const car & carI = carLib[MostExpensiveIndex];
cout << " Most Expensive car is: " << MostExpensiveIndex << " " << carI.year << " " << carI.make << " " << carI.model << " " << carI.price << "\n";
break;
}
return 0;
}
void menu()
{
cout << " 1 - Show Cars\n";
cout << " 2 - Rental Cost\n";
cout << " 3 - Most Expensive Car\n";
}

Having troubles w/functions and arrays

I'm Using Visual studio (C++) in a class that I am taking, I've had to teach myself functions, and I've hit a small snag in the road that I'd appreciate some advice on.
What I'm having trouble with, is the part of the assignment that states i must
"Utilize a function that prints (not find) the largest/average/smallest commissions"
The way I read this, I'm assuming she only wants it to print, and not do calculations in the function.
A friend suggested I try void print(etc) However I'm unsure how to grab the calculation in main and give it to the function I'm trying to print, or am i going at it all wrong?
I've commented out the portion of code I was trying to function. You can also find it at the very bottom of the code.
Any suggestions/help is greatly appreciated, as most of what I've looked up haven't really dealt with this problem (that I've found)
#include <iostream>
#include <iomanip>
using namespace std;
#define TITLE "Alva's"
#define STANDARD 0.05
#define HYBRID 0.10
#define ELECTRIC 0.15
#define HOLD 50
void dashLine();
int getSalesId();
int runProgram();
char vehicleType();
double sellingPrice();
void print(double largeSmallAverage);
int main()
{
int tot_count,
id_num[HOLD], //* ID
r_ay = 0, //* array
s_count, //*standard
h_count, //*Hybrid
e_count, //*electric
hold_id, //*array Id
compare, //*Pass
change, //*change made when change has value
yesno;
double tot_standard,
tot_hybrid,
tot_electric,
tot_price,
price[HOLD],
hold_price, //*Array hold
comm_l, //* Large
comm_s, //* Small
hold_comm, //*Array hold
avg_comm,
tot_commission,
commission[HOLD];
char car[HOLD],
temp_car;
tot_count = 0;
s_count = 0;
h_count = 0;
e_count = 0;
tot_price = 0;
tot_standard = 0;
tot_hybrid = 0;
tot_electric = 0;
cout << "\n" << TITLE << " Commission Calculator";
yesno = runProgram();
while (yesno == 1)
{
dashLine();
id_num[r_ay] = getSalesId();
car[r_ay] = vehicleType();
price[r_ay] = sellingPrice();
tot_price += price[r_ay];
switch (car[r_ay])
{
case 'S':
case 's':
commission[r_ay] = (price[r_ay] * STANDARD);
tot_standard += commission[r_ay];
s_count++;
break;
case 'H':
case 'h':
commission[r_ay] = (price[r_ay] * HYBRID);
tot_hybrid += commission[r_ay];
h_count++;
break;
case 'E':
case 'e':
commission[r_ay] = (price[r_ay] * ELECTRIC);
tot_electric += commission[r_ay];
e_count++;
break;
}
cout << "\n The commission for this sale, for Employee ID: " << fixed << setprecision(0) << id_num[r_ay] << " is:$ " << fixed << setw(5) << setprecision(2) << commission[r_ay];
cout << "\n";
yesno = runProgram();
r_ay++;
if (r_ay >= HOLD)
yesno = 0;
}
tot_count = (s_count + h_count + e_count);
tot_commission = (tot_standard + tot_hybrid + tot_electric);
{
cout << "\n Number of standard vehicle commissions calculated = " << fixed << setw(8) << setprecision(0) << s_count;
cout << "\n Number of hybrid vehicle commissions calculated = " << fixed << setw(8) << h_count;
cout << "\n Number of electric vehicle commissions calculated = " << fixed << setw(8) << e_count;
cout << "\n Number of vehicle commissions calculated = " << fixed << setw(8) << tot_count;
cout << "\n Total Overall price calculated =$ " << fixed << setw(8) << setprecision(2) << tot_price;
cout << "\n Total amount of standard vehicle commissions =$ " << fixed << setw(8) << tot_standard;
cout << "\n Total amount of hybrid vehicle commissions =$ " << fixed << setw(8) << tot_hybrid;
cout << "\n Total amount of electric vehicle commissions =$ " << fixed << setw(8) << tot_electric;
cout << "\n Total amount of all commissions paid out =$ " << fixed << setw(8) << tot_commission;
cout << "\n";
cout << "\n " << "Sales ID " << "Car type " << "Selling price " << "Commission ";
for (r_ay = 0; r_ay < tot_count; r_ay++)
{
cout << "\n " << fixed << id_num[r_ay] << " " << setprecision(2) << car[r_ay] << " " << setw(10) << price[r_ay] << " " << setw(10) << commission[r_ay];
}
if (tot_count > 0)
{
avg_comm = (tot_commission / tot_count);
comm_s = commission[0];
comm_l = commission[0];
for (r_ay = 1; r_ay < tot_count; r_ay++)
{
if (commission[r_ay] < comm_s)
comm_s = commission[r_ay];
if (commission[r_ay] > comm_l)
comm_l = commission[r_ay];
}
void print(double largeSmallAverage);
//{
// cout << "\n ";
// cout << "\n The smallest commission computed totals =$ " << fixed << setw(10) << comm_s;
// cout << "\n The largest commission computed totals =$ " << fixed << setw(10) << comm_l;
// cout << "\n Total average of commissions computed =$ " << fixed << setw(10) << avg_comm;
//}
}
cout << "\n";
change = 1;
compare = tot_count - 1;
do
{
change = 0;
for (r_ay = 0; r_ay < compare; r_ay++)
{
if (commission[r_ay] > commission[r_ay + 1])
{
temp_car = car[r_ay];
hold_id = id_num[r_ay];
hold_price = price[r_ay];
hold_comm = commission[r_ay];
commission[r_ay] = commission[r_ay + 1];
commission[r_ay + 1] = hold_comm;
id_num[r_ay] = id_num[r_ay + 1];
id_num[r_ay + 1] = hold_id;
car[r_ay] = car[r_ay + 1];
car[r_ay + 1] = temp_car;
price[r_ay] = price[r_ay + 1];
price[r_ay + 1] = hold_price;
change = 1;
}
}
compare--;
} while ((compare > 0) && (change == 1));
cout << "\n";
cout << "\n " << "Sales ID " << "Car type " << "Selling price " << "Commission ";
for (r_ay = 0; r_ay < tot_count; r_ay++)
{
cout << "\n " << fixed << id_num[r_ay] << " " << setprecision(2) << car[r_ay] << " " << setw(10) << price[r_ay] << " " << setw(10) << commission[r_ay];
cout << "\n ";
}
system("pause");
return 0;
}
}
void dashLine()
{
cout << "\n -----------------------------------";
}
int getSalesId()
{
int id_num;
cout << "\n Please enter Employee ID: ";
cin >> id_num;
while (id_num < 10000 || id_num > 99999)
{
cout << "\n Invalid Employee ID, Please enter a 5 digit ID ";
cin >> id_num;
}
return id_num;
}
int runProgram()
{
int rp_yesno;
cout << "\n Is there a customer? 1 = yes, 0 = no ";
cin >> rp_yesno;
while ((rp_yesno != 1) && (rp_yesno != 0))
{
cout << "\n Invalid Entry Please enter 1/0 ";
cout << "\n Is there a customer? 1 = yes 0 = no ";
cin >> rp_yesno;
}
return rp_yesno;
}
char vehicleType()
{
char car;
cout << "\n Please enter type of vehicle sold";
cout << "\n (S=standard, H=hybrid, E=electric): ";
cin >> car;
while (!((car == 'S') || (car == 's') || (car == 'h') || (car == 'H') || (car == 'E') || (car == 'e')))
{
cout << "\n Invalid input Please enter S/E/H ";
cin >> car;
}
return car;
}
double sellingPrice()
{
double price;
cout << "\n Please enter the selling price of the car:$ " << fixed << setprecision(2);
cin >> price;
while (price < 1)
{
cout << "\n Invalid entry, Please enter an amount greater than 0 ";
cin >> price;
}
return price;
}
void print(double largeSmallAverage)
{
double comm_s,
comm_l,
avg_comm;
{
cout << "\n ";
cout << "\n The smallest commission computed totals =$ " << fixed << setw(10) << comm_s;
cout << "\n The largest commission computed totals =$ " << fixed << setw(10) << comm_l;
cout << "\n Total average of commissions computed =$ " << fixed << setw(10) << avg_comm;
}
}
You would have a function called print that takes has three double parameters: void print(double small, double large, double average);.
Then later on you would call it with print(comm_s, comm_l, avg_comm);.
You need to change the definition of print:
void print(double small, double large, double average)
{
cout << "\n ";
cout << "\n The smallest commission computed totals =$ " << fixed << setw(10) << small;
cout << "\n The largest commission computed totals =$ " << fixed << setw(10) << large;
cout << "\n Total average of commissions computed =$ " << fixed << setw(10) << average;
}

Wrong variable in for...loop

Not a great title I know but I'm not sure how to word it. Anyway, in brief I am trying to calculate the number of scores, the total of scores and the average and grade of the scores. I am using for... loops to complete this task. So...
my function prototypes:
int validateNumber(int);
void summary(int,int,float,char);
char getGrade(float);
float getAvg(int,int);
probably only the validateNumber(int) is relevant here but just in case.
The main()
int num, total, scores;
cout << over4 << "How many scores do you want to average? " << endl;
cout << over4 << "Enter a value from 1 to 4: ";
cin >> num;
And the calls(?):
total = validateNumber(num);
summary(num,total,average,grade);
And then the definitions:
int validateNumber(int num)
{
int total = 0, score;
while (num < 1 || num > 4)
{
cout << over3 << num << " is not between 1 and 4! Try again: ";
cin >> num;
}
system("CLS");
for (int i = 1; i <= num; i++)
{
cout << over3 << "Enter score " << i << ": " << endl;
cout << over3 << "Enter a value from 0 to 100: ";
cin >> score;
while (score < 0 || score > 100)
{
cout << over3 << score << " is not between 0 and 100! Renter the score: "
<< i << ": ";
cin >> score;
}
total += score;
}
return total;
}
and:
void summary(int num,int total,float average,char grade)
{
cout << over4 << "Number of scores : " << num << endl;
cout << over4 << "Scores total : " << total << endl;
cout << over4 << "Average : " << average << "%" << endl;
cout << over4 << "Grade : " << grade << endl;
cout << down11;
}
When the user enters a num value between 1 and 4, there is no problem, the program works as it should. However when the user enters a value for num not in that range, the function works as it should BUT the summary will tell me that the number of scores was that first erroneous value and as a result mess up my average/grade.
in your function you are passing by value not by reference, so the change which is done in your function int validateNumber(int); is only in that function's scope, outside num's value is same as it was first. you should send value by reference. in this way :
int validateNumber(int &);
What happens is that you pass num to the validateNumber function by value. The local num in the validateNumber function copies the global num's value and get's processed accordingly. However, the global num variable stays as it were. In order to change the num itself you will have to pass it by reference. Change the parameters on your function definition to: int validateNumber(int &num) { ... }

Vector Errors in C++ Program [closed]

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