How can I get a variable in one function to the next.

I need to get the variable "cost" in function get_item, into the function get_discount.

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
53
54
55
56
57
float get_item() //Getting information on the items.
{   
    char item [20];
    float price;
    float quantity;
    float cost;
    
       cout << "Enter the item name: ";
            cin >> item;

       do {
               cout << "Enter the items price: $";
               cin >> price;
                   if (price < 0)
                    {
                        cout << "Prices cannot be negative, please enter a valid price." << endl;
                    }
          }
                while (price <0);
            do {   
                cout << "Enter the quantity you want to purchase: ";
                cin >> quantity;
                   if (quantity <0)
                     {
                       cout << "Item quantity cannot be negative, please enter a valid amount." << endl;
                     }
                     cost=price*quantity; // This is what I want to send, cost.
                }
                while (quantity <0);
                 
                 
                
}     





float get_discount() //Getting information on the discount.
{

    float discount;
    float dcost;                         // I need cost to be here as dcost, is that possible?
    do {
        cout << "Enter percent discount here: %";
        cin >> discount;
         if (discount < 0)
                  {
                     cout << "Discount cannot be negative, please enter a valid discount." << endl;
                  }
                  discount = (discount * 0.01f);
                  
       }
       
                 while (discount <0);
         
}
Last edited on
Pass it as a parameter to get_discount().
Can you show me how to do that please? I have been trying for so long but I keep typing it up wrong or something and I get errors! All week I have been stuck here!
Passing a parameter -

1
2
3
4
5
6
7
8
9
10
void myFunction(int myNumber)
{
    do something with myNumber
}

int main()
{
    int coolNumber
    myFunction(coolNumber);
}
Last edited on
Oh man ... I think I messed this assignment up. Cause in my first function nothing is getting done to "mynumber" all I wanted was to get the cost variable down to the next one.

Thanks for help.
You may also need to return the value after doing whatever you want to do though, since you aren't directly modifying the address itself.

1
2
3
4
5
6
7
8
9
10
11
12
void myFunction(int myNumber)
{
    myNumber + 5
    return myNumber
}

int main()
{
    int coolNumber = 0
    myFunction(coolNumber)
    cout coolNumber
}


In that case, coolNumber would be 0 + 5 = 5.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void myFunction(int myNumber)
{
    myNumber + 5
    return myNumber
}

int main()
{
    int coolNumber = 0
    myFunction(coolNumber)
    cout coolNumber
}
 


counting all the missing semicolons as a single problem, I count 4 problems with that code. 3 of them compile errors.

1. No semicolons anywhere.

2. Function is declared as type void, yet it returns an int.

3. cout has no extraction operator ( << ).

4. Logic error. coolNumber will still be zero after the call to myFunction.
Plus the semantic error that the expression

myNumber + 5

does absolutely nothing...
Sorry, it was just a quick fail pseudo-code example about returning.
Topic archived. No new replies allowed.