counting loops? - c++

i was creating a seating program and i was wonder if there was a way to count the loops and place it in a variable. im trying to let the user know how many tickets he purchased
#include <iostream>
#include <iomanip>
#include <string>
#include <istream>
#include <fstream>
using namespace std;
const int numberOfRow = 15;
const int numberOfCol = 20;
void print(char matrix[][20],int numberOfRow, int numberOfCol);
int main()
{
ifstream datafile;
int i, j;
char matrix[numberOfRow][numberOfCol], seat[numberOfRow][numberOfCol];
char option;
int row, col, totalsold;
float totSold, temp,price = 0 , ticketprice[numberOfRow], totRevenue;
bool another = true;
string filename;
datafile.open("c:\\price.dat");
for(i=0;i<numberOfRow;++i)
{
datafile >> temp;
ticketprice[i]=temp;
cout<< "Row ";
cout<< setw(2)<< fixed << setprecision(2)<< i << setw(7) << ticketprice[i]<< endl;
}
for(i = 0; i< numberOfRow; i++)
for(j = 0; j< numberOfCol; j++)
matrix[i][j] = '*';
print(matrix,numberOfRow, numberOfCol);
while(another)
{
totalsold = 0;
totRevenue = 0;
cout << "Please enter the row you would like to sit in: " << endl;
cin >> row;
cout << "Please enter the column you would like to sit in: " << endl;
cin >> col;
cout << "would you like to purchase more tickets? <y,n>" << endl;
cin >> option;
matrix[row][col] = '#';
/*totRevenue = totRevenue + ticketprice[row];*/
if(option == 'y' || option == 'Y')
{
another = true;
}
else
{
another = false;
print(matrix,numberOfRow, numberOfCol);
totRevenue = totRevenue + ticketprice[row];
}
}
totRevenue = totRevenue + ticketprice[row];
cout << "Total Tickets Sold: " << endl;// << totSold << endl;
cout << "Total Revenue: $ " << fixed << setprecision(2)<< totRevenue<< endl;
cin >> i;
cin.get();
return 0;
}
void print(char matrix[][20],int numberOfRow, int numberOfCol)
{
int row, col, i, j;
cout << "seat: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19"<< endl;
for(i = 0; i < numberOfRow; i++)
{
cout << "row" << setw(3)<< i;
for(j = 0; numberOfCol > j; j++)
cout << setw(3) << matrix[i][j];
cout << endl;
}
}

If you want to count the number of tickets bought, you should define a variable that contain the number of bought tickets and increment it right after another = true;.
if(option == 'y' || option == 'Y')
{
another = true;
++totSold;
}

Are you sure about your while loop ?
Something like this, would make more sens:
//...
int totalsold = 0;
float totRevenue = 0.0;
while(another)
{
cout << "Please enter the row you would like to sit in: " << endl;
cin >> row;
cout << "Please enter the column you would like to sit in: " << endl;
cin >> col;
cout << "would you like to purchase more tickets? <y,n>" << endl;
cin >> option;
matrix[row][col] = '#';
++totalsold; // increment the number of tickets sold
totRevenue += ticketprice[row]; // increment to total price of the tickets
if(option == 'n' || option == 'N')
{
another = false; // exit the loop
}
}
print(matrix,numberOfRow, numberOfCol);
cout << "Total Tickets Sold: " << totalsold << endl;
cout << "Total Revenue: $ " << fixed << setprecision(2)<< totRevenue<< endl;
//...
However, there is plenty of strange things in the provided code. But the most important is to keep practicing, so keep playing with the code and those strange things will disappear like magic with experience ;)

I think what you're looking for is a line like:
totSold += 1;
immediately after assigning # to the arena layout.
Note that you should check if the seat is sold already or not before selling it again. :)

Related

How to only print out highest/smallest array values in c++

