Vector Errors in C++ Program [closed] - c++

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

Related

gotoxy prints wrong in c++

I am making a menu for a store with 4 options; add product, delete product, see table of created products and exit the program.
I finished the option to add products and exit, they both work fine.
Now I have to make the option to see the product table, supposedly I had already finished it but I noticed a problem ...
The first time I go in to see the product table, it prints it to me normally, like this.
enter image description here
But when I go to the menu and go back to the product table, it prints it like this.
enter image description here
Like I said, it only prints it right the first time I see it, then it prints it wrong.
Do you know how I can solve it?
This is the part of the code where I print the product table.
void table()
{
int c = k, m = 7;
system("cls");
cout << endl;
cout << "Table of created products";
cout << endl;
cout << endl;
cout << " number of order ";
cout << "| Name of product |";
cout << " Product code |";
cout << " Amount |";
cout << " Unit price |";
cout << " Subtotal |";
cout << " IVA |";
cout << " total price |";
cout << endl;
cout << "-------------------------------------------------------------------------------------------------------------------------------------";
cout << endl;
for (int L = 2; L <= c; L++)
{
cout << " ";
cout << "| |";
cout << " |";
cout << " |";
cout << " |";
cout << " |";
cout << " |";
cout << " |";
cout << endl;
cout << "-------------------------------------------------------------------------------------------------------------------------------------";
cout << endl;
}
for (int h = 1; h < c; h++)
{
gotoxy(8, m);
cout << product[h].numor;
gotoxy(20, m);
cout << product[h].descr;
gotoxy(52, m);
cout << product[h].cod;
gotoxy(70, m);
cout << product[h].cant;
gotoxy(83, m);
cout << "$" << product[h].preuni;
gotoxy(96, m);
cout << "$" << product[h].subt;
gotoxy(107, m);
cout << "$" << product[h].IVA;
gotoxy(119, m);
cout << "$" << product[h].total;
m = m + 4;
}
cout << "\n\n\n";
system("pause");
system("cls");
}
This is my complete code (maybe I have one or another library too many because I have been experimenting).
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<string>
#include<ctype.h>
#include <stdio.h>
#include <windows.h>
#include<iomanip>
using namespace std;
struct products
{
int numor{},
cant{};
float preuni{},
subt{},
IVA{},
total{};
string cod{};
string descr{};
}product[51];
void add();
void intro_code();
int val_code(char codigo[]);
void table();
void gotoxy(int x, int y);
int i = 1; //Product No. (counter)
int k = 1; //Consecutive number (counter)
int main()
{
char opc;
cout << " Welcome ";
cout << endl;
cout << "\n1.- Add product.";
cout << "\n2.- Delete product.";
cout << "\n3.- Table of created products.";
cout << "\n4.- Exit";
cout << endl;
cout << "\nWhat do you want to do today?: "; cin >> opc;
switch (opc)
{
case '1':
system("cls");
cout << "\nAdd product";
add();
system("pause");
system("cls");
return main();
case '2':
cout << "\nDelete product";
system("cls");
return main();
case '3':
table();
return main();
case '4':
exit(EXIT_SUCCESS);
default:
system("cls");
cout << "Warning: the data entered is not correct, try again.\n\n";
return main();
}
return 0;
}
void add()
{
int can;
cout << "\nHow many products do you want to register?: "; cin >> can;
if (can <= 0 || can > 51)
{
system("cls");
cout << "Warning: The amount of products entered is not valid, try again";
return add();
}
for (int p = 1; p <= can;)
{
if (i < 51)
{
if (k <= 51) // In this part, consecutive numbers are generated automatically
{
cout << "\nNumber of order: ";
cout << k;
cout << endl;
product[i].numor = k;
k++;
}
cin.ignore();
cout << "Name of product: ";
getline(cin, product[i].descr);
intro_code();
cout << "Quantity to sell: "; cin >> product[i].cant;
cout << "unit price: $"; cin >> product[i].preuni;
product[i].subt = product[i].preuni * product[i].cant;
cout << "Subtotal: $" << product[i].subt;
product[i].IVA = product[i].subt * 0.16;
cout << "\nIVA: $" << product[i].IVA;
product[i].total = product[i].subt + product[i].IVA;
cout << "\nTotal price: $" << product[i].total;
cout << "\n-------------------------------------------------------------------------------";
cout << endl;
i++;
}
p++;
}
}
/*In this function the product code is entered, if the user enters a code
less than or greater than 5 characters the program displays an error message and allows the
user try again.*/
void intro_code()
{
char hello[10];
int xyz;
do {
cout << "Code of product (5 digits): ";
cin.getline(hello, 10, '\n');
if (strlen(hello) != 5)
{
cout << "The data entered is incorrect, try again ...";
cout << endl;
return intro_code();
}
else
{
xyz = val_code(hello);
}
} while (xyz == 0);
product[i].cod = hello;
}
/*As the requirement for "product code" is that it does not contain letters, special characters or blank spaces
create the function val_code to go through each character entered in the code, check character by character with the isdigit function
to see if the character is numeric or not, if it is not numeric it prints a warning message and allows the user to try again
without leaving the console.*/
int val_code(char hello[]) {
int abc;
for (abc = 0; abc < strlen(hello); abc++) {
if (!(isdigit(hello[abc]))) {
cout << "The data entered is incorrect, try again ...";
cout << endl;
return 0;
}
}
return 1;
}
void table()
{
int c = k, m = 7;
system("cls");
cout << endl;
cout << "Table of created products";
cout << endl;
cout << endl;
cout << " number of order ";
cout << "| Name of product |";
cout << " Product code |";
cout << " Amount |";
cout << " Unit price |";
cout << " Subtotal |";
cout << " IVA |";
cout << " total price |";
cout << endl;
cout << "-------------------------------------------------------------------------------------------------------------------------------------";
cout << endl;
for (int L = 2; L <= c; L++)
{
cout << " ";
cout << "| |";
cout << " |";
cout << " |";
cout << " |";
cout << " |";
cout << " |";
cout << " |";
cout << endl;
cout << "-------------------------------------------------------------------------------------------------------------------------------------";
cout << endl;
}
for (int h = 1; h < c; h++)
{
gotoxy(8, m);
cout << product[h].numor;
gotoxy(20, m);
cout << product[h].descr;
gotoxy(52, m);
cout << product[h].cod;
gotoxy(70, m);
cout << product[h].cant;
gotoxy(83, m);
cout << "$" << product[h].preuni;
gotoxy(96, m);
cout << "$" << product[h].subt;
gotoxy(107, m);
cout << "$" << product[h].IVA;
gotoxy(119, m);
cout << "$" << product[h].total;
m = m + 4;
}
cout << "\n\n\n";
system("pause");
system("cls");
}
void gotoxy(int x, int y)
{
HANDLE hcon;
hcon = GetStdHandle(STD_OUTPUT_HANDLE);
COORD dwPos;
dwPos.X = x;
dwPos.Y = y;
SetConsoleCursorPosition(hcon, dwPos);
}
Thank you very much in advance :)
The main reason for this problem is that after the size of the console changes, the position of the symbols you draw has also changed. At the same time, the line break determined by cout<<endl; also has a problem. For example, after the window becomes larger, the line length of the console becomes larger, but cout<<endl; is still the original line length.
However, there is no good solution. I suggest you directly set the console size and m=5, m=m+2.
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<string>
#include<ctype.h>
#include <stdio.h>
#include <windows.h>
#include<iomanip>
using namespace std;
struct products
{
int numor{},
cant{};
float preuni{},
subt{},
IVA{},
total{};
string cod{};
string descr{};
}product[51];
void add();
void intro_code();
int val_code(char codigo[]);
void table();
void gotoxy(int x, int y);
int i = 1; //Product No. (counter)
int k = 1; //Consecutive number (counter)
int main()
{
HWND console = GetConsoleWindow();
RECT r;
GetWindowRect(console, &r);
MoveWindow(console, r.left, r.top, 1400, 400, TRUE);
char opc;
cout << " Welcome ";
cout << endl;
cout << "\n1.- Add product.";
cout << "\n2.- Delete product.";
cout << "\n3.- Table of created products.";
cout << "\n4.- Exit";
cout << endl;
cout << "\nWhat do you want to do today?: "; cin >> opc;
switch (opc)
{
case '1':
system("cls");
cout << "\nAdd product";
add();
system("pause");
system("cls");
return main();
case '2':
cout << "\nDelete product";
system("cls");
return main();
case '3':
table();
return main();
case '4':
exit(EXIT_SUCCESS);
default:
system("cls");
cout << "Warning: the data entered is not correct, try again.\n\n";
return main();
}
return 0;
}
void add()
{
int can;
cout << "\nHow many products do you want to register?: "; cin >> can;
if (can <= 0 || can > 51)
{
system("cls");
cout << "Warning: The amount of products entered is not valid, try again";
return add();
}
for (int p = 1; p <= can;)
{
if (i < 51)
{
if (k <= 51) // In this part, consecutive numbers are generated automatically
{
cout << "\nNumber of order: ";
cout << k;
cout << endl;
product[i].numor = k;
k++;
}
cin.ignore();
cout << "Name of product: ";
getline(cin, product[i].descr);
intro_code();
cout << "Quantity to sell: "; cin >> product[i].cant;
cout << "unit price: $"; cin >> product[i].preuni;
product[i].subt = product[i].preuni * product[i].cant;
cout << "Subtotal: $" << product[i].subt;
product[i].IVA = product[i].subt * 0.16;
cout << "\nIVA: $" << product[i].IVA;
product[i].total = product[i].subt + product[i].IVA;
cout << "\nTotal price: $" << product[i].total;
cout << "\n-------------------------------------------------------------------------------";
cout << endl;
i++;
}
p++;
}
}
/*In this function the product code is entered, if the user enters a code
less than or greater than 5 characters the program displays an error message and allows the
user try again.*/
void intro_code()
{
char hello[10];
int xyz;
do {
cout << "Code of product (5 digits): ";
cin.getline(hello, 10, '\n');
if (strlen(hello) != 5)
{
cout << "The data entered is incorrect, try again ...";
cout << endl;
return intro_code();
}
else
{
xyz = val_code(hello);
}
} while (xyz == 0);
product[i].cod = hello;
}
/*As the requirement for "product code" is that it does not contain letters, special characters or blank spaces
create the function val_code to go through each character entered in the code, check character by character with the isdigit function
to see if the character is numeric or not, if it is not numeric it prints a warning message and allows the user to try again
without leaving the console.*/
int val_code(char hello[]) {
int abc;
for (abc = 0; abc < strlen(hello); abc++) {
if (!(isdigit(hello[abc]))) {
cout << "The data entered is incorrect, try again ...";
cout << endl;
return 0;
}
}
return 1;
}
void table()
{
int c = k, m = 5;
system("cls");
cout << endl;
cout << "Table of created products";
cout << endl;
cout << endl;
cout << " number of order ";
cout << "| Name of product |";
cout << " Product code |";
cout << " Amount |";
cout << " Unit price |";
cout << " Subtotal |";
cout << " IVA |";
cout << " total price |";
cout << endl;
cout << "-------------------------------------------------------------------------------------------------------------------------------------";
cout << endl;
for (int L = 2; L <= c; L++)
{
cout << " ";
cout << "| |";
cout << " |";
cout << " |";
cout << " |";
cout << " |";
cout << " |";
cout << " |";
cout << endl;
cout << "-------------------------------------------------------------------------------------------------------------------------------------";
cout << endl;
}
for (int h = 1; h < c; h++)
{
gotoxy(8, m);
cout << product[h].numor;
gotoxy(20, m);
cout << product[h].descr;
gotoxy(52, m);
cout << product[h].cod;
gotoxy(70, m);
cout << product[h].cant;
gotoxy(83, m);
cout << "$" << product[h].preuni;
gotoxy(96, m);
cout << "$" << product[h].subt;
gotoxy(107, m);
cout << "$" << product[h].IVA;
gotoxy(119, m);
cout << "$" << product[h].total;
m = m + 2;
}
cout << "\n\n\n";
system("pause");
system("cls");
}
void gotoxy(int x, int y)
{
HANDLE hcon;
hcon = GetStdHandle(STD_OUTPUT_HANDLE);
COORD dwPos;
dwPos.X = x;
dwPos.Y = y;
SetConsoleCursorPosition(hcon, dwPos);
}
In addition, it is not recommended that you use cmd to make gui.

