C++ Program to resuse variables OR create variables during runtime? - c++

Is there anyway you can store data in a c++ console application, transfer it to a different variable, then reuse the variable?
Why I want this:
I want to have the user input order details for an order then for the program to be able to store them so the program can recreate another order.
Current Code:
int quant;
int cost;
int selling;
int profit;
NewOrder:
cout <<""<< endl;
cout << "Enter an Order Number: " << flush;
getline(cin, ord);
cout << "Enter a Product ID: " << flush;
getline(cin, Prod);
cout << "Enter a Quantity: " << flush;
cin>>quant;
Pricing:
cout << "Enter a cost per unit: " <<char(156) << flush;
cin >> cost;
cout << "Enter a selling price per unit: " <<char(156) << flush;
cin >> selling;
Sleep(2000);
cout << endl;
cout << "Your order Details are:" << endl;
cout << "Order Number: " << ord << endl;
cout << "Product: " <<Prod << endl;
cout << "Quantity:" << quant << endl;
cout << "Total Cost: " <<char(156) << (quant*cost) << endl;
cout << "Total Selling Price: " <<char(156)<< (quant*selling) << endl;
profit = ((quant*selling) - (quant*cost)); //Assigning a value to profit.
cout << "Total Profit: " <<char(156)<< profit << endl;
if (profit < 0) {
cout << "You have a negative profit. Please change your pricing." << endl;
Sleep(3000);
goto Pricing; }
Currently, it lets the user input the details to one order and then displays them. I want to have it so the program can enter more then one order and going by order number can recall them. Can I use the programs memory to do this or will need set it to a SQL DB?
If so, how do I setup the SQL connection?
If I can do it within the memory, how? I have been looking around and I cant create and declare variables during runtime.

You could keep track of all the orders using a vector:
struct Product {
//Add additional fields you need here
int quant;
int cost;
int selling;
int profit;
};
int main() {
std::vector<Product> products;
while (stillAddingProducts) {
//Get all the data from the user
Product p;
p.quant = 10; //example you should get this from the user
//Insert the product based on the information received
products.push_back(p);
}
//Perhaps iterate through all products and display information
for (const auto& e : products) {
std::cout << "Quantity: " << e.quant << std::endl;
}
}
With this you will have a 'fresh' Product object every iteration. On a sidenote, try to avoid using goto in your code.

Related

New to structures, I'm confused about how to return values out of a void function and put it into another function