Good day, I'm having difficulty on the last two parts of my program where it's supposed to only output players who got maximum/minimum scores, I need help on how to do it because I'm really confused. If it's also alright to provide some explanations I'd really appreciate it.
I tried this approach:
#include <iostream>
using namespace std;
int main() {
double lrgst, lrgst2, lrgst3;
int numbers[5];
lrgst = lrgst2 = lrgst3;
for (int i = 0; i < 5; i++) {
cin >> numbers[i];
}
for (int i = 0; i < 5; i++) {
if (numbers[i] > lrgst) {
lrgst3 = lrgst2;
lrgst2 = lrgst;
lrgst = numbers[i];
} else if (numbers[i] > lrgst2) {
lrgst3 = lrgst2;
lrgst2 = numbers[i];
} else if (numbers[i] > lrgst3) {
lrgst3 = numbers[i];
}
}
cout << "largest are: " << lrgst << " " << lrgst2 << " " << lrgst3;
}
this is my actual code:
#include <iostream>
using namespace std;
struct playerdata {
char name[50];
int age, score1, score2;
double average;
};
int main() {
int choice, i = 1, j = 1, z = 1, backtomain2;
char backtomain;
playerdata p1[10];
do {
for (int a = 0; a < 47; a++) {
cout << "=";
}
cout << "\n";
for (int b = 0; b < 22; b++) {
cout << " ";
if (b == 21) {
cout << "MENU \n";
}
}
for (int c = 0; c < 47; c++) {
ocut << "=";
}
cout << " "
"\n1. Add record\n"
"2. View players records\n"
"3. Compute for the average\n"
"4. Show the player(s) who gets the max average.\n"
"5. Show the player(s) who gets the min average.\n"
"6. Exit\n"
"Enter your choice:";
cin >> choice;
if (choice == 1) {
cout << "Add player data" << endl;
do {
cout << "Enter player " << i << " nickname:";
cin >> p1[i].name;
cout << "Enter player " << i << " age:";
cin >> p1[i].age;
cout << "Enter player " << i << " score 1:";
cin >> p1[i].score1;
cout << "Enter player " << i << " score 2:";
cin >> p1[i].score2;
cout << "Enter again? (Y/N)";
cin >> backtomain;
i++;
}
while (backtomain != 'N' && backtomain != 'n' && i < 7);
if (choice == 2) {
cout << "Player records" << endl;
cout << "Player nickname "
<< "Player age "
<< " player score 1"
<< "
player score 2\n ";
for (z = 1; z <= i - 1; z++) {
cout << p1[z].name << " " << p1[z].age << "" << p1[z].score1 << ""
<< p1[z].score2 << "\n";
}
cout << "Press 1 to go back to main menu\n";
cin >> backtomain;
}
if (choice == 3) {
cout << "Computing for average...\n";
for (int d = 1; d <= i - 1; d++) {
p1[d].average = (p1[d].score1 + p1[d].score2) / 2.0;
cout << "\n" << p1[d].average << "\n";
}
cout << "Press 1 to go back to main menu\n";
cin >> backtomain;
}
if (choice == 4) {
cout << "Player(s) who got the max average:\n";
cout << "\nPress 1 to go back to main menu";
cin >> backtomain;
}
if (choice == 5) {
cout << "player(s) who got the min average: \n";
cout << "Press 1 to go back to main menu";
cin >> backtomain;
}
}
while (choice != 6);
}
You can simply sort the array of players for that
int n = sizeof(p1)/ sizeof(p1[0]);
sort(p1, p1+n, compPlayer);
//larget at pl[0]
//smallest at pl[9]
where
bool compPlayer(playerdata p1, playerdata p2) {
return (p1.score1+p1.score2) > (p2.score1+p2.score2);
//use score incase average has not been calculated for all players yet
}

for loop with invalid error and looping questions

I am fairly new to C++, I am trying to write a code that allows input number of reviewers then allows number of reviewers to enter movie rating and display asterisks based on the number input. I am having difficulty incorporating an if statement that display "Movie ratings must be from 1 to 5." when the user input any number that's outside of 1 to 5. Another thing when it does work, it still continues the for loop of cout << "\nReviwer " << r << " rating: " ; instead of stopping and restarting. Any assistance is appreciated.
complied code
int reviewers;
int rating;
//input how many reviewers will be reviewing
cout << "How many reviewers? ";
cin >> reviewers;
cout << "Movie ratings must be from 1 to 5." << endl;
for (int r = 1; r <= reviewers; r++) {
cout << "\nReviwer " << r << " rating: ";
cin >> rating;
if (rating < 1 || rating > 5)
cout << "Movie ratings must be from 1 to 5." << endl;
else {
for (int j = 1; j <= rating; j++) {
cout << "* ";
} //end for
cout << endl;
} //end if
} //end for
Output example should be like this
You should have
#include <iostream>
#include <string>
using namespace std;
int main(){
int reviewers;
int rating;
//input how many reviewers will be reviewing
cout << "How many reviewers? ";
cin >> reviewers;
cout << "Movie ratings must be from 1 to 5." << endl;
for (int r = 1; r <= reviewers; r++) {
cout << "\nReviwer " << r << " rating: ";
cin >> rating;
while (rating < 1 || rating > 5){
cout << "Movie ratings must be from 1 to 5." << endl;
cout << "\nReviwer " << r << " rating: ";
cin >> rating;
}
if (rating >= 1 && rating <=5){
for (int j = 1; j <= rating; j++) {
cout << "* ";
} //end for
cout << endl;
} //end if
} //end for
}
};
It's not optimised but do the trick
if your mean is the reviewer must input the number between 1 and 5,
you can use a do while loop like this:
do
{
//enter rating;
}while (rating < 1||rating >5);
//print out rating;
I suppose you aim at something like this:
int reviewers;
int rating;
//input how many reviewers will be reviewing
cout << "How many reviewers? ";
cin >> reviewers;
cout << "Movie ratings must be from 1 to 5." << endl;
for (int r = 1; r <= reviewers; r++) {
while(true) {
cout << "\nReviewer " << r << " rating: ";
cin >> rating;
if (rating < 1 || rating > 5) {
cout << "Movie ratings must be from 1 to 5." << endl;
} else {
break;
}
}
for (int j = 1; j <= rating; j++) {
cout << "* ";
} //end for
cout << endl;
} //end for
Here is some code I quickly put together. I hope the comments are useful and if you have any questions just ask!
The main part is the recursive function get_ratings which will loop forever until it returns 1
// All this function does is returns the correct amount of stars
// E.G. make_stars(4) returns " * * * *"
string make_stars(int star_Count) {
string stars;
for (int i = 0 ; i < star_Count ; i++) {
stars += " *";
}
return stars;
}
// We get the ratings and returns 1 or 0 depending of it succeeded or failed
int get_ratings(int reviewer_count) {
// We initialise the ratings integer
int rating;
// We loop through all reviewers
for (int i = 0 ; i < reviewer_count ; i++) {
// We do i + 1 so it is more readable (usually in English we start at 1 not 0 like computers)
cout << "What is reviewer " << (i + 1) << "'s rating?" << endl;
//We get the user input
cin >> rating;
// We check to see if rating is withing the range. We could also do is NOT in the range and flip line 27 and 29
if (1 <= rating && rating <= 5) {
// If it is within range we will print the correct amount of stars
cout << make_stars(rating) << endl;
} else {
// We return 0 so we can determine the function "failed"
return 0;
}
}
// We return 1 so we can determine the function "succeeded"
return 1;
}
// This is a recursive function (it can run itself)
int get_ratings_rec(int reviewers) {
cout << "All ratings must be given between 1 and 5 (inclusive)" << endl;
// get_ratings_status is equal to 1/0 depending on if get_ratings() succeeded or failed
int get_ratings_status = get_ratings(reviewers);
if (get_ratings_status == 1) {
// If it was a success we print "Success!"
cout << "Success!" << endl;
} else {
// If it was a failure we tell the user and run get_ratings_loop() again until it succeeds
cout << "Failed, please try again\n" << endl;
get_ratings_loop(reviewers);
}
}
// Our main entry point to the program
int main() {
// We initialise the reviewers integer
int reviewers;
cout << "How many reviewers?\n>>> " << endl;
cin >> reviewers;
// We run get_ratings_loop() with the integer given
get_ratings_loop(reviewers);
}
You'd want to keep asking the number of stars if the user inputs a value outside the range, something like:
for (int r = 1; r <= reviewers; r++)
{
cout << "\nReviwer " << r << " rating: ";
do
{
cin >> rating;
if (rating < 1 || rating > 5) // print error message
cout << "Movie ratings must be from 1 to 5." << endl;
} while (rating < 1 || rating > 5); // repeat the loop if out of range
for (int j = 1; j <= rating; j++)
{
cout << "* ";
} //end for
cout << endl;
}
Note that you should be doing input validation also, e.g., if the input is an alphabetic character, your code will trigger an infinite loop, here an example of a possible solution:
#include <limits>
//...
do
{
if (!(cin >> rating))
{
std::cout << "Bad input, try again";
cin.clear(); //clear error flags
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // clear buffer
rating = 0;
}
if (rating < 1 || rating > 5)
cout << "Movie ratings must be from 1 to 5." << endl;
} while (rating < 1 || rating > 5);