C++ Iterating a menu until all choices have been selected

I am working on my final project for a c++ class. My program is an Inventory-taking console app. So far I have been able to get the program to fully run as intended through each item and each size under the item. I added a menu to the program for the employee to be able to choose which item they want to do inventory for. I would like this menu to keep being displayed until ALL choices (items) have been "accounted for". I did a switch case which works but it does NOT iterate through all items, but instead only through the single item selected. How can I make the menu loop until all items have been accounted for? (as this is the point for doing inventory at a retail store)
here is my code:
//Name: Abdul Tabel
//Inventory program
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <fstream>
#include <cstdlib>
using namespace std;
// clothing item class declaration
class Item
{
private:
double small;
double medium;
double large;
public:
void setSmall(double);
void setMedium(double);
void setLarge(double);
double getRem() const;
double getSmall() const;
double getMedium() const;
double getLarge() const;
double allRem() const;
};
//menu function
void showMenu()
{
cout << "Please choose an item from the menu to do inventory for\n Only choose each item one time!";
cout << "\nType 'A' for Shirts";
cout << "\nType 'B' for Pants";
cout << "\nType 'C' Shoes";
cout << "\nType 'D' to quit the program" << endl;
}
//assign value to small item
void Item::setSmall(double null)
{
small = null;
}
//assign value to medium item
void Item::setMedium(double null)
{
medium = null;
}
//assign value to large item
void Item::setLarge(double null)
{
large = null;
}
//gets value from small variable
double Item::getSmall() const
{
return small;
}
//gets value from medium variable
double Item::getMedium() const
{
return medium;
}
//gets value from large variable
double Item::getLarge() const
{
return large;
}
//gets total of reamining items
double Item::allRem() const
{
return small + medium + large;
}
int main()
{
Item shirt;
Item pants;
Item shoes;
double number;
double totalRemaining;
char selection;
// constants for menu choice
const char choice_a = 'A',
choice_b = 'B',
choice_c = 'C',
quit_choice = 'D';
// set output format
cout << fixed << showpoint << setprecision(2);
//show menu
showMenu();
cin >> selection;
switch (selection) // respond to the user's menu selection
{
case choice_a: // get shirt item inventory
cout << "Enter how many small shirts are left? ";
cin >> number;
shirt.setSmall(number);
cout << "Enter how many medium shirts are left? ";
cin >> number;
shirt.setMedium(number);
cout << "Enter how many large shirts are left? ";
cin >> number;
shirt.setLarge(number);
break;
case choice_b: // get pants item inventory
cout << endl << "Enter how many small pants are left? ";
cin >> number;
pants.setSmall(number);
cout << "Enter how many medium pants are left? ";
cin >> number;
pants.setMedium(number);
cout << "Enter how many large pants are left? ";
cin >> number;
pants.setLarge(number);
break;
case choice_c: // get shoes item inventory
cout << endl << "Enter how many small shoes are left? ";
cin >> number;
shoes.setSmall(number);
cout << "Enter how many medium shoes are left? ";
cin >> number;
shoes.setMedium(number);
cout << "Enter how many large shoes are left? ";
cin >> number;
shoes.setLarge(number);
break;
case quit_choice:
cout << "Program ending.\n";
cin.get(); cin.get();
return 0;
break;
default:
cout << "The valid choices are A through D. Run the\n"
<< "program again and select one of those.\n";
cin.get(); cin.get();
return 0;
}
//being displaying inventory results
cout << endl << "\n*******************";
cout << endl << "*Inventory results*";
cout << endl << "*******************" << endl;
//displays shirt results
if (shirt.getSmall() < 5)
cout << endl << "There are " << shirt.getSmall() << " small shirts left. ORDER MORE!" << endl;
else
cout << endl << "There are " << shirt.getSmall() << " small shirts left." << endl;
if (shirt.getMedium() < 5)
cout << "There are " << shirt.getMedium() << " medium shirts left. ORDER MORE!" << endl;
else
cout << "There are " << shirt.getMedium() << " medium shirts left." << endl;
if (shirt.getLarge() < 5)
cout << "There are " << shirt.getLarge() << " large shirts left. ORDER MORE!" << endl;
else
cout << "There are " << shirt.getLarge() << " large shirts left." << endl;
cout << "There are a total of " << shirt.allRem() << " shirts left." << endl;
// displays pant results
if (pants.getSmall() < 5)
cout << endl << "There are " << pants.getSmall() << " small pants left. ORDER MORE!" << endl;
else
cout << endl << "There are " << pants.getSmall() << " small pants left." << endl;
if (pants.getMedium() < 5)
cout << "There are " << pants.getMedium() << " medium pants left. ORDER MORE!" << endl;
else
cout << "There are " << pants.getMedium() << " medium pants left." << endl;
if (pants.getLarge() < 5)
cout << "There are " << pants.getLarge() << " large pants left. ORDER MORE!" << endl;
else
cout << "There are " << pants.getLarge() << " large pants left." << endl;
cout << "There are a total of " << pants.allRem() << " pants left." << endl;
// displays shoe results
if (shoes.getSmall() < 5)
cout << endl << "There are " << shoes.getSmall() << " small shoes left. ORDER MORE!" << endl;
else
cout << endl << "There are " << shoes.getSmall() << " small shoes left." << endl;
if (shoes.getMedium() < 5)
cout << "There are " << shoes.getMedium() << " medium shoes left. ORDER MORE!" << endl;
else
cout << "There are " << shoes.getMedium() << " medium shoes left." << endl;
if (shoes.getLarge() < 5)
cout << "There are " << shoes.getLarge() << " large shoes left. ORDER MORE!" << endl;
else
cout << "There are " << shoes.getLarge() << " large shoes left." << endl;
cout << "There are a total of " << shoes.allRem() << " shoes left." << endl;
cin.get(); cin.get();
return 0;
}
cin.get(); cin.get();
return 0;
}
PS: without the switch case, I got the program to successfully iterate through each single item and display all results without any "garbage" results. The garbage results only appeared after I did the switch case, as the inputs only account for one single item.
PSS: I also tried a do while loop enclosing the switch case, but it was not working as intended. I am open to different types of solutions and it does NOT have to end up being switch case, although if there is an easy way to incorporate switch case, it is preferable.
THANK YOU!
EDIT: HERE IS THE CLEAN CODE WITHOUT SWITCH CASE OR MENU
//Name: Abdul Tabel
//Inventory program
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <fstream>
#include <cstdlib>
using namespace std;
// clothing item class declaration
class Item
{
private:
double small;
double medium;
double large;
public:
void setSmall(double);
void setMedium(double);
void setLarge(double);
double getRem() const;
double getSmall() const;
double getMedium() const;
double getLarge() const;
double allRem() const;
};
//menu function
void showMenu()
{
cout << "Please enter the items remaining for the following items" << endl;
}
//assign value to small item
void Item::setSmall(double null)
{
small = null;
}
//assign value to medium item
void Item::setMedium(double null)
{
medium = null;
}
//assign value to large item
void Item::setLarge(double null)
{
large = null;
}
//gets value from small variable
double Item::getSmall() const
{
return small;
}
//gets value from medium variable
double Item::getMedium() const
{
return medium;
}
//gets value from large variable
double Item::getLarge() const
{
return large;
}
//gets total of reamining items
double Item::allRem() const
{
return small + medium + large;
}
int main()
{
Item shirt;
Item pants;
Item shoes;
double number;
double totalRemaining;
char selection;
// set output format
cout << fixed << showpoint << setprecision(2);
//show menu
showMenu();
cout << "Enter how many small shirts are left? ";
cin >> number;
shirt.setSmall(number);
cout << "Enter how many medium shirts are left? ";
cin >> number;
shirt.setMedium(number);
cout << "Enter how many large shirts are left? ";
cin >> number;
shirt.setLarge(number);
cout << endl << "Enter how many small pants are left? ";
cin >> number;
pants.setSmall(number);
cout << "Enter how many medium pants are left? ";
cin >> number;
pants.setMedium(number);
cout << "Enter how many large pants are left? ";
cin >> number;
pants.setLarge(number);
cout << endl << "Enter how many small shoes are left? ";
cin >> number;
shoes.setSmall(number);
cout << "Enter how many medium shoes are left? ";
cin >> number;
shoes.setMedium(number);
cout << "Enter how many large shoes are left? ";
cin >> number;
shoes.setLarge(number);
//being displaying inventory results
cout << endl << "\n*******************";
cout << endl << "*Inventory results*";
cout << endl << "*******************" << endl;
//displays shirt results
if (shirt.getSmall() < 5)
cout << endl << "There are " << shirt.getSmall() << " small shirts left. ORDER MORE!" << endl;
else
cout << endl << "There are " << shirt.getSmall() << " small shirts left." << endl;
if (shirt.getMedium() < 5)
cout << "There are " << shirt.getMedium() << " medium shirts left. ORDER MORE!" << endl;
else
cout << "There are " << shirt.getMedium() << " medium shirts left." << endl;
if (shirt.getLarge() < 5)
cout << "There are " << shirt.getLarge() << " large shirts left. ORDER MORE!" << endl;
else
cout << "There are " << shirt.getLarge() << " large shirts left." << endl;
cout << "There are a total of " << shirt.allRem() << " shirts left." << endl;
// displays pant results
if (pants.getSmall() < 5)
cout << endl << "There are " << pants.getSmall() << " small pants left. ORDER MORE!" << endl;
else
cout << endl << "There are " << pants.getSmall() << " small pants left." << endl;
if (pants.getMedium() < 5)
cout << "There are " << pants.getMedium() << " medium pants left. ORDER MORE!" << endl;
else
cout << "There are " << pants.getMedium() << " medium pants left." << endl;
if (pants.getLarge() < 5)
cout << "There are " << pants.getLarge() << " large pants left. ORDER MORE!" << endl;
else
cout << "There are " << pants.getLarge() << " large pants left." << endl;
cout << "There are a total of " << pants.allRem() << " pants left." << endl;
// displays shoe results
if (shoes.getSmall() < 5)
cout << endl << "There are " << shoes.getSmall() << " small shoes left. ORDER MORE!" << endl;
else
cout << endl << "There are " << shoes.getSmall() << " small shoes left." << endl;
if (shoes.getMedium() < 5)
cout << "There are " << shoes.getMedium() << " medium shoes left. ORDER MORE!" << endl;
else
cout << "There are " << shoes.getMedium() << " medium shoes left." << endl;
if (shoes.getLarge() < 5)
cout << "There are " << shoes.getLarge() << " large shoes left. ORDER MORE!" << endl;
else
cout << "There are " << shoes.getLarge() << " large shoes left." << endl;
cout << "There are a total of " << shoes.allRem() << " shoes left." << endl;
cin.get(); cin.get();
return 0;
}
I've posted an Answer to my own question at the bottom. I scrapped the menu idea and went with something else. How do I mark this question as resolved?
Thanks to all that helped!
There is copy-paste mistake in your code
In the problem description you say "I added a menu to the program for the employee to be able to choose which item they want to do inventory for. I would like this menu to keep being displayed until ALL choices (items) have been accounted for".So whats the point to give them a choice to choose which item they would want to do inventory for if they MUST choose all the items before showing the result?I suggest you remove the switch case and put the "shirts", "pants" and "shoes" items one by one. There is no need for switch case.
I've scrapped the whole menu idea and added different loops and methods to make my code longer. I added a class that produces an order to be placed for the missing inventory items.
here is my final project completed. Thank you for all that tried to help!
//Name: Abdul Tabel
//Inventory program
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <fstream>
#include <cstdlib>
#include <array>
#include <list>
using namespace std;
// clothing item class declaration
class Item
{
private:
double small;
double medium;
double large;
string description;
public:
void setDescription(string);
void setSmall(double);
void setMedium(double);
void setLarge(double);
string getDescription() const;
double getRem() const;
double getSmall() const;
double getMedium() const;
double getLarge() const;
double allRem() const;
bool orderMore(int);
/*Item();*/
};
class Order
{
private:
string description;
string size;
double qty;
int OrderNumber;
public:
void setDescription(string);
void setSize(string);
void setQty(double);
void setOrderNumber();
string getDescription() const;
string getSize() const;
double getQty() const;
int getOrderNumber() const;
};
//methods for Order class
string Order::getDescription() const
{
return description;
}
string Order::getSize() const
{
return size;
}
double Order::getQty() const
{
return qty;
}
int Order::getOrderNumber() const
{
return OrderNumber;
}
void Order::setDescription(string x)
{
description = x;
}
void Order::setSize(string x)
{
size = x;
}
void Order::setQty(double x)
{
qty = x;
}
void Order::setOrderNumber()
{
OrderNumber = (rand() % 900) + 100;
}
bool Item::orderMore(int Count)
{
bool returnValue = false;
if (Count < 5)
{
returnValue = true;
}
return returnValue;
}
//menu function
void showMenu()
{
cout << "Please enter the items remaining for the following items" << endl;
}
//assign value to small item
void Item::setDescription(string x)
{
description = x;
}
//assign value to small item
void Item::setSmall(double number)
{
small = number;
}
//assign value to medium item
void Item::setMedium(double number)
{
medium = number;
}
//assign value to large item
void Item::setLarge(double number)
{
large = number;
}
string Item::getDescription() const
{
return description;
}
//gets value from small variable
double Item::getSmall() const
{
return small;
}
//gets value from medium variable
double Item::getMedium() const
{
return medium;
}
//gets value from large variable
double Item::getLarge() const
{
return large;
}
//gets total of reamining items
double Item::allRem() const
{
return small + medium + large;
}
int main()
{
double number;
std::array <string,4> itemType = {"shirts", "pants", "shoes", "coats"};
//dynamic allocation of array size
Item *items;
items = new Item[itemType.size()];
//for loop will iterate through all items in array
showMenu();
for(int i=0; i < itemType.size(); i++)
{
items[i].setDescription(itemType[i]);
cout << "Enter how many small " + itemType[i] + " are left? ";
cin >> number;
items[i].setSmall(number);
cout << "Enter how many medium " + itemType[i] + " are left? ";
cin >> number;
items[i].setMedium(number);
cout << "Enter how many large " + itemType[i] + " are left? ";
cin >> number;
items[i].setLarge(number);
}
//being displaying inventory results
cout << endl << "\n*******************";
cout << endl << "*Inventory results*";
cout << endl << "*******************" << endl;
// dynamically creates a list for the unknown orders
list<Order> orders;
// Output the quantitis entered back to the user and create orders for the ones that need orders.
for(int i=0; i < itemType.size(); i++)
{
cout << endl << "There are " << items[i].getSmall() << " small " << items[i].getDescription() << " left. ";
if (items[i].orderMore(items[i].getSmall()))
{
cout << " Automatically creating order for this product";
Order m;
m.setDescription(items[i].getDescription());
m.setQty(5-items[i].getSmall());
m.setSize("small");
m.setOrderNumber();
orders.push_front(m);
}
cout << endl << "There are " << items[i].getMedium() << " medium " << items[i].getDescription() << " left.";
if (items[i].orderMore(items[i].getMedium()))
{
cout << " Automatically creating order for this product";
Order m;
m.setDescription(items[i].getDescription());
m.setQty(5-items[i].getMedium());
m.setSize("medium");
m.setOrderNumber();
orders.push_front(m);
}
cout << endl << "There are " << items[i].getLarge() << " large " << items[i].getDescription() << " left.";
if (items[i].orderMore(items[i].getLarge()))
{
cout << " Automatically creating order for this product";
Order m;
m.setDescription(items[i].getDescription());
m.setQty(5-items[i].getLarge());
m.setSize("large");
m.setOrderNumber();
orders.push_front(m);
}
cout << endl << "There are " << items[i].allRem() << " total " << items[i].getDescription() << " left." << endl;
}
cout << endl << "\n*******************";
cout << endl << "* Order results *";
cout << endl << "*******************" << endl;
list<Order>::iterator iter; //iterator is used to iterate through all objects in the list
// loop for orders
for (iter = orders.begin(); iter != orders.end(); iter++)
{
cout << endl << "Order placed for " << iter->getQty() << " " << iter->getSize() << " " << iter->getDescription() << " on order number: " << iter->getOrderNumber();
}
cin.get(); cin.get();
return 0;
}