my C++ class just started learning about developing structures. I'm stuck on a homework problem where I'm asked to write a program that uses a structure named movie_data and two movie_data variables to display information about a movie. I'm able to develop the movie_data structure correctly along with the two variable to outsoruce to a function named get_movie_info. However, because I set it as a void function, I'm unable to return anything produced by the get_movie_function to send to my movie_display function. I tried rewriting my functions to be of the movie_data structure data type, but that seemed to make things worse. The first function produces all the information correctly, but the second function doesn't output anything. Thank you for your time.
#include <iostream>
#include <iomanip>
using namespace std;
struct movie_data
{
string title;
string director;
int year_released;
int running_time;
};
//Function Prototype
void get_movie_info(movie_data movie1, movie_data movie2);
void movie_display(movie_data movie1, movie_data movie2);
int main()
{
movie_data movie1;
movie_data movie2;
get_movie_info(movie1, movie2);
movie_display(movie1, movie2);
return 0;
}
void get_movie_info(movie_data movie1, movie_data movie2)
{
//Get movie_data's title
cout << "Enter the title for the first movie: ";
//cin.ignore();
getline(cin, movie1.title);
cout << movie1.title << endl;
//Get movie_data's director
cout << "Enter the director's name for " << movie1.title << ": ";
//cin.ignore();
getline(cin, movie1.director);
cout << movie1.director << endl;
//Get movie_data's release year
cout << "Enter the release year for " << movie1.title << ": ";
cin >> movie1.year_released;
cout << movie1.year_released << endl;
//Get movie_data's running time
cout << "Enter the runtime of " << movie1.title << " in minutes: ";
cin >> movie1.running_time;
cout << movie1.running_time << " minutes" << endl;
//Get movie_data's title
cout << "Enter the title for the second movie: ";
cin.ignore();
getline(cin, movie2.title);
cout << movie2.title << endl;
//Get movie_data's director
cout << "Enter the director's name for " << movie2.title << ": ";
//cin.ignore();
getline(cin, movie2.director);
cout << movie2.director << endl;
//Get movie_data's release year
cout << "Enter the release year for " << movie2.title << ": ";
cin >> movie2.year_released;
cout << movie2.year_released << endl;
//Get movie_data's running time
cout << "Enter the runtime of " << movie2.title << " in minutes: ";
cin >> movie2.running_time;
cout << movie2.running_time << " minutes" << endl;
}
void movie_display(movie_data movie1, movie_data movie2)
{
//Display movie1 information
cout << "\nBelow is the data of the first movie:\n";
cout << "Movie Title: " << movie1.title << endl;
cout << "Director's Name: " << movie1.director << endl;
cout << "Release Year: " << movie1.year_released << endl;
cout << "Movie Runtime in minutes: " << movie1.running_time << endl;
//Display the movie information
cout << "\nBelow is the data of the second movie:\n";
cout << "Movie Title: " << movie2.title << endl;
cout << "Director's Name: " << movie2.director << endl;
cout << "Release Year: " << movie2.year_released << endl;
cout << "Movie Runtime in minutes: " << movie2.running_time << endl;
}
While #Kai's answer of using refrences would work and correctly would answer your original question, I suggest doing something else.
First, use a function to read in only one move_data and make it return that:
movie_data get_movie_info();
A possible implementation (using your code) could be like this:
movie_data get_movie_info(){
movie_data movie;
cout << "Enter the title for the first movie: ";
getline(cin, movie.title);
cout << "Enter the director's name for " << movie.title << ": ";
getline(cin, movie.director);
cout << "Enter the release year for " << movie.title << ": ";
cin >> movie.year_released;
cout << "Enter the runtime of " << movie.title << " in minutes: ";
cin >> movie.running_time;
return movie;
}
Now you can call it twice to read your info and it will return the movie data as the correct structure.
movie_data movie1 = get_movie_data();
If you need to have structs that can be edited, references are a good choice. For returning multiple values, there are better choices: An array of a suitable size (std::array), a Pair for two, or a vector of Objects.
It's better to avoid having output parameters (as a rule of thumb, break it if you need to and know why) as they are hard to grasp from the signature and hard to keep track of.
Notice, that you do everything twice. The point of using functions, is to not do everything twice, so you should write one function to do one thing and just call it with different parameters. For example in get_movie_info. A better design would be to create a function that creates exactly one movie_data and returns it:
movie_data get_movie_info()
{
movie_data result = {}; // That's the variable were we store the data
//Get movie_data's title ...
//Get movie_data's director ...
//Get movie_data's release year ...
//Get movie_data's running time ...
return result; // return the created movie data
}
The same goes for movie_display. Don't create a function that does exactly the same thing for two parameters, but create a function that does it one time and call it twice:
void movie_display(movie_data movie)
{
cout << "Movie Title: " << movie.title << endl;
//And so on ...
}
Then you combine both in the main like this:
int main()
{
movie_data movie1 = get_movie_info();
movie_data movie2 = get_movie_info();
std::cout << "data of the first movie:\n";
movie_display(movie1);
std::cout << "data of the second movie:\n";
movie_display(movie2);
return 0;
}
Update your function signatures to take references instead of values.
https://www.learncpp.com/cpp-tutorial/72-passing-arguments-by-value/
void get_movie_info(movie_data& movie1, movie_data& movie2)
void movie_display(const movie_data& movie1, const movie_data& movie2)

Math results in zero. New to coding