How do I properly run if statements and do while loops with functions?

I have 5 programs that run perfectly fine individually, but when I combine them I get error messages and won't build. I have a menu to pick which program to run using if statements. Also a do while loop to repeat the programs. I believe it has something to do with the functions because I haven't had this problem before with simple programs. The program should first ask which program you want to run from the menu. It will run that program, then ask if you want to repeat.
I don't know what to try other than what is now in the program. I did take the do while loop out but still had the issue with the if statements.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>
#include <stdlib.h>
using namespace std;
int main()
{
int choice;
char repeat;
if (repeat == 'm' || 'M'){
cout << "1. Perfect Scores\n"
"2. Larger Than n\n"
"3. Roman Numeral Converter\n"
"4. Monkey Business\n"
"5. Lottery\n"
"6. Exit\n";
cout << "Pick which program you would like to run." << endl;
cin >> choice;
}
else if (choice == 1){ // -----------Perfect Scores------------
do{
int countPerfect(int a[])
{
int i=0;
for(int l=0; l<10; l++)
if(a[1]==100)
i++;
return i;
}
{
int score[10];
for(int i=0; i<10; i++)
{
cout << "Enter score " << i+1 << endl;
cin >> score[i];
while(score[i]<0 || score[i] > 100)
{
cout << "Enter score between 1 and 100." << endl;
cin >> score[i];
}
}
int n = countPerfect(score);
cout << "No of perfect scores: " << n << endl;
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
return 0;
}
}while(repeat == 'Y' || repeat == 'y');
}
// ------------Larger Than n--------------
else if (choice == 2){
do{
void display_greator(int A[], int size, int n)
{
int i;
for(i=0; i< size; i++)
{
if(A[i]>n)
{
cout << A[i] << endl;
}
}
}
int main(void)
{
int i, size;
cout << "Enter the size of your array:"<< endl;
cin >> size;
int N[size];
cout << "Enter a list of " << size << " numbers:" << endl;
for( i=0; i<size; i++)
{
cin >> N[i];
}
int num;
cout << "Enter your number n:" << endl;
cin >> num;
display_greator(N, size, num);
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
}
}while(repeat == 'Y' || repeat == 'y');
}
// ----------------------Roman Numeral Converter------------------
else if (choice == 3){
char repeat;
do{
{
int n;
string romanNumbers[]={"I", "II", "III", "IV", "V", "VI", "VII",
"VIII", "IX", "X", "XI", "XII",
"XIII", "VIX", "XV", "XVI", "XVII", "XVIII", "XIX", "XX"};
cout << "Enter a decimal number or enter 0 to quit." << endl;
cin >> n;
if(n==0)
exit(0);
do
{
cout << "Enter number between 1 and 20" << endl;
cout << "Enter number or enter 0 to quit" << endl;
cin >> n;
} while(n < 0 || n > 20);
{
cout << "Enter decimal equivalent roman number:" << endl;
cout << "Enter a number between 1 and 20:" << endl;
cin >> n;
if(n==0)
exit(0);
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
}
while(n > 0 || n < 20);
return 0;
}
}while(repeat == 'Y' || repeat == 'y');
}
// ---------------Monkey Business--------------------
else if(choice == 4){
do{
const int DAYS = 7;
double getTotalAmountOfFood(int[][DAYS],int);
double getLeastAmountOfFood(int[][DAYS],int, double);
double getGreatestAmountOfFood(int[][DAYS],int, double);
{
const int MONKEYS = 3;
double totalFood, averageFood, leastFood, greatestFood;
int foodInfo[MONKEYS][DAYS];
for(int i= 0; i< MONKEYS; i++)
{
cout << "Enter the food information of the monkey" <<
(i + 1) << ":" << endl;
for(int j = 0; j < DAYS; j++)
{
cout << "Day" << (j + 1) << ":" << endl;
cin >> foodInfo[i][j];
while(foodInfo[i][j] < 0)
{
cout << "Day " << (j+1) << ":" << endl;
cin >> foodInfo[i][j];
}
}
cout << endl;
}
totalFood = getTotalAmountOfFood(foodInfo, MONKEYS);
leastFood = getLeastAmountOfFood(foodInfo, MONKEYS, totalFood);
greatestFood = getGreatestAmountOfFood(foodInfo, MONKEYS, 0);
averageFood = totalFood / DAYS;
cout << "The average amount of food per day for three monkeys(in pounds):"
<< averageFood << endl;
cout << "The least amount of food per week for monkeys(in pounds) is:"
<< leastFood << endl;
cout << "The greatest amount of food per wek for a monkey is(in pounds):"
<< greatestFood << endl;
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
return 0;
}
double getTotalAmountOfFood(int food[][DAYS],int mnks)
{
double total = 0;
for(int i = 0; i < mnks; i++)
{
for(int j = 0; j < DAYS; j++)
{
total += food[i][j];
}
}
return total;
}
double getLeastAmountOfFood(int food[][DAYS], int mnks, double leastAmount)
{
double least = leastAmount;
double weekTotal;
for(int i = 0; i < mnks; i++)
{
weekTotal = 0;
for(int j = 0; j < DAYS; j++)
{
weekTotal += food[i][j];
}
if(least > weekTotal)
least = weekTotal;
}
return least;
}
double getGreatestAmountOfFood(int food[][DAYS], int mnks, double greatestAmount)
{
double greatest = greatestAmount;
double weekTotal;
for(int i = 0; i < mnks; i++)
{
weekTotal = 0;
for(int j = 0; j < DAYS; j++)
{
weekTotal +=food[i][j];
}
if(greatest < weekTotal)
greatest = weekTotal;
}
return greatest;
}
}while(repeat == 'Y' || repeat == 'y');
}
// ----------------Lottery--------------------
else if (choice == 5){
char repeat;
do{
srand(time(NULL));
int winningDigits[5];
int player[5];
int num;
int matchCount = 0;
for (int i = 0; i < 5; i++)
{
winningDigits[i] = rand() % 10;
}
cout << "Enter 5 integers in the range of 0 to 9." << endl;
for(int i = 0; i < 5; i++)
{
cout << "Number #" << (i + 1) << ": " << endl;
cin >> num;
while (num < 0 || num > 9)
{
cout << "Invalid number! It should be in the range of 0 through 9." << endl;
cout << "Number #" << (i + 1) << ": " << endl;
cin >> num;
}
player[i] = num;
}
for (int i = 0; i < 5; i++)
{
if (winningDigits[i] == player[i])
{
matchCount++;
}
}
cout << "Winning digits: " << endl;
for (int i = 0; i < 5; i++)
{
cout << winningDigits[i] << " " << endl;
}
cout << "Player's digits: " << endl;
for (int i = 0; i < 5; i++)
{
cout << player[i] << " " << endl;
}
cout << endl << endl << "Number of digits matched: "
<< matchCount << endl;
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
return 0;
}while(repeat == 'Y' || repeat == 'y');
}
else if (choice == 6)
{cout << "Bye" << endl;}
}
I'm expecting to be able to choose a program to run, repeat it, and repeat the entire program from main menu.
I am not going to point out all errors in your code, they are just too many. You went too fast too far. If you think the complexity of code and the errors it procudes are intimidating you are right. I wrote codes with more lines, but yours is too complicated for me. Go in small steps. Start with something along the line of:
int choose() { return 0; }
void func1() {}
void func2() {}
int main() {
int choice = 0;
while ( choice = choose() ) {
switch(choice) {
case 1 : func1(); break;
case 2 : func2(); break;
}
}
}
Your main function does not have to be more complex than that.
Write this, not more. Make sure it compiles, then in tiny steps fill the gaps, after each step compile, see if it does what you expect and only then continue to put more.
Some problems in your code (partly stolen from comments):
you cannot have more than one main
you cannot define functions inside functions
you cannot start an if statement with else if
int N[size]; is not standard C++ for a non-compile-time-constant size. Use std::vector instead
see here why using namespace std is considered bad practice
if (repeat == 'm' || 'M') is not doing what you expect, it should be if (repeat == 'm' || repeat == 'M'). In yours 'M' is taken as a bool which is always true (because it isnt 0).
make sure to initialize variables. Using variables that are not initialized causes undefined behaviour.
please next time reduce your code to a mcve and try to concentrate on a single problem, also include the error in the question
I cannot help myself than to point that for each single problem there are duplicate questions, which brings me back to: Don't do too many things at once. Fixing 100 errors at once is extremely difficult, fixing 1 error is doable.
last but not least, pay attention to compiler errors and warnings while you write the code (again: not after you wrote several pages, but after each single line)

