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

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

Related

How to update a while loop with multiple if statements?

I am working on the "checkout" process of my vending machine code. I want to write it so that the program will keep asking for the amount needed from the user until all the money is entered. However, this code segment does not completely work.
"Checkout" Segment of Code:
while (money < total) {
float amountOwed = total - money;
cout << "Please insert another $" << amountOwed << endl;
cout << "Enter amount: $" << flush;
float payment;
cin >> payment;
}
if (money > total) {
float change = money - total;
cout << "Thank you! You have $" << change << " change." << endl;
}
if (money == total) {
cout << "Thank you! Have a nice day!." << endl;
}
Full code below:
#include <iostream>
#include <iomanip>
using namespace std;
string menuItems[5] = { "Popcorn", "Coconut Clusters" , "Granola Bar" , "Trail Mix" , "Chocolate" };
float cost[5] = { 2, 3, 2.50, 1.50, 1 };
void vendingMachine() {
for (int i = 0; i < 5; i++)
cout << i + 1 << ". " << menuItems[i] << ": $" << cost[i] << endl;
}
int main() {
cout.precision(2);
cout << std::fixed;
cout << "Vending Machine" << endl;
cout << "----Items------" << endl;
vendingMachine();
cout << "Enter 0 to checkout" << endl;
float total;
total = 0;
int item;
do {
cout << "Enter your selection: " << flush;
cin >> item;
item = item - 1;
//here will be printed : $0 has been added to cart even if you pressed 0 and what to escape
//is it possible to fix this??
cout << menuItems[item] << ": $" << cost[item] << " has been added to cart." << endl;
total = total + cost[item];
} while (item != -1);
cout << " " << endl;
cout << "Proceding to checkout..." << endl;
cout << "========================" << endl;
cout << "Amount due: $" << total << endl;
cout << "Insert money here: $" << flush;
float money;
cin >> money;
while (money < total) {
float amountOwed = total - money;
cout << "Please insert another $" << amountOwed << endl;
cout << "Enter amount: $" << flush;
float payment;
cin >> payment;
}
if (money > total) {
float change = money - total;
cout << "Thank you! You have $" << change << " change." << endl;
}
if (money == total) {
cout << "Thank you! Have a nice day!." << endl;
}
return 0;
}
In this loop:
while (money < total) {
you are not modifying money or total so the loop will never exit.
You probably want to update money like this:
while (money < total) {
// ...
cin >> payment;
money += payment;
}

Can Xcode mix buffered and non-buffered input functions?

I created this simple program to practice working with classes. I'm certain there are some errors with the way I used the class, but I'm just beginning to learn about them, so I haven't learned all of the conventions and etiquette. My main question is, can Xcode have functions that are buffered and other functions that are non-buffered? I'd like my function void InputValuesAndDisplayTotals(); to be buffered and my function void ManuallyAddCoinsAndDisplayTotals(); to be non-buffered (basically, hit a key and the character is instantly processed without using the enter key). Is this possible? Thanks in advanced.
#include <iostream>
#include <iomanip>
using namespace std;
class Coin
{
private:
const float PENNYVALUE = 0.01;
const float NICKELVALUE = 0.05;
const float DIMEVALUE = 0.10;
const float QUARTERVALUE = 0.25;
int PennyInput=0;
int NickelInput=0;
int DimeInput=0;
int QuarterInput=0;
public:
void InputValuesAndDisplayTotals();
void ManuallyAddCoinsAndDisplayTotals();
void Total();
};
int main()
{
int Choice;
Coin Count;
cout << "1 to enter total coin counts, 2 to manually count coins: ";
cin >> Choice;
if (Choice==1)
{
Count.InputValuesAndDisplayTotals();
Count.Total();
}
else
{
Count.ManuallyAddCoinsAndDisplayTotals();
}
cout << endl;
}
void Coin::InputValuesAndDisplayTotals()
{
cout << fixed << setprecision(2) << endl;
cout << "Input penny count: ";
cin >> PennyInput;
cout << "Total penny value: $" << PENNYVALUE*PennyInput;
Total();
cout << endl;
cout << "Input nickel count: ";
cin >> NickelInput;
cout << "Total nickel value: $" << NICKELVALUE*NickelInput;
Total();
cout << endl;
cout << "Input dime count: ";
cin >> DimeInput;
cout << "Total dime value: $" << DIMEVALUE*DimeInput;
Total();
cout << endl;
cout << "Input quarter count: ";
cin >> QuarterInput;
cout << "Total quarter value: $" << QUARTERVALUE*QuarterInput;
Total();
}
void Coin::ManuallyAddCoinsAndDisplayTotals()
{
char Choice2;
cout << "\n'1' for penny,\n'2' for nickel,\n'3' for dime,\n'4' for quarter,\n'q' to quit\n";
do
{
cout << "\nInput: ";
cin >> Choice2;
if (Choice2=='1')
++PennyInput;
if (Choice2=='2')
++NickelInput;
if (Choice2=='3')
++DimeInput;
if (Choice2=='4')
++QuarterInput;
cout << endl;
cout << "Pennies: " << PennyInput << endl;
cout << "Nickels: " << NickelInput << endl;
cout << "Dimes: " << DimeInput << endl;
cout << "Quarters: " << QuarterInput << endl;
Total();
}
while (Choice2!='q' && Choice2!='Q');
}
void Coin::Total()
{
cout << "\nTotal amount: $";
cout << fixed << setprecision(2) << (PENNYVALUE*PennyInput)+(NICKELVALUE*NickelInput)+(DIMEVALUE*DimeInput)+(QUARTERVALUE*QuarterInput);
cout << "\n";
}

Stuck with getting the occupancy to change with the input and checking it against the capacity

Here is my problem I set this program up and the thing I need it to do I cannot figure it out. I need to make a one argument method Change_Status that changes the status of the room to the value of its argument. The method should verify that the argument value does not exceed the room capacity. If it does, the method should return -1. Here is the code that I wrote
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class HotelRoom
{
private:
string room_no;
int capacity;
int occupancy;
double rate;
public:
HotelRoom();
HotelRoom(string, int,double, int);
string get_number();
int get_capacity();
int get_status();
double get_rate();
void change_rate(double);
bool change_status(int);
};
HotelRoom::HotelRoom (string room, int cap,double rt, int occup)
{
room_no = room;
capacity = cap;
occupancy = occup;
rate = rt;
}
string HotelRoom::get_number()
{
return room_no;
}
int HotelRoom::get_capacity()
{
return capacity;
}
int HotelRoom::get_status()
{
return occupancy;
}
double HotelRoom::get_rate()
{
return rate;
}
void HotelRoom::change_rate( double amount)
{
rate += amount;
}
bool HotelRoom::change_status(int occupancy)
{
bool result;
if (capacity <= occupancy )
{ occupancy = capacity ;
result = true;
}
else
result = false;
return result;
}
int _tmain(int argc, _TCHAR* argv[])
{
cout << setprecision(2)
<< setiosflags(ios::fixed)
<< setiosflags(ios::showpoint);
int occupancy;
double amount;
string room = "123";
int capacity = 4;
double rate = 150.00;
cout << endl;
cout << "enter the number of guests: ";
cin >> occupancy;
HotelRoom guest ( room, capacity, rate, occupancy);
cout << endl;
cout << " Room number is " << guest.get_number() << endl << endl;
cout << " Room Capacity is " << guest.get_capacity () << endl << endl;
cout << " Room Rate is " << guest.get_rate() << endl << endl;
cout << " Room Occupancy is " << guest.get_status()
<< endl << endl;
cout << endl;
cout << "enter the number of guests: ";
cin >> occupancy;
cout << endl;
cout << " Room number is " << guest.get_number() << endl << endl;
cout << " Room Capacity is " << guest.get_capacity() << endl << endl;
cout << " Room Rate is " << guest.get_rate() << endl << endl;
cout << " Room Occupancy is " << guest.get_status()
<< endl << endl;
cout << endl;
cout << " Change room rate: ";
cin >> amount;
guest.change_rate(amount);
cout << endl;
cout << " Room number is " << guest.get_number() << endl << endl;
cout << " Room Capacity is " << guest.get_capacity() << endl << endl;
cout << " Room Rate is " << amount << endl << endl;
cout << " Room Occupancy is " << guest.get_status()
<< endl << endl;
cout << endl;
cout << "enter the number of guests: ";
cin >> occupancy;
cout << endl;
cout << " Room number is " << guest.get_number() << endl << endl;
cout << " Room Capacity is " << guest.get_capacity() << endl << endl;
cout << " Room Rate is " << amount << endl << endl;
cout << " Room Occupancy is " << guest.get_status()
<< endl << endl;
system("pause");
return 0;
}
First of all, I believe this is the relevant code?
bool HotelRoom::change_status(int occupancy)
{
bool result;
if (capacity <= occupancy )
{
occupancy = capacity ;
result = true;
}
else
result = false;
return result;
}
There are a few problems here. The first is that the boolean logic is broken. You are checking if the capacity is less than or equal to the intended occupancy... it should be the other way around.
The second problem is that you're always setting the occupancy to the capacity. If the hotel has a capacity of 100, and you call change_status(50), the occupancy should change to 50, not 100.
And the last problem is that when you set occupancy in the function, you're referring to the variable occupancy which is an argument to the function. Function scope takes precedence over class scope. So you should give the argument a different name, or use this->occupancy to refer to the member variable. So a revised version is:
bool HotelRoom::change_status(int occupancy)
{
if (occupancy <= capacity)
{
this->occupancy = occupancy;
return true;
}
else
return false;
}

Vector Errors in C++ Program [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am currently working on just a miscellaneous Theater Seating program! I am just starting to progress through C++, but I can't figure out these errors. I am trying to make the program store user entered row numbers and row seats (only if it is multiple seats) in two vectors; vector seat_row() and vector seat_number(). I know what the error is stating, but I don't know how to approach the situation otherwise. Sorry if I am being too unclear, but to be honest, I don't know what else to include. Please take a look at my code, get a feel for my program, and let me have any ideas. Please keep answers somewhat simple; again, I'm no master at C++
P.S.
Sorry for showing the whole code... I didn't really know what to include, what not to; and I wanted people to be able to get a feel for the program by even testing it themselves... Thanks again!
/*
* This is a program built by a team of students
* to help local movie theaters sell tickets
*
* File: main.cpp
* Author(s):
*
*
* Created on April 15, 2013, 11:10 AM
*/
#include <cstdlib>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;
//Function Prototypes
void Title_Card();
void seating_prices();
void seating_chart();
void pick_seating();
void purchase_history();
void quit();
void update_file();
void Show_Menu();
void picking_seats();
void display_seating();
void display_name();
void display_number_of_seats();
void display_correct1(string, int);
void display_seats();
void display_correct2(string, int, int, int);
void display_correct2_alt(string, int, vector<int>, vector<int>);
void display_correct3(string);
void multiple_seats(string, int, vector<int>, vector<int>);
void display_rows_seats(string, int);
void display_finished(string);
void display_purchase_seats(string, int, int);
void display_purchase_seats_alt(string, int, vector<int>, vector<int>);
int readSeating (const char*, vector<char>&); //Reads SeatingChart.txt
int readPrices(string, vector<double>&); //Reads SeatingPrices.txt
vector<double> prices(15); //For SeatPrices.txt
vector<char> seating(450); //For SeatingChart.txt
vector<int> seat_row(); //For storing multiple seat rows
vector<int> seat_number(); //For storing multiple seat numbers
//Actual Program
int main() {
Title_Card(); //Calls Title Page
readSeating("SeatingChart.txt", seating); //Reads SeatingChart.txt
readPrices("SeatPrices.txt", prices); //Reads SeatPrices.txt
Show_Menu(); //Shows Introductory Menu
system ("pause");
return 0;
}
//**************************************************************
// Definition of the ShowMenu function. Shows introductory menu*
// and controls where user ends up in the program. *
//**************************************************************
void Show_Menu() {
int choice;
string password;
cout << "Welcome to the our theater program! Made for a person" << endl;
cout << "who is looking for seating, purchasing a ticket, and" << endl;
cout << "searching for other miscellaneous things... We hope" << endl;
cout << "you enjoy the program!" << endl << endl;
cout << "Below is a list of options the user can choose from:" << endl << endl;
cout << "1.\tSeating Prices" << endl;
cout << "2.\tSeating Chart" << endl;
cout << "3.\tPick Seating" << endl;
cout << "4.\tPurchase History" << endl;
cout << "5.\tQuit" << endl << endl;
cout << "Enter a choice... (1-5): ";
cin >> choice;
while (choice < 1 || choice > 5){
cout << endl << "You have entered an invalid choice!" << endl;
cout << "Enter a choice... (1-5): ";
cin >> choice;
}
switch (choice){
case 1:
seating_prices();
case 2:
seating_chart();
case 3:
pick_seating();
case 4:
purchase_history();
case 5:
quit();
}
}
//**************************************************************
// Definition of the seating_prices function. Displays to the *
// user, SeatPrices.txt *
//**************************************************************
void seating_prices(){
system ("cls");
cout << "The Current Seating Prices Are:" << endl << endl;
for (int count = 0; count < 4; count++){
cout << " " << setprecision(4) << showpoint << prices[count] << " | Row " << (count + 1) << endl;
}
for (int count = 4; count < prices.size(); count++){
cout << " " << setprecision(3) << showpoint << prices[count] << " | Row " << (count + 1) << endl;
}
cout << endl;
system ("pause");
system ("cls");
Show_Menu();
}
//**************************************************************
// Definition of the seating_chart function. Function for *
// displaying the seating chart by itself *
//**************************************************************
void seating_chart(){
system ("cls");
int counter = 30;
int row_counter = 0;
cout << "The Current Seating Chart Is:" << endl << endl << endl << " 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0" << endl << endl;
cout << " ---------------------------------------------------------------" << endl;
cout << " Row " << (row_counter + 1) << " ";
//Displaying Seating Chart
for (int index = 0; index < 270; index++){
if (index == counter){
row_counter = (row_counter + 1);
counter = (counter + 30);
cout << "" << endl << " Row " << (row_counter + 1) << " ";
}
cout << seating[index] << " ";
}
for (int index = 270; index < seating.size(); index++){
if (index == counter){
row_counter = (row_counter + 1);
counter = (counter + 30);
cout << "" << endl << " Row " << (row_counter + 1) << " ";
}
cout << seating[index] << " ";
}
cout << endl << " ---------------------------------------------------------------" << endl;
cout << endl << endl;
system ("pause");
system ("cls");
Show_Menu();
}
//**************************************************************
// Definition of the pick_seating function. Displays the *
// current seating chart and allows the user to pick their seat*
//**************************************************************
void pick_seating(){ //Not Finished
system ("cls");
display_seating();
}
//**************************************************************
// Definition of the purchase_history function. Displays the *
// current the total sum of all movie ticket purchases *
//**************************************************************
void purchase_history(){ //Not finished
system ("cls");
system ("pause");
system ("cls");
Show_Menu();
}
//**************************************************************
// Definition of the quit function. Allows the user to quit the*
// program entirely *
//**************************************************************
void quit(){
update_file();
exit(0);
}
//**************************************************************
// Definition of the update_file function. Designed to update *
// the seating chart upon leaving the pick_seating function *
//**************************************************************
void update_file(){ //Not finished
//This function is supposed to
//Update the seating chart
//upon exit of the pick_seating function
}
//**************************************************************
// Definition of the read_Prices function. Reads SeatPrices.txt *
// and stores the pre-determined prices into a vector named *
// prices. *
//**************************************************************
int readPrices(string myFile, vector<double>& vect) {
//input file
ifstream SeatPrices;
SeatPrices.open(myFile.c_str());
//if file cannot be found
if (!SeatPrices)
cout << "Cannot find the file!" << endl;
for (int index = 0; index < vect.size(); index++){
SeatPrices >> vect[index]; //Reading the file "SeatPrices.txt"
}
SeatPrices.close(); //Closes the file
return 0;
}
//**************************************************************
// Definition of the readSeating function. Reads a text file *
// with a seating chart in it. *
//**************************************************************
int readSeating(const char* myFile, vector<char>& vect){
//input file
ifstream SeatingChart;
SeatingChart.open(myFile);
//if file cannot be found
if (!SeatingChart)
cout << "Cannot find the file!" << endl;
for (int index = 0; index < vect.size(); index++){
SeatingChart >> vect[index]; //Reading the file "SeatingChart.txt"
}
SeatingChart.close(); //Closes the file
return 0;
}
//**************************************************************
// Definition of the display_seating function. Function for *
// simplifying the pick_seating function *
//**************************************************************
void display_seating(){
int counter = 30;
int row_counter = 0;
//Displaying Seating Chart
cout << " 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0" << endl << endl;
cout << " ---------------------------------------------------------------" << endl;
cout << " Row " << (row_counter + 1) << " ";
//Displaying Seating Chart
for (int index = 0; index < 270; index++){
if (index == counter){
row_counter = (row_counter + 1);
counter = (counter + 30);
cout << "" << endl << " Row " << (row_counter + 1) << " ";
}
cout << seating[index] << " ";
}
for (int index = 270; index < seating.size(); index++){
if (index == counter){
row_counter = (row_counter + 1);
counter = (counter + 30);
cout << "" << endl << " Row " << (row_counter + 1) << " ";
}
cout << seating[index] << " ";
}
cout << endl << " ---------------------------------------------------------------" << endl << endl;
cout << "In the seating chart... All hashtags (#'s) are seats already taken" << endl;
cout << "and all stars (*'s) are available seats..." << endl << endl;
system ("pause");
display_name(); //Continues the program, helps loop if necessary
}
//**************************************************************
// Definition of the display_name function. Function for *
// simplifying the pick_seating function *
//**************************************************************
void display_name(){
string name;
//Picking your seat
cout << endl << endl << "To pick your own seat(s), follow the instructions below:" << endl << endl;
cout << "What is the name of the recipient of the seat(s)? ";
cin >> name;
display_correct3(name);
}
//**************************************************************
// Definition of the display_number_of_seats function. Function*
// for simplifying the pick_seating function *
//**************************************************************
void display_number_of_seats(string name){
int number_of_seats;
int available_seats = 450; //Amount of remaining seats out of 450
cout << "Alright " << name << "!" << endl;
cout << "How many seats are you purchasing today? ";
cin >> number_of_seats;
while (number_of_seats < 1 || number_of_seats > available_seats){
cout << endl << endl << "You have entered an invalid number of seats!" << endl;
cout << "This might be because your number is zero or less," << endl;
cout << "or that the number you entered is more than the amount" << endl;
cout << "of remaining seats! Try again!" << endl << endl;
cout << "How many seats are you purchasing today? ";
cin >> number_of_seats;
}
display_correct1(name, number_of_seats);
}
//**************************************************************
// Definition of the display_correct1 function. Function for *
// simplifying the pick_seating function *
//**************************************************************
void display_correct1(string name, int number_of_seats){
int correct;
cout << endl << "Alright " << name << ", you are purchasing " << number_of_seats << " seat(s)?" << endl;
cout << "Is this correct? Enter a '0' for 'no', or a '1' for yes: ";
cin >> correct;
while (correct < 0 || correct > 1){
cout << "You have entered an invalid number!" << endl;
cout << "Enter a '0' for 'no', or a '1' for yes: ";
cin >> correct;
}
if (correct == 0){
cout << endl << endl;
display_number_of_seats(name);
}
if (correct == 1){
cout << endl << endl;
if (number_of_seats > 1)
multiple_seats(name, number_of_seats, seat_row, seat_number);
display_rows_seats(name, number_of_seats);
}
}
//**************************************************************
// Definition of the multiple_seats function. Function only *
// used if user chooses to purchase multiple seats *
//**************************************************************
void multiple_seats(string name, int number_of_seats, vector<int> vect, vector<int> vect2){
for (int index = 1; index <= number_of_seats; index++){
for (int count = 0; count < number_of_seats; count++){
cout << "For Seat #" << index << "..." << endl;
cout << "Enter the row number you would like to be in: (1-15): ";
cin >> vect[count];
while (vect[count] < 1 || vect[count] > 15){
cout << endl << "You have entered an invalid row number!" << endl;
cout << "Enter the row number you would like to be in (1-15): ";
cin >> vect[count];
}
cout << "Enter the seat number you would like to have (1-30): ";
cin >> vect2[count];
while (vect2[count] < 1 || vect2[count] > 30){
cout << endl << "You have entered an invalid seat number!" << endl;
cout << "Enter the seat number you would like to have (1-30): ";
cin >> vect2[count];
}
cout << endl;
}
}
display_correct2_alt(name, number_of_seats, seat_row, seat_number);
}
//**************************************************************
// Definition of the display_rows_seats function. Function for *
// simplifying the pick_seating function *
//**************************************************************
void display_rows_seats(string name, int number_of_seats){
int seat_choice;
int row_choice;
cout << "Enter the row number you would like to be in: (1-15): ";
cin >> row_choice;
while (row_choice < 1 || row_choice > 15){
cout << endl << "You have entered an invalid row number!" << endl;
cout << "Enter the row number you would like to be in (1-15): ";
cin >> row_choice;
}
cout << "Enter the seat number you would like to have (1-30): ";
cin >> seat_choice;
while (seat_choice < 1 || seat_choice > 30){
cout << endl << "You have entered an invalid seat number!" << endl;
cout << "Enter the seat number you would like to have (1-30): ";
cin >> seat_choice;
}
display_correct2(name, number_of_seats, row_choice, seat_choice); //Helps looping if necessary
}
//**************************************************************
// Definition of the display_correct2_alt function. Alternate *
// function if user enters multiple seats *
//**************************************************************
void display_correct2_alt(string name, int number_of_seats, vector<int> vect, vector<int> vect2){
int correct;
int counter = 1;
int counter_2 = 1; //For minor details for looks
for (int index = 1; index <= number_of_seats; index++){
for (int count = 0; count < number_of_seats; count++){
if (counter_2 != number_of_seats && counter_2 == 1){ //For first seat
cout << endl << endl << "Alright " << name << ";" << endl;
cout << "For Seat #" << index << "; you chose " << "Row #" << vect[count];
cout << " and Seat #" << vect2[count] << "?" << endl;
cout << "Is this correct? Enter a '0' for 'no', or a '1' for yes: ";
cin >> correct;
}
if (counter_2 != number_of_seats && counter_2 > 1){ //For all seats after first, except last seat
cout << endl << endl;
cout << "Next, for Seat #" << index << "; you chose " << "Row #" << vect[count];
cout << " and Seat #" << vect2[count] << "?" << endl;
cout << "Is this correct? Enter a '0' for 'no', or a '1' for yes: ";
cin >> correct;
}
if (counter_2 == number_of_seats){ //For last seat
cout << endl << endl;
cout << "And for your last seat, Seat #" << index << "; you chose " << "Row #" << vect[count];
cout << " and Seat #" << vect2[count] << "?" << endl;
cout << "Is this correct? Enter a '0' for 'no', or a '1' for yes: ";
cin >> correct;
}
}
while (correct < 0 || correct > 1){
cout << "You have entered an invalid number!" << endl;
cout << "Enter a '0' for 'no', or a '1' for yes: ";
cin >> correct;
}
if (correct == 0){
cout << endl << endl;
if (number_of_seats > 1)
multiple_seats(name, number_of_seats, seat_row, seat_number);
display_rows_seats(name, number_of_seats);
}
if (correct == 1){
if (counter == number_of_seats)
display_purchase_seats_alt(name, number_of_seats, seat_row, seat_number);
}
counter = (counter + 1);
counter_2 = (counter_2 + 1);
}
}
//**************************************************************
// Definition of the display_correct2 function. Function for *
// simplifying the pick_seating function *
//**************************************************************
void display_correct2(string name, int number_of_seats, int row_choice, int seat_choice){
int correct;
cout << endl << endl << "Alright " << name << ", you chose " << "Row #" << row_choice;
cout << " and Seat #" << seat_choice << "?" << endl;
cout << "Is this correct? Enter a '0' for 'no', or a '1' for yes: ";
cin >> correct;
while (correct < 0 || correct > 1){
cout << "You have entered an invalid number!" << endl;
cout << "Enter a '0' for 'no', or a '1' for yes: ";
cin >> correct;
}
if (correct == 0){
cout << endl << endl;
if (number_of_seats > 1)
multiple_seats(name, number_of_seats, seat_row, seat_number);
display_rows_seats(name, number_of_seats);
}
if (correct == 1)
display_purchase_seats(name, row_choice, seat_choice);
}
//**************************************************************
// Definition of the display_purchase_seats function. Function *
// user to purchase his chosen seats *
//**************************************************************
void display_purchase_seats(string name, int row_choice, int seat_choice){
int total_cost = 0; //Not set up yet; supposed to calculate the row price
system ("cls");
cout << name << ", now it is time to pay for your chosen seats!" << endl;
cout << "Since you chose Row #" << row_choice << ", and Seat# " << seat_choice << "..." << endl;
cout << "Your total cost is: S" << total_cost << endl << endl;
system ("pause");
display_finished(name);
}
//**************************************************************
// Definition of the display_purchase_seats_alt function. *
// Alternate function used for purchasing multiple seats *
//**************************************************************
void display_purchase_seats_alt(string name, int number_of_seats, vector<int> vect, vector<int> vect2){
int total_cost = 0; //Not set up yet; supposed to calculate the row price
system ("cls");
cout << name << ", now it is time to pay for your chosen seats!" << endl;
cout << "Since you chose " << number_of_seats << " seats:" << endl << endl;
for (int index = 0; index <= number_of_seats; index++){
cout << "Seat #" << index << " being in Row #" << seat_row[index] << ";" << endl;
}
cout << endl << "Your total cost is: $" << total_cost << endl << endl;
system ("pause");
display_finished(name);
}
//**************************************************************
// Definition of the display_correct3 function. Function for *
// simplifying the pick_seating function *
//**************************************************************
void display_correct3(string name){
int correct;
cout << endl << "Alright, you chose the name " << name << "?" << endl;
cout << "Is this correct? Enter a '0' for 'no', or a '1' for yes: ";
cin >> correct;
while (correct < 0 || correct > 1){
cout << "You have entered an invalid number!" << endl;
cout << "Enter a '0' for 'no', or a '1' for yes: ";
cin >> correct;
}
if (correct == 0){
system ("cls");
display_seating();
}
if (correct == 1){
cout << endl;
display_number_of_seats(name); //Helps if looping is necessary
}
}
//**************************************************************
// Definition of the display_finished function. Function for *
// simplifying the pick_seating function *
//**************************************************************
void display_finished(string name){
system ("cls");
cout << "Congratulations " << name << "! You have picked your seat!" << endl;
cout << "The Seating Chart will update momentarily..." << endl << endl;
update_file();
system ("pause");
system ("cls");
Show_Menu();
}
//**************************************************************
// Definition of the Title_Card function. Starts the program *
// with a title card, showing a little introductory title *
//**************************************************************
void Title_Card(){
cout << endl << endl << endl << endl;
cout << "\t\t" << "************************************************\n";
cout << "\t\t" << "* THEATER SEATING! *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "* A program created by a team of three *\n";
cout << "\t\t" << "* students to help small theaters sell *\n";
cout << "\t\t" << "* more tickets *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "* Team of Students: *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "************************************************\n";
cout << endl << endl;
system ("pause");
system ("cls");
}
Your error is here
vector<int> seat_row(); //For storing multiple seat rows
vector<int> seat_number(); //For storing multiple seat numbers
it should be
vector<int> seat_row; //For storing multiple seat rows
vector<int> seat_number; //For storing multiple seat numbers
It's a very common error, you were trying to declare two vectors called seat_row and seat_number, but because you used empty parens instead you declared two functions which return vectors.
Here's a summary
vector<int> a(10); // vector, size 10
vector<int> b(0); // vector, size 0
vector<int> c; // also vector size 0
vector<int> d(); // FUNCTION!! taking no arguments and returning a vector

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.