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;
cost = price(cost)to assign the return value back tocost.