So I am creating this program to make a receipt. Keep in mind that the program is nowhere near done, this is just the basic shell of the program, with little, very little content. So the problem is that I made a function that makes the receipt and prints it out, however it uses variables from the main() function. So to use them I declare them globally right? But when I do, the value is printed out at 0, no matter what value I give it in the main function....why is that?
The first thing I noticed is that you have itemcost defined twice. Once as a global variable, and again as a local variable. Try removing the local definition, as it is likely that main is using the local variable while receipt is using the global variable.
Another thing you can do is instead of using a global variable, which is typically looked upon as very bad practice, pass the value as a function argument.
Also whenever you declare a global variable ALWAYS initialize it (You should always initialize them anyways)
It is your choice to use global variables if you want (I strongly disagree with them usually) but in this case you don't even need it and you are not even using your function effectively.
Remove the global double itemcost; and change your function to this.
First the the double cost inside the function is called a parameter. Parameters are where you can pass other variables into a function to use.
For example when you call the function like this in main
receipt(itemcost);
You are telling the function to print itemcost since you are putting itemcost in the cost parameter, and cost gets called in the cout statement cout << "Total: " << cost << endl;
I hope that makes a bit of sense :p if you need more info check out the link rssair posted.