For class I need to make a program that has the user input items they wish to buy, the price, and that calculates the discount and tax.
My teacher said that I cannot use any global variables, we are suppose to use a function for each thing, like one for items, one for the discount, and one for tax.
But if I cannot use any global variables how do I save all the information, am I allowed to put variables inside the functions? And what if I put some in the int main() function? Are those global?
global variables are ones outside of functions, main is a function. Hence the syntax "return value" name (parameters).
int main(void)
void Name(int)
Here is an example of code with a global variable.
1 2 3 4 5
int a = 0;
int main(void)
{
a = 5;
}
What you want to do is simply handle the variables inside of the functions by passing them to the functions. I recommend structs or classes if you have to have multiple variables passed and returned from multiple functions. (using pointers or references)
if you can use stl you can store it in a string, otherwise you use a char array which will be decayed to a char* in the functions you use. Here is a tip, I don't want to solve the assignment for you, but this is what I recommend.
If you use the syntax you have above you will need to return the float you get. This is why a class might be useful, if you need to hold multiple things.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
struct Name
{
float price;
string ItemName;
}
int main(void)
{
//some set item and price function. Probably a constructor call (means you need to write a constructor).
//an example of this would be to have your constructor take input from the user, otherwise you can simply
//assign the values manually after retrieving input.
return 0;
}
So I can do something like this and it will not be considered a global variable?
In this case price is definitely not a global variable. Although void function wouldn't help you. What your teacher wants is to use a function for obtaining particular information and return it to main function, e.g.
1 2 3 4 5 6 7
float MyFunction()
{
float price;
cout << "Please enter the item price: ";
cin >> price;
return price;
}
Just for clarification: A variable declared outside any function is global whilst a variable declared inside a function (including main) is local.
yes and no, technically a variable declared in a function is local to that function. Global variables are declared outside functions. What you have above is correct. If this is for a c++ class, you may want to use classes, meaning you would have your functions as member functions that have "direct" access to the variables. If this is a C class assignment you won't be able to do that.
If you go with the easiest case, you can simply create all of your variables in main and pass them to functions and return them from them. Keep in mind you can only return one variable from a function unless you pass by pointer or reference.
Let's clear the matter up: I can assume from the original post (quite safely I think) that the exercise is intended for a beginner who is not allowed to use either classes/structs or pointers/references. Then the only possible solution is to use several consecutive functions each returning one value.
Ok thanks for all the replies, I think I can do it with one function after the other, that seems the only possible way for me so far! Thanks for clearing everything up.
Here's the actual instructions though, I think you are right about having to just use functions.
Write a C++ Program that emulates a point of sale terminal at a Grocery Store. Use as few Global Variables as possible. Consider writing functions that perform the following tasks:
Accepts the Name of an Item
Accepts the Price of an Item
Accepts the Quantity of the item
Accepts a % Discount
Accepts a Tax category
Computes the subtotal of all items purchased
Computes the total tax on all Taxable items
Computes the Total Sales
Suggested Functions
Get_Item – Ask for name and quantity and Price of the item to be purchased
Get_Discount – Asks if the item is to be discounted and if so how much.
Get_Tax – Asks if the item is taxable
Compute_Cost – figures cost of Item
Get_Item – Ask for name and quantity and Price of the item to be purchased
because if you are supposed to use such a function then it wouldn't be possible to do it without constructs mentioned above by genuwine21 ie. either store them in a class/struct or use pointers/references. Have you learned any of these yet?
Darn ok ya you guys are right, in the chapter we are in it talks about storage classes? Is that what you mean? And also references towards the end. So I will have to store the info input from the user in one of those classes?
It would be easiest. Also I get the feeling you have to have multiple instances of the same class, so you may want to consider some way to organize them. If you are meant to do some sort of searching (with the get functions) there are a couple ways to do it. But before I explain that, I have to know if you are meant to search through or are you simply accessing information on only one instance?
In case you don't understand what I mean by instance, here is an example:
This is a class definition
class example
{
//data
}
these are instances of the class:
example a;
example b;
Ok , yes I will have to do this a bunch of times, there will be a mechanism to stop the loop when the user has no more items to input. I need to do it at least 5 times and keep a running total for the price and everything.
So one question, you need to keep a total, but do you need to keep each item's individual information?
Example: I have an apple an orange and a pear. The cost totals to $7.00, do I have to ever look up the individual items and their prices at any time, or do I just have to know there are 3 items that total $7.00?
Well after each item is entered I will have to display a math problem something like,
Soap is $3.00 x 2(quantity) = $6.00 - 15%(discount) + $0.33(tax) , so that wil be displayed already for each item.
Then at the end I need to post is the running totals of everything like you said just the total amounts all added up.
Ok so I am reading on references and classes and I think I understand how it is working so far but I am confused as to how I am suppose to use it for this. Thanks for the help again, this is confusing. Reading a lot today I think I am really close to getting it.
If you don't need to search you can do this a few different ways, I recommend definitely having a class or struct for organization purposes when you do your printing. Do you know how to access members inside a class/struct?
If not here is how to:
if you have a pointer to a struct/class you use an -> to get to the data member, if it is simply an instance you use a .
struct Name
{
int numberOfLetters;
}
Name *Temp = new Name();//some compilers complain about the (), you may need to take them off.
Name Instance(); //same deal with the () in this part as well.
If the goal of the exercise is only to print results of some evaluation (although a couple of them one after another) and not to store them until the end of your program then one object of the appropriate class/struct should be sufficient. You can (after printing previous results) override its members with new values.
Whoa that's so great, so I can pull the values out of that structure in other functions?
What if we are not allowed to use structures would I still be able to use the input saved in Get_item variables in other functions like for calculating costs and things? Darn because I think that is in a later chapter he hasn't gone over yet and I might not be able to put it in this one. Oh man I am confused for this program.