Variable Declared Globally but has no value?

Feb 11, 2013 at 7:51pm
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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>

using namespace std;

double itemcost;

void receipt();


int main()
{
    double itemcost;
    double inputmoney;

    cout << "Item cost: ";
    cin >> itemcost;
    cout << "Select payment type" << endl << endl;
    cout << "1 - Cash" << endl;
    cout << "2 - Debit" << endl;
    cout << "3 - Credit" << endl << endl;

    cout << "Type: ";
    int choice;
    cin >> choice;

    switch (choice)
    {
    case  1 :
        receipt();
        break;
    case 2 :
        receipt();
        break;
    case 3 :
        receipt();
        break;
    default :
        cout << "Error";
        break;
    }


    return 0;
}



void receipt()
{
    cout << "Total: " << itemcost << endl;
}


Thanks.
Feb 11, 2013 at 8:04pm
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.
1
2
3
4
5
6
7
8
9
10
11
using namespace std;

double itemcost;

void receipt();


int main()
{
    double itemcost;
    double inputmoney;
Feb 11, 2013 at 8:06pm
Feb 11, 2013 at 8:18pm
closed account (3qX21hU5)
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.


1
2
3
4
void receipt(double cost)
{
    cout << "Total: " << cost << endl;
}


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.
Last edited on Feb 11, 2013 at 8:19pm
Feb 11, 2013 at 8:22pm
as it is likely that main is using the local variable while receipt is using the global variable.

That is actually the case, because receipt() cannot 'see' itemcost in main().

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?

That is because the compiler initializes all global variables to a default value (0 in this case) at compile time.
Last edited on Feb 11, 2013 at 8:22pm
Feb 11, 2013 at 8:25pm
Thank you very much! Just as a beginner, alot of things dont make sense, so thanks for all the help!
Topic archived. No new replies allowed.