Invalid conversion from 'const char*' to 'char'

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");
}

Serendipity booksellers software program C++

this is a project I'm working on which comes from the book I'm using to learn C++ - "Starting out with C++". I'm having a problem with the cashier portion of the project at the moment. It asks the user to enter the date, quantity, isbn, title, and price of the book. Then, it asks the user if they wish to enter another book. Regardless of whether they type "y" or "n" it continues to the next part of the program. I don't really know why the for loop doesn't repeat after I type "y" to enter another book. Also, the date is coming out with garbage at the end when it is displayed, that's another thing I need to fix. Any help would be appreciated. There is definitely more problems but the main problem is in the cashier function in the first for loop. I didn't include the whole program because it's very long.
/*
* mainmenu.cpp
* Serendipity Booksellers software
*
* Created by Abraham Quilca on 9/5/12.
* Copyright 2012 __MyCompanyName__. All rights reserved.
*
*/
#include<iostream>
#include<iomanip>
#include<cstring>
#include"mainmenu.h"
using namespace std;
char bookTitle[20][51],
isbn[20][14],
author[20][31],
publisher[20][31],
dateAdded[20][11];
int qtyOnHand[20];
double wholesale[20];
double retail[20];;
int main()
{
int choice;
do
{
cout << "\t\t Serendipity Booksellers"<< endl;
cout << "\t\t\t Main Menu" << endl << endl;
cout << "\t\t1. Cashier Module" << endl;
cout << "\t\t2. Inventory Database Module" << endl;
cout << "\t\t3. Report Module" << endl;
cout << "\t\t4. Exit" << endl << endl;
cout << "\t\tEnter your choice: ";
cin >> choice;
cout << endl;
switch (choice)
{
case 1:
cashier();
break;
case 2:
invmenu();
break;
case 3:
reports();
break;
case 4:
continue;
break;
default:
cout << "\t\tPlease enter a number in the range 1-4." << endl << endl;
}
}
while(choice != 4);
cout << "\t\tYou selected item 4." << endl;
return 0;
}
// Cashier function
void cashier()
{
char again;
char date[8];
int quantity[20] = {0};
char ISBN[20][20] = {0};
char title[20][40] = {0};
float price[20] = {0}, bookTotal[20] = {0}, subtotal, total, tax;
const float tax_rate = .06;
cout << "Serendipity Booksellers" << endl;
cout << " Cashier Module" << endl << endl;
for(int count = 0; count < 20; count++)
{
cout << "Date: ";
cin >> date;
cout << "Quantity of Book: ";
cin >> quantity[count];
cout << "ISBN: ";
cin >> ISBN[count];
cout << "Title: ";
cin.ignore();
cin.getline(title[count], 40);
cout << "Price: ";
cin >> price[count];
bookTotal[count] = quantity[count] * price[count];
subtotal += price[count];
cout << "Would you like to enter another book? (Y/N) ";
cin >> again;
if(again == 'N' || 'n')
count = 21; // This line will end the for loop
}
// Calculating tax and total
tax = subtotal * tax_rate;
total = subtotal + tax;
cout << "\n\nSerendipity Booksellers" << endl << endl;
cout << "Date:" << date << endl << endl;
cout << "Qty\t ISBN\t\t "
<< left << setw(40) << "Title" << "Price\t Total" << endl
<< "-------------------------------------------------------------------------------"
<< endl << endl;
for(int count = 0; count < 20; count++)
{
cout << quantity[count] << "\t " << ISBN[count] << " " << left << setw(40) << title[count]
<< setprecision(2) << fixed << "$" << setw(6) << price[count] << " $" << setw(6) << bookTotal[count]
<< endl << endl;
}
cout << "\t\t\t Subtotal" << "\t\t\t\t $" << setw(6) << subtotal << endl;
cout << "\t\t\t Tax" << "\t\t\t\t $" << setw(6) << tax<< endl;
cout << "\t\t\t Total" "\t\t\t\t $" << setw(6) << total << endl << endl;
cout << "Thank You for Shopping at Serendipity!" << endl << endl;
}
if(again == 'N' || 'n')
This doesn't do what you think it does. Look at it like this:
if((again == 'N') || ('n'))
Is again == N true OR is n true? Well n will always be true (it is a char with non-zero value) so your loop will always end immediately. What you want is:
if(again == 'N' || again == 'n')
Also, you can break out of a loop using the aptly named break keyword:
if (again == 'N' || again == 'n') {
break;
}
The problem with the loop is this line:
if(again == 'N' || 'n')
C++ doesn't know that you mean it to check again against both characters. Instead, it tries again == 'N', which fails, and then tries 'n', which - not being zero - evaluates as true.
Instead, try:
if (again == 'N' || again == 'n')
break;