I'm trying to complete an assignment but I'm having difficulty with the math expressions and variables in general. I'm trying to make a program that takes user info on groceries and then outputs a receipt. Here is my code.
#include <iostream>
#include <string>
using namespace std;
int main()
{
//user input
string firstItem, secondItem;
float firstPrice, secondPrice;
int firstCount, secondCount;
double salesTax = 0.08675;
double firstExt = firstPrice * firstCount;
double secondExt = secondPrice * secondCount;
double subTotal = firstExt + secondExt;
double tax = subTotal * salesTax;
double total = tax + subTotal;
//user input
cout << "What is the first item you are buying?" << endl;
getline(cin, firstItem);
cout << "What is the price of the " << firstItem << "?" << endl;
cin >> firstPrice;
cout << "How many " << firstItem << "s?" <<endl;
cin >> firstCount;
cin.ignore();
cout << "What is the second item you are buying?" << endl;
getline(cin, secondItem);
cout << "what is the price of the " << secondItem << "?" << endl;
cin >> secondPrice;
cout << "How many " << secondItem << "s?" << endl;
cin >> secondCount;
// receipt output
cout << "1st extended price: " << firstExt << endl;
cout << "2nd extended price: " << secondExt << endl;
cout << "subtotal: " << subTotal << endl;
cout << "tax: " << tax << endl;
cout << "total: " << total << endl;
return 0;
}
The program output either 0 for all or negatives.
Your calculations must go after you read in the values, not before. You're making your calculations based on uninitialized variables.
A declaration and initialisation like
double firstExt = firstPrice * firstCount;
initialises firstExt to be the product of the current values AT THAT POINT of firstPrice and firstCount.
It doesn't set up some magic so that the value of firstExt is recalculated whenever the values of firstPrice or firstCount are changed.
In your case, firstPrice and firstCount are uninitialised variables when you do this. Accessing values of uninitialised variables of type int gives undefined behaviour.
What you need to do is something like
cout << "What is the price of the " << firstItem << "?" << endl;
cin >> firstPrice;
cout << "How many " << firstItem << "s?" <<endl;
cin >> firstCount;
firstExt = firstPrice*firstCount; // do the calculation here
If the value of firstExt is not needed until this point, you can declare it here instead;
double firstExt = firstPrice*firstCount; // do the calculation here
which means any earlier use of firstExt will give a compiler diagnostic.

Saving each iteration of an accumulator?