2D array Seating Chart C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am studying c++ and I need to make a seating chart program. This is not my code as I am studying it to see how each part works. I am interested in the seating chart. I have tried many different things to try and figure this out myself, I have come close, having the Full (*) mark in the right column but it is never in the correct row.
If I choose seat # 1 and Row # 1 it makes the mark on the chart at seat and row #2.
Again, I am not using this code, I just learn better when I test things and see how they all work.
Here is the code
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int Show_Menu();
void Show_Chart();
const char FULL = '*';
const char EMPTY = '#';
const int rows = 11;
const int columns = 10;
char map[rows][columns];
double price;
int total = 0;
int seat = 90;
int seat2 = 0;
int Quit = 1;
int main()
{
const int Num_Rows = 11;
double price[Num_Rows];
int row2, column2, cost;
int answer;
//I have this blocked out as I am testing the chart only
/* cout << "Please enter price for each row." << endl;
for (int count = 0; count < rows; count++)
{
cout << "Row # " << (count + 1) << ": ";
cin >> price[count];
}
*/
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
map[i][j] = EMPTY;
}
int choice;
do
{
choice = Show_Menu();
switch (choice)
{
case 1:
cout << "View Seat Prices\n\n";
for (int count = 0; count < rows; count++)
{
cout << "The price for row " << (count + 1) << ": ";
cout << price[count] << endl;
}
break;
case 2:
cout << "Purchase a Ticket\n\n";
do
{
cout << "Please select the row you would like to sit in: ";
cin >> row2;
cout << "Please select the seat you would like to sit in: ";
cin >> column2;
if (map[row2][column2] == FULL)
{
cout << "Sorry that seat is sold-out, Please select a new
seat.";
cout << endl;
}
else
{
cost = price[row2] + 0;
total = total + cost;
cout << "That ticket costs: " << cost << endl;
cout << "Confirm Purchase? Enter (1 = YES / 2 = NO)";
cin >> answer;
if (answer == 1)
{
cout << "Your ticket purchase has been confirmed."
<< endl;
map[row2][column2] = FULL;
}
else if (answer == 2)
{
cout << "Would you like to look at another seat? (1 =
YES / 2 = NO)";
cout << endl;
cin >> Quit;
}
cout << "Would you like to look at another seat?(1 = YES / 2
= NO)";
cin >> Quit;
}
} while (Quit == 1);
break;
case 3:
cout << "View Available Seats\n\n";
Show_Chart();
break;
case 4:
cout << "Total ticket sales: " << total << ".\n\n";
break;
case 5:
cout << "quit\n";
break;
default: cout << "Error input\n";
}
} while (choice != 5);
return 0;
}
int Show_Menu()
{
int MenuChoice;
cout << endl << endl;
cout << " \tMAIN MENU\n";
cout << " 1. View Seat Prices.\n";
cout << " 2. Purchase a Ticket.\n";
cout << " 3. View Available Seats.\n";
cout << " 4. View Ticket Sales.\n";
cout << " 5. Quit the program.\n";
cout << "_____________________\n\n";
cout << "Please enter your choice: ";
cin >> MenuChoice;
cout << endl << endl;
return MenuChoice;
}
void Show_Chart()
{
cout << "Seats 1 2 3 4 5 6 7 8 9 ";
for (int row = 0; row < 10; row++)//rows
{
cout << endl << "Row " << (row + 1);
for (int columns = 0; columns < 9; columns++)
{
cout << " " << map[row][columns];
}
}
cout << endl;
}
If I choose seat # 1 and Row # 1 it makes the mark on the chart at
seat and row #2...
The problem is that arrays in C start with index 0, so the left most seat has index 0, not 1. Thus, if you enter 1 into row2, and write map[row2][column2] = FULL, then actually the second seat is marked.
A simple fix would be to write map[row2-1][column2-1]; but make sure that the user must not enter value 0 then.

