1

So I'm trying to write a basic program in C++ to get the cost of something, the quantity, and calculate the total/subtotal, in three different functions, then display it in main().

Problem is, the variables aren't making it out of the function and I don't know why. I've put output statements inside the functions themselves to check, and the problem only seems to be when I'm trying to pull them out of said functions.

#include <iostream> using namespace std; int price(int cost) { cout << "What is the cost of the robot?" << endl; cin >> cost; if (cost < 1000) //validation { cout << "Cost is too low. Setting to $1000." << endl; cost = 1000; return cost; } return cost; } int numRobots(int number) { cout << "How many robots are being ordered?" << endl; cin >> number; if (number < 50) //validation { cout << "We only sell in quantities of 50 or more. Setting quantity to 50." << endl; number = 50; return number; } return number; } void grandTotal(int cost, int number, double &subtotal, double &total) { subtotal = (cost * number); total = (subtotal * .07) + subtotal; } int main() { int cost = 0; int number = 0; double subtotal = 0; double total = 0; price(cost);`enter code here` numRobots(number); grandTotal(cost, number, subtotal, total); cout << cost; //testing cout << number; //outputs cout << total; //of cout << subtotal; //variables system("pause"); return 0; 
4
  • 3
    Either you need to pass the variable by reference, or use the return value of the function. All of your functions return values, but you never use them to assign anything, for example you should do cost = price(cost) to assign the return value back to cost. Commented Nov 20, 2014 at 14:23
  • I figured it was something like that; not assigning the value to something. Thanks for the speedy reply! Commented Nov 20, 2014 at 14:32
  • 1
    What if you introduce a line of budget robots? Commented Nov 20, 2014 at 14:34
  • 1
    Now there's an idea. Commented Nov 20, 2014 at 14:35

3 Answers 3

4

price(cost);

You are calling a function which returns an int, but you're not storing the int anywhere. You might want to go back to your text book and check the chapter on functions, and how they work. No offense but this is rather basic.

You're doing the same thing with numRobots.

Alternatively, you could pass the parameter by reference and modify it, but imo, that's less easy to understand.

tl;dr;

You should be doing int cost = price(); (there's no reason for the function to take an int as a parameter)

Sign up to request clarification or add additional context in comments.

1 Comment

Ugh, that was it. I'm a bit rusty in C++, took a course on it a year ago, taking this one for a refresher and we're just now getting to functions. Thanks for the help!
0

Use returned value or pass parameter by reference or pointer.

1. int result = numRobots(number);

2. int numRobots(int& number) {.....}

Comments

0

You need to pass the variables by reference:

int cost = 0; int number = 0; price(cost); numRobots(number); void price(int& cost) { .... } void numRobots(int& number) { .... } 

Note the void return type in this case!

Alternatively, you can utilize the return value:

int cost = price(cost); int number = numRobots(number); 

But this method doesn't make much sense because the variable passed as parameter to methods is the same as the one in which the return value is stored!

4 Comments

I like how you copied the OP's code but left in the broken "`enter code here`".
@LightnessRacesinOrbit That's called focusing on the problem at hand! ;) (Corrected now.)
...Coulda sworn I got rid of that "enter code here". Oh well, first time poster, long time lurker. >.>
But you just trimmed the comment :O

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.