Creating Table headers?

OK, in this program I am required to make a table based of user input. The problem is I cannot figure out how to get the table headers to properly align with the information that is displayed. The table headers would not line up from lets say if the user enters in Michael for player one and Michael Jordan for player 2. Any advice to allow the headers to properly align with the displayed input regardless of character length would be greatly appreciated, thanks.
Here is my code:
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
//struct of Basketball Player info
struct BasketballPlayerInfo
{
string name; //player name
int playerNum, //player number
pointsScored; //points scored
};
int main()
{
int index, //loop count
total = 0; //hold total points
const int numPlayers = 5; //nuymber of players
BasketballPlayerInfo players[numPlayers]; //Array of players
//ask user for Basketball Player Info
cout << "Enter the name, number, and points scored for each of the 5 players.\n";
for (index = 0; index < numPlayers; index++)
{
//collect player name
cout << " " << endl;
cout << "Enter the name of player # " << (index + 1);
cout << ": ";
//input validation
if(!(getline(cin, players[index].name)))
{
cout << "Player Name must be alphabetical characters only!\n";
cout << "Program terminating please start over." << endl;
system("pause");
exit(0);
}
//getline(cin, players[index].name);
//collect players number
cout << "Enter the number of player # " << (index + 1);
cout << ": ";
//input validation
if(!(cin >> players[index].playerNum))
{
cout << "Player Name must be numeric characters only!\n";
cout << "Program terminating please start over." << endl;
system("pause");
exit(0);
}
//collect points scored
cout << "Enter points scored for player # " << (index + 1);
cout << ": ";
//input validation
if(!(cin >> players[index].pointsScored))
{
cout << "Player Name must be numeric characters only!\n";
cout << "Program terminating please start over." << endl;
system("pause");
exit(0);
}
cin.ignore();
}
//display
cout << "\n";
cout << "Here is the information for each player: \n";
cout << fixed << showpoint << setprecision(2);
cout << "\n";
cout << " \tName\tNumber\tPoints\n";
cout << "------------------------------------------------" << endl;
for(index = 0; index < numPlayers; index++)
{
cout << "Player # " << (index + 1);
cout << ": \t" << players[index].name << "\t" << players[index].playerNum << "\t" << players[index].pointsScored << endl;
cout << "------------------------------------------------" << endl;
}
//display total points scored by all players
for(index = 0; index < numPlayers; index++)
{
//hold total
total += players[index].pointsScored;
}
cout << "Total Points scored are: " << total << endl;
system("pause");
return 0;
}
you could use setw io manipulator which comes under #include <iomanip>.
cout << setw(20) << "Column1"
<< setw(20) << "Column2"
<< setw(8) << "Column3";
or you could use Boost library
// using Boost.Format
cout << format("%-20s %-20s %-8s\n") % "Column1" % "Column2" % "Column3";
Take a look at the setw and setfill functions. You can use them to assign a minimum width to your columns, which will make for much cleaner output formatting than tabs.