C++ press enter to continue

Evening, I am looking for a way to get the program to continue on instead of exiting out after asking to press enter to continue. 1 I cannot use the list command because im calling function "seatingChart" in another function and having the list command sends me back into the menu. Any suggestions?
void seatingChart()
{
for(row = 0; SEATROWS > row; ++row) // Placeholder for '#'
for (seat = 0; SEATS > seat; ++seat) // Placeholder for '#'
theater[row][seat] = '#'; // Applying '#' to the chart
cout << "\n\t\tSeats";
cout << "\n 123456789012345678901234567890" << endl; //seat header
for (int row = 0; SEATROWS > row; ++row)
{ // Initializing 15 rows
cout << "\nRow " << setw(2) << row+1 << "\t";
for (int seat = 0; SEATS > seat; ++seat)
{ // Initializing 30 seats
cout << theater [row][seat];} //display seating chart
}
cout << "\n\n\n\tLegend:\t* = Sold";
cout << "\n\t\t# = Available";
cout << "\n\n\nPress the Enter key to continue.";
cin.ignore();
cin.get();
}
}
Entire code below: I get the issue when I input "1" at the menu in order to display the seating chart.
#include <iostream>
#include <iomanip>
using namespace std;
void list();
void getPrices();
void viewSales();
void seatingChart();
void ticketSales();
const int ROWS = 15;
const int COLS = 2;
double price[ROWS][COLS];
const int SEATROWS = 15; //PAT
const int SEATS = 30;//PAT
char theater[SEATROWS][SEATS];//PAT
int row;//PAT
int seat;//PAT
const char TAKEN = '*';//seats taken
const char EMPTY = '#';//seats free
int main()
{
int x; // Loop counter
for (x=0; x<ROWS;x++)
{
cout << "Please enter ticket price for Row " << setw(2) << (x + 1) << ": ";
cin >> price[x][COLS];
}
list();
return 0;
}
void list()
{
int choice;
cout << "\n\n\n\t\tC++ Theatre" << endl << endl;
cout << "\n\t1. View Available Seats";
cout << "\n\t2. View Seating Prices";
cout << "\n\t3. View Ticket Sales";
cout << "\n\t4. Purchase a Ticket";
cout << "\n\t5. Exit the Program\n\n";
cout << "\n\tEnter your choice(1-5): ";
cin>>choice;
while(choice>5 || choice<1)
{
cout<<"Choice must be between 1 and 5. Please re-enter:";
cin>>choice;
}
if (choice == 1)
seatingChart();
else if (choice == 2)
getPrices();
else if (choice == 3)
viewSales();
else if (choice == 4)
ticketSales();
}
void getPrices()
{
cout<<"\nTicket Prices By Row "<<endl;
cout<<" Row Price"<<endl;
cout<<" --- -----"<<endl;
for (int x= 0; x < ROWS; x++)
{
cout<<setw(8)<<x+1<<setw(10);
cout<<fixed<<showpoint<<setprecision(2)<<price[x][2]<<endl;
}
cout<<"\n\n\nPress the Enter key to continue.";
cin.ignore();
cin.get();
list();
}
void viewSales()
{
double sum=0;
cout<<"\n\nTotal Sales to Date: $"<<fixed<<showpoint<<setprecision(2)<<sum<<"\n\n";
list();
}
void seatingChart()
{
for(row = 0; SEATROWS > row; ++row) // Placeholder for '#'
for (seat = 0; SEATS > seat; ++seat) // Placeholder for '#'
theater[row][seat] = '#'; // Applying '#' to the chart
cout << "\n\t\tSeats";
cout << "\n 123456789012345678901234567890" << endl; //seat header
for (int row = 0; SEATROWS > row; ++row) { // Initializing 15 rows
cout << "\nRow " << setw(2) << row+1 << "\t";
for (int seat = 0; SEATS > seat; ++seat){ // Initializing 30 seats
cout << theater [row][seat];} //display seating chart
}
cout << "\n\n\n\tLegend:\t* = Sold";
cout << "\n\t\t# = Available";
cout << "\n\n\nPress the Enter key to continue.";
cin.ignore();
cin.get();
}
void ticketSales()
{
//*********************DISPLAY SEATING**********************************//
int row;
int seat;
char showSeating;
char anotherTicket = 'N';
int tickets = 0;
double totalPrice = 0;
cout << "\n\t\t C++ Theatre" << endl;
cout << "\t\tTicket Purchase Opportunity" << endl << endl;
cout << "Do you wish to view the chart of available seats \n"
<< "before making your selections (y/n)? ";
cin >> showSeating;
if (toupper(showSeating) == 'Y')
seatingChart();
/*------------------Working display and working taken------------------------------*/
do
{
cout << "\nPlease enter desired row number (1-" << ROWS << "): ";
cin >> row;
while (row < 1 || row > ROWS)
{
cout << "Row must be between 1 and " << ROWS << ". Please re-enter: ";
cin >> row;
}
cout << "\nPlease enter desired seat number (1-" << SEATS << "): ";
cin >> seat;
while (seat < 1 || seat > SEATS)
{
cout << "Seat must be between 1 and " << SEATS << ". Please re-enter: ";
cin >> seat;
}
row--; seat--; // row and seat indexing starts from 0
if(theater[row][seat] == TAKEN)
{
cout << "This seat is taken! Try another one. \n";
}
else{ // and if it is - sell the ticket
theater[row][seat]==TAKEN;
tickets++;
// Need to update seating chart upon purchase
totalPrice += price[row][COLS];
}
cout << "\nWould you like to purchase another seat (y/n)? ";
cin >> anotherTicket;
anotherTicket = toupper(anotherTicket);
}while (anotherTicket == 'Y');
cout << "\n\nYou have purchased a total of " << tickets << " tickets " << "for a total price of $" << totalPrice;
list();
}
Unless you wrap your entire menu code inside a loop and make the exit condition for this loop the user input(in your case typing 5 for exit) the program will terminate as soon as it returns from your seatingchart() function because the list() function will return to main(i.e seatingchart() returns to list() and list() will return to main() ). You should do something like this:
do
{
cout << "\n\n\n\t\tC++ Theatre" << endl << endl;
cout << "\n\t1. View Available Seats";
cout << "\n\t2. View Seating Prices";
cout << "\n\t3. View Ticket Sales";
cout << "\n\t4. Purchase a Ticket";
cout << "\n\t5. Exit the Program\n\n";
cout << "\n\tEnter your choice(1-5): ";
cin>>choice;
while(choice>5 || choice<1)
{
cout<<"Choice must be between 1 and 5. Please re-enter:";
cin>>choice;
}
if (choice == 1)
seatingChart();
else if (choice == 2)
getPrices();
else if (choice == 3)
viewSales();
else if (choice == 4)
ticketSales();
else if (choice==5)//this is your exit condition
break;//will break out of the menu loop
}while(1);//the is your menu loop
By the way there are some logical errors in your code, see the comment that are given in the comment section and correct them.