(Yes this WAS homework, but already completed, I'm just trying to improve it now for practice)
This is basically a sales calculator, that allows you to have multiple inputs for sales items, then displays the total, sales tax, and grand total.
The modification I'm trying to make, is that I want to be able to SAVE the cost of each number of items in a variable, without them overlapping memory, and then be able to call them above the grand total, so you can see what each item was worth.
===========================================================================
//importing libraries for cin and cout, as well as setw() and setprecision()
#include <iostream>
#include <iomanip>
using namespace std; //sets all code to standard syntax
int main(){ //initializes the main function
//initializing variables
char answer = ' ';
int saleItems = 0;
double saleTax = 0.0;
double grandTotal = 0.0;
double itemValue = 0.0;
double titemValue = 0.0;
double taxPerc = 0.0;
//begins a post-test loop
do {
titemValue = 0.0; //makes sure the accumulator resets WITHIN the loop
//prompts for sale items amount
cout << "How many sales items do you have? : ";
cin >> saleItems;
//creates a loop that displays the prompt for each iteration of saleItems
for (int x = 1; x <= saleItems; x += 1){
cout << "Enter in the value of sales item " << x << " : $";
cin >> itemValue;
titemValue += itemValue; //accumulator for adding up the iterated values
}
//prompts the user to enter a sales percentage
cout << endl << endl;
cout << "Enter in the sales tax percentage(Enter 10 for 10%): ";
cin >> taxPerc;
cout << endl << endl;
//processes the variables after taxPerc has been given
saleTax = titemValue * (taxPerc / 100);
grandTotal = titemValue + saleTax;
//sets decimal precision to 2 places
cout << fixed << setprecision(2);
//displays receipt with the calculated and input values
cout << "********************************************" << endl;
cout << "******** S A L E S R E C E I P T ********" << endl;
cout << "********************************************" << endl;
cout << "** **" << endl;
cout << "** **" << endl;
cout << "** **" << endl;
cout << "** **" << endl;
cout << "** Total Sales $" << setw(9) << titemValue << " **" << endl;
cout << "** Sales Tax $" << setw(9) << saleTax << " **" << endl;
cout << "** ---------- **" << endl;
cout << "** Grand Total $" << setw(9) << grandTotal << " **" << endl;
cout << "** **" << endl;
cout << "** **" << endl;
cout << "********************************************" << endl << endl << endl;
//prompts user to begin loop again
cout << "Do you want to run this program again? (Y/N):";
cin >> answer;
answer = toupper(answer);
cout << endl << endl;
} while (answer == 'Y');
===========================================================================
So, essentially, I need to be able to save each itemValue to multiple different values without the loop repeating itself, and just replacing them, and I can't really see how I can do that considering the accumulator will just keep looping, and adding up the itemValue values.
Here is one way to use an simple array to store the item values.
Declare an array at the top. Note: you will have to give it a fixed size. There are ways to have a variable size, but they get more complex (such as vectors). It is better to specify the size using a constant, rather than a hard coded number, as you will need the constant later.
const int maxSaleItems = 100;
double itemValues[maxSaleItems];
After you have asked the user for the number of items, max sure they haven't entered a number that is too big.
cout << "How many sales items do you have? : ";
cin >> saleItems;
if (saleItems > maxSaleItems) {
cout << "Sorry, I can only handle " << maxSaleItems << " items.";
continue;
}
Inside the loop where you are inputting the item values, save the item value to the array:
cout << "Enter in the value of sales item " << x << " : $";
cin >> itemValue;
titemValue += itemValue; //accumulator for adding up the iterated values
itemValues[x - 1] = itemValue;
Note the x-1 in the array access - arrays are 0 based (i.e. their index starts from 0). Normally I would loop x from 0 to < saleItems, but I didn't want to change your existing loop.
When printing the receipt, add a loop which prints out all the values (you will need to add formatting):
cout << "** **" << endl;
for (int x = 1; x <= saleItems; x += 1){
cout << "** Item " << x << " $" << itemValues[x-1] << " **" <<endl;
}
cout << "** **" << endl;
As I said in the comments, using std::vector would be better, but if you aren't up to that yet, arrays will do.
Edit: Simple vector example.
To add vectors you need to include the appropriate header:
#include <vector>
No need for maxSaleItems any more, as vectors can grow. Declare the vector variable. The <double> makes it a vector that contains double values:
std::vector<double> itemValues;
Inside the loop, instead of setting the array value for the new item by location, just add it to the end of the vector using push_back.
cout << "Enter in the value of sales item " << x << " : $";
cin >> itemValue;
titemValue += itemValue; //accumulator for adding up the iterated values
itemValues.push_back(itemValue);
The printing receipt code, can be left exactly as it was for the array version, as you can access vectors like arrays:
cout << "** **" << endl;
for (int x = 1; x <= saleItems; x += 1){
cout << "** Item " << x << " $" << itemValues[x-1] << " **" <<endl;
}
cout << "** **" << endl;
There are other changes you could do to make the vector version simpler, but I wanted to change as little as possible.

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

Results always returning 0 and accessing classees

I am having a couple problems with my code.
First off, with the code like it is, No matter what information I put in, It always returns 0, Any suggestions on where to fix this and how? I believe it has something to do with my Class Employee. How would I go about fixing this?
Second, How do I access the information in int total()? I need to access it for the last bit of code.
Also if you notice anything else that I can do to optimize my program, I welcome your suggestions. I am learning C++ as I go and will always be a Student.
// Datamax.cpp
// Created by Kennith Adkins
#include <iostream>
#include <string>
using namespace std;
class Employee
{
public:
string eName;
float eHours;
float eWage;
float ePay;
float eOvertimeHours;
float eOvertimePay;
float eTotalPay;
float eTotalBaseHours;
float eTotalSalary;
float eTotalOvertimeHours;
int Overtime ()
{
if (eHours > 40)
{
eOvertimeHours = (eHours - 40);
eOvertimePay = (eOvertimeHours * (eWage * 1.5));
ePay = ((eHours - eOvertimeHours) * eWage);
eTotalPay = ePay + eOvertimePay;
}
else
{
ePay = (eHours * eWage);
}
}
int total()
{
eTotalBaseHours = (employee1.eHours - employee1.eOvertimeHours) + (employee2.eHours - employee2.eOvertimeHours) + (employee3.eHours - employee3.eOvertimeHours);
eTotalSalary = (employee1.eTotalPay + employee2.eTotalPay + employee3.eTotalPay);
eTotalOvertimeHours = (employee1.eOvertimeHours + employee2.eOvertimeHours + employee3.eOvertimeHours);
}
} employee1, employee2, employee3;
// Start the main program here
int main()
{
// Gretting
cout << "Welcome to the Employee Pay Center\n";
// Employee1 information
cout << "Enter the employee name: ";
cin >> employee1.eName;
cout << "Enter the hours worked: ";
cin >> employee1.eHours;
cout << "Enter his or her hourly wage: ";
cin >> employee1.eWage;
cout << endl; // Adding a blank line to space the information out
// Employee2 information
cout << "Enter the employee name: ";
cin >> employee2.eName;
cout << "Enter the hours worked: ";
cin >> employee2.eHours;
cout << "Enter his or her hourly wage: ";
cin >> employee2.eWage;
cout << endl; // Adding a blank line to space the information out
// Employee3 information
cout << "Enter the employee name: ";
cin >> employee3.eName;
cout << "Enter the hours worked: ";
cin >> employee3.eHours;
cout << "Enter his or her hourly wage: ";
cin >> employee3.eWage;
cout << endl; // Adding a blank line to space the information out
// Returning the information to the Employeer
cout << "Employe Name ............ = " << employee1.eName << "\n";
cout << "Base Pay................. = " << employee1.ePay << "\n";
cout << "Hours in Overtime........ = " << employee1.eOvertimeHours << "\n";
cout << "Overtime Pay Amount...... = " << employee1.eOvertimePay << "\n";
cout << "Total Pay................ = " << employee1.eTotalPay << "\n\n";
cout << "Employe Name ............ = " << employee2.eName << "\n";
cout << "Base Pay................. = " << employee2.ePay << "\n";
cout << "Hours in Overtime........ = " << employee2.eOvertimeHours << "\n";
cout << "Overtime Pay Amount...... = " << employee2.eOvertimePay << "\n";
cout << "Total Pay................ = " << employee2.eTotalPay << "\n\n";
cout << "Employe Name ............ = " << employee3.eName << "\n";
cout << "Base Pay................. = " << employee3.ePay << "\n";
cout << "Hours in Overtime........ = " << employee3.eOvertimeHours << "\n";
cout << "Overtime Pay Amount...... = " << employee3.eOvertimePay << "\n";
cout << "Total Pay................ = " << employee3.eTotalPay << "\n\n";
cout << "*******************************************************\n";
cout << "*****************EMPLOYEE SUMMARY DATA*****************\n";
cout << "*******************************************************\n";
cout << "** Total Employee Salaries............ " << "**\n";
cout << "** Total Employee Hours............... " << "**\n";
cout << "** Total Overtime Hours............... " << "**\n";
cout << "*******************************************************\n";
cout << "*******************************************************\n";
return 0;
}
Hey Guys, Thanks for the help. I have most of it done now. It is displaying all the information. I am now just working on getting it to display the Employee Summary Data. I revamped my code to make it cleaner because I was trying every suggestion given to me as I learn best by hands on.
That's what you get for using non-initialized variables. You have set no value to your class members, you can't expect the compiler to guess what is your employee's name or total pay.
You need to use the form:
object name.member name = value
Of course, you should call the functions before outputting results that are supposed to be produced by these functions:
employee1.Overtime();
employee2.Overtime();
employee3.Overtime();