I am fairly new to programming, but a fast learner. I have an assignment in my C++ class that is to create a menu based coffee shop program. Here is what I have so far:
#include<iostream>
#include<cmath> //doubt this is needed but i added it just in case
usingnamespace std;
int choice1();
/*void choice2(); i commented these because i have not written the code
void choice3(); for them yet. i need to get the first function done
void choice4(); before moving on.
void help();*/
int main()
{
int choice;
constdouble small = 1.75, medium = 1.90, large = 2.00;
double totalPriceCups = 0.00;
bool startMenu = true;
while (startMenu != false)
{
cout << "*************************************\n";
cout << " 1 - Select Coffee Size.\n";
cout << " 2 - Show Total Number of Cups Sold of Each Size.\n";
cout << " 3 - Show Total Amount of Coffee Sold.\n";
cout << " 4 - Show Total Amount of Money Made.\n";
cout << " 5 - Help With This Program.\n";
cout << " 6 - End This Program.\n";
cout << "Enter your choice and press return: \n";
cin >> choice;
system("cls");
while ((choice < 1) || (choice > 6))
{
cout << "Not a valid choice. Please choose again.\n";
cin >> choice;
cout << "*************************************\n";
cout << " 1 - Select Coffee Size.\n";
cout << " 2 - Show Total Number of Cups Sold of Each Size.\n";
cout << " 3 - Show Total Amount of Coffee Sold.\n";
cout << " 4 - Show Total Amount of Money Made.\n";
cout << " 5 - Help With This Program.\n";
cout << " 6 - End This Program.\n";
cout << "Enter your choice and press return: \n";
cin >> choice;
system("cls");
}
switch (choice)
{
case 1:
int choice1();
double priceSmaCups, priceMedCups, priceLarCups;
//tried to declare 'int smallCups, medCups, largeCups;' but still came back with errors.
//how do i return the values of the function and continue the program?
//i would like to declare 'double totalPriceCups' here as well, but kept receiving errors.
priceSmaCups = small * smallCups;
priceMedCups = medium * medCups;
priceLarCups = large * largeCups;
totalPriceCups = priceSmaCups + priceMedCups + priceLarCups;
cout << "Your total comes to: " << totalPriceCups << endl;
break;
case 2: cout << "Total Number of cups sold\n";
break;
case 3: cout << "Total Amount of coffee sold\n";
break;
case 4: cout << "Total Amount of money made\n";
break;
case 5: cout << "Help\n";
break;
case 6: cout << "End of Program.\n";
startMenu = false;
break;
}
}
return 0;
}
int choice1()
{
bool sizeMenu = true;
int size, smallCups, medCups, largeCups;
while (sizeMenu != false)
{
cout << "Please select which size coffee you would like to purchase.\n";
cout << " 1 - Small\n";
cout << " 2 - Medium\n";
cout << " 3 - Large\n";
cout << " 4 - Finished Selecting Coffee Size(s)\n";
cout << "Enter your choice and press return: \n";
cin >> size;
while ((size < 1) || (size > 4))
{
cout << "Not a valid choice. Please choose again.\n";
cin >> size;
cout << "Please select which size coffee you would like to purchase.\n";
cout << " 1 - Small\n";
cout << " 2 - Medium\n";
cout << " 3 - Large\n";
cout << " 4 - Finished Selecting Coffee Size(s)\n";
cout << "Enter your choice and press return: \n";
cin >> size;
}
switch (size)
{
case 1:
cout << "You have selected size: SMALL\n";
cout << "The price is $1.75 per cup. How many cups would you like to purchase?\n";
cin >> smallCups;
break;
case 2:
cout << "You have selected size: MEDIUM\n";
cout << "The price is $1.90 per cup. How many cups would you like to purchase?\n";
cin >> medCups;
break;
case 3:
cout << "You have selected size: LARGE\n";
cout << "The price is $1.90 per cup. How many cups would you like to purchase?\n";
cin >> largeCups;
break;
case 4:
cout << "End of size selection.\n";
sizeMenu = false;
break;
}
}
return smallCups, medCups, largeCups;
}
My questions are submitted within the code. Any suggestions would be greatly appreciated. I also looked in the tutorials before posting but could not really make much sense of it. I am more of a "do it myself a few times" type of learner.
Just got some time to take a closer look, that's what I see so far.
For starters, #include<cmath> indeed is not needed.
1 2 3
priceSmaCups = small * smallCups;
priceMedCups = medium * medCups;
priceLarCups = large * largeCups;
Problem with this is that your variables you want to use in your equation are declared in your function. As soon as the function is executed, these go out of scope, hence you end up with uninitialized integers in the main().
I would simply declare those constants you have globally, calculate the total in the function and return it.
Then in your main:
1 2 3
case 1:
cout << "Your total comes to: " << choice1();
break;
By the way, be careful how you work with integers and doubles - your cost total could be truncated.
Try this out and see how it goes. Please bear in mind that I'm very new to C++, so there might be better ways around it.
Correct me if I'm wrong, but I thought it was a good practice to use global variables if they are made constant, i.e. initialized right away and had fixed value throughout the life of a program? I can swear I've seen this principle in a couple of sources. Not going against you guys here, just want to learn since I have only a month experience in C++
but I thought it was a good practice to use global variables if they are made constant, i.e. initialized right away and had fixed value throughout the life of a program?
in c maybe. ive never had a need for them. its like goto. there are better ways of achieving the same affect
I agree, but the only value that changes is user input, the price is constant. I don't know, I tried to compile my version and it works fine. But I'm best dealing with my own code though, I don't always see what sort of result others want.
Anyway, no response from op, so let's maybe drop it 😊
To MarciusV - in this assignment, #1 I am not supposed to use global variables.
#2 I do not like using global variables due to, as Little Bobby Tables said, they are messy and can cause bugs easily.
#3 'smallCups, medCups, largeCups' are user defined variables so I can not make them constants in this particular program.
But thank you for your input, gave me something to look at in the future.
#4 I understand that "choice(1)' does not return anything, that is what I am trying to rectify.
To LB - what would 'someVar' represent? is this a user defined variable or a constant? where would I initiate it, the function or main?
To Little Bobby Tables - I am not familiar with data structures. Could you go into a little more detail on that? Not asking to give me the code (I quite enjoy writing it myself... more satisfaction in that) just a simple example or a link to an example.
Thank you everyone for your speedy responses. I will post more of the code as I progress to show what I have come up with.
Here is the problem word for word so you can see what I am going for:
"Jason opened a coffee shop at the beach and sells coffee in three sizes: small (9oz), medium (12oz), and large (15oz). The cost of one small cup is $1.75, one medium cup is $1.90, and one large cup is $2.00. Write a menu-driven program that will make the coffee shop operational. Your program should allow the user to do the following:
a. Buy coffee in any size and in any number of cups.
b. At any time show the total number of cups of each size sold.
c. At any time show the total amount of coffee sold.
d. At any time show the total money made.
Your program should consist of at least the following functions: a function to show the user how to use the program, a function to sell coffee, a function to show the number of cups of each size sold, a function to show the total amount of coffee sold, and a function to show the total money made. Your program should not use any global variables and special vales such as coffee sizes and cost of a coffee cup must be declared as named constants."
Side note, as I am looking at the program, I noticed that I will need to return the values from the first function, not only to do the calculations to find the total cost of the cups, but also in my second function to show the total amount of cups in each size. I am thinking I will need to adjust my code to change a few things in my first function to make this a shorter program to make it easier to understand. Any suggestions?
Possibly a few functions within each other...? Just seems like the code would be extra long and look messy. I am kind of a perfectionist and OCD about how my code looks... lol
To LB - what would 'someVar' represent? is this a user defined variable or a constant? where would I initiate it, the function or main?
Did you miss the keyword int in my post? That line is a full line of code. It declares the variable right then and there, and initializes it from the return value of calling your function.
Ah I see. I must have missed that. Trying to read everything fast. My eyes must have been moving faster than my brain. Anyways, I have adjusted my code a bit. Having a different issue now. I have created a different function for each size. Then created a function to do the calculations. I chose to delete function 'choice2()' due to the fact that it was doing the same thing as a different function. For some reason, it will run through, choose 1 on the first menu, then select 4 to exit out of the second menu. But instead of it exiting out of the second menu, it goes through each choice of the second menu, does the math and exits to the main menu. here is the code, hope it is not too confusing:
#include<iostream>
#include<iomanip>
usingnamespace std;
int choice1();
int size1();
int size2();
int size3();
double price();
/*void choice3();
void choice4();
void help();*/
int main()
{
int choice;
bool startMenu = true;
while (startMenu != false)
{
cout << "*************************************\n";
cout << " 1 - Select Coffee Size.\n";
cout << " 2 - Show Total Number of Cups Sold of Each Size.\n";
cout << " 3 - Show Total Amount of Coffee Sold.\n";
cout << " 4 - Show Total Amount of Money Made.\n";
cout << " 5 - Help With This Program.\n";
cout << " 6 - End This Program.\n";
cout << "Enter your choice and press return: \n";
cout << "*************************************\n";
cin >> choice;
system("cls");
while ((choice < 1) || (choice > 6))
{
cout << "Not a valid choice. Please choose again.\n";
cin >> choice;
cout << "*************************************\n";
cout << " 1 - Select Coffee Size.\n";
cout << " 2 - Show Total Number of Cups Sold of Each Size.\n";
cout << " 3 - Show Total Amount of Coffee Sold.\n";
cout << " 4 - Show Total Amount of Money Made.\n";
cout << " 5 - Help With This Program.\n";
cout << " 6 - End This Program.\n";
cout << "Enter your choice and press return: \n";
cin >> choice;
system("cls");
}
switch (choice)
{
case 1:
choice1();
cout << "Your total comes to: $" << price() << endl;
break;
case 2:
cout << "Total number of SMALL cups: " << size1() << endl;
cout << "Total number of MEDIUM cups: " << size2() << endl;
cout << "Total number of LARGE cups: " << size3() << endl;
break;
/*case 3: cout << "Total Amount of coffee sold\n";
break;
case 4: cout << "Total Amount of money made\n";
break;
case 5:
help();
cout << "Help\n";
break;
case 6: cout << "End of Program.\n";
startMenu = false;
break;*/
}
}
return 0;
}
int size1()
{
int smallCups;
cout << "You have selected size: SMALL\n";
cout << "The price is $1.75 per cup. How many cups would you like to purchase?\n";
cin >> smallCups;
return smallCups;
}
int size2()
{
int medCups;
cout << "You have selected size: MEDIUM\n";
cout << "The price is $1.90 per cup. How many cups would you like to purchase?\n";
cin >> medCups;
return medCups;
}
int size3()
{
int largeCups;
cout << "You have selected size: LARGE\n";
cout << "The price is $1.90 per cup. How many cups would you like to purchase?\n";
cin >> largeCups;
return largeCups;
}
double price()
{
constdouble small = 1.75, medium = 1.90, large = 2.00;
double priceSmaCups, priceMedCups, priceLarCups, totalPriceCups = 0.00;
setprecision(2);
priceSmaCups = small * size1();
priceMedCups = medium * size2();
priceLarCups = large * size3();
totalPriceCups = priceSmaCups + priceMedCups + priceLarCups;
return totalPriceCups;
}
int choice1()
{
bool sizeMenu = true;
int size;
while (sizeMenu != false)
{
cout << "Please select which size coffee you would like to purchase.\n";
cout << " 1 - Small\n";
cout << " 2 - Medium\n";
cout << " 3 - Large\n";
cout << " 4 - Finished Selecting Coffee Size(s)\n";
cout << "Enter your choice and press return: \n";
cin >> size;
system("cls");
while ((size <= 0) || (size >= 5))
{
cout << "Not a valid choice. Please choose again.\n";
cout << "Please select which size coffee you would like to purchase.\n";
cout << " 1 - Small\n";
cout << " 2 - Medium\n";
cout << " 3 - Large\n";
cout << " 4 - Finished Selecting Coffee Size(s)\n";
cout << "Enter your choice and press return: \n";
cin >> size;
system("cls");
}
switch (size)
{
case 1:
size1();
break;
case 2:
size2();
break;
case 3:
size3();
break;
case 4:
cout << "End of size selection.\n";
sizeMenu = false;
break;
}
}
return 0;
}
To LB - Ah yes... of course. I will make that change. I did not see that. Probably would have caught it when I started my final debugging process. Thanks for catching that.
Does anyone know why when going into the second menu (function 'choice1') and selecting 4, the program does not exit the function and continue the program? Instead it goes though the function again, overwriting the original responses (if selected) and then exits. I'm confused with this one. My instructor was having a hard time finding the issue as well. He is supposed to be looking at the program and let me know what he comes up with, but I have not heard from him just yet.