在C++中通过函数传递变量 所以我尝试用C++编写一个基本程序,得到一些东西的成本、数量,并计算三个不同的函数的总数/小计,然后在主显示。

在C++中通过函数传递变量 所以我尝试用C++编写一个基本程序,得到一些东西的成本、数量,并计算三个不同的函数的总数/小计,然后在主显示。,c++,function,variables,C++,Function,Variables,问题是,变量没有从函数中产生,我不知道为什么。我将输出语句放在函数中进行检查,问题似乎只有在我试图将它们从上述函数中拉出时才会出现 #include <iostream> using namespace std; int price(int cost) { cout << "What is the cost of the robot?" << endl; cin >> cost; if (cost < 1000) //validatio

问题是,变量没有从函数中产生,我不知道为什么。我将输出语句放在函数中进行检查,问题似乎只有在我试图将它们从上述函数中拉出时才会出现

#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;
价格成本

您正在调用一个返回int的函数,但没有将int存储在任何位置。您可能想回到您的课本,查看有关函数及其工作原理的章节。无意冒犯,但这是很基本的

你用NumRobot也在做同样的事情

或者,您可以通过引用传递参数并对其进行修改,但在我看来,这不太容易理解

tl;博士

你应该做int成本=价格;函数没有理由将int作为参数

pricecost

您正在调用一个返回int的函数,但没有将int存储在任何位置。您可能想回到您的课本,查看有关函数及其工作原理的章节。无意冒犯,但这是很基本的

你用NumRobot也在做同样的事情

或者,您可以通过引用传递参数并对其进行修改,但在我看来,这不太容易理解

tl;博士


你应该做int成本=价格;函数没有理由将int作为参数

使用返回值或通过引用或指针传递参数

一,。 int结果=numRobotsnumber

二,。
int numrobotint&number{..}

使用返回值或通过引用或指针传递参数

一,。 int结果=numRobotsnumber

二,。
int numrobotint&number{..}

您需要通过引用传递变量:

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

void price(int& cost)
{
    ....
}

void numRobots(int& number)
{
    ....
}
注意这种情况下的void返回类型

或者,您可以利用返回值:


但是这个方法没有多大意义,因为作为参数传递给方法的变量与存储返回值的变量相同

您需要通过引用传递变量:

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

void price(int& cost)
{
    ....
}

void numRobots(int& number)
{
    ....
}
注意这种情况下的void返回类型

或者,您可以利用返回值:



但是这个方法没有多大意义,因为作为参数传递给方法的变量与存储返回值的变量相同

您需要通过引用传递变量,或者使用函数的返回值。你所有的函数都返回值,但你从不使用它们来赋值,例如,你应该做cost=pricecost来将返回值赋值回cost;不给某物赋值。谢谢你的快速回复!如果引入一行预算机器人呢?现在有一个想法。要么通过引用传递变量,要么使用函数的返回值。你所有的函数都返回值,但你从不使用它们来赋值,例如,你应该做cost=pricecost来将返回值赋值回cost;不给某物赋值。谢谢你的快速回复!如果你推出一系列廉价机器人呢?现在有了一个想法。啊,就是这样。我在C++中有点生疏了,一年前修了一门课程,拿这个复习,现在我们就开始学习函数了。谢谢你的帮助!啊,就是这样。我在C++中有点生疏了,一年前修了一门课程,拿这个复习,现在我们就开始学习函数了。谢谢你的帮助!我喜欢你复制OP代码的方式,但留下了一个坏的“在这里输入代码”。@LightnessRacesinOrbit,这叫做关注手头的问题;现在更正了…我可以发誓我去掉了这里的输入码。哦,好吧,第一次海报,长期潜伏者。>>但你只是删掉了评论:哎,就像你复制了OP的代码,但留下了一个坏掉的“在这里输入代码”一样。@LightnessRacesinOrbit,这叫做关注手头的问题;现在更正了…我可以发誓我去掉了这里的输入码。哦,好吧,第一次海报,长期潜伏者。>>但是你刚刚删掉了评论:哦