C++ 从函数返回某些变量?

C++ 从函数返回某些变量?,c++,C++,在周五学习函数之后,我在过去的几个小时里一直在做这个程序,我不知道如何从我的函数中调用特定的变量。在下面的代码中,我需要调用我在cash()函数中计算的值,它们是;百人,五十人,二十人,十人,五人,傻瓜和疯子。谢谢我的问题是如何从函数中调用特定值,下面的代码不起作用 int main() { //DECLARATIONS int totalDollarAmount; int hundreds = 0; int fifties = 0; int twenties = 0; int tens = 0

在周五学习函数之后,我在过去的几个小时里一直在做这个程序,我不知道如何从我的函数中调用特定的变量。在下面的代码中,我需要调用我在cash()函数中计算的值,它们是;百人,五十人,二十人,十人,五人,傻瓜和疯子。谢谢我的问题是如何从函数中调用特定值,下面的代码不起作用

int main()
{
//DECLARATIONS
int totalDollarAmount;
int hundreds = 0;
int fifties = 0;
int twenties = 0;
int tens = 0;
int fives = 0;
int loonies = 0;
int toonies = 0;

 //prompt for input
cout << "Please input a dollar amount: " << totalDollarAmount << endl;

hundreds = cash(hundreds);
hundreds = cash(fifties);
hundreds = cash(twenties);
hundreds = cash(tens);
hundreds = cash(fives);
hundreds = cash(toonies);
hundreds = cash(loonies);

cout << " The total number of Fifties is: " << fifties << endl;
cout << "The total number of Twenties is: " << twenties << endl;
cout << "The total number of Tens is: " << tens << endl;
cout << "The total number of Fives is: " << fives << endl;
cout << "The total number of Toonies is: " << toonies << endl;
cout << "The total number of Loonies is: " << loonies << endl;
return 0;
}//end main

//code function
int cash(int hundreds, int fifties, int twenties, int tens, int fives, int loonies, int toonies, int totalDollarAmount)
{
totalDollarAmount * 100;

hundreds = totalDollarAmount/10000;
totalDollarAmount = totalDollarAmount % 10000;

fifties = totalDollarAmount/5000;
totalDollarAmount = totalDollarAmount % 5000;

twenties = totalDollarAmount/2000;
totalDollarAmount = totalDollarAmount % 2000;

tens = totalDollarAmount/1000;
totalDollarAmount = totalDollarAmount % 1000;

fives = totalDollarAmount/500;
totalDollarAmount= totalDollarAmount % 500;

toonies = totalDollarAmount/200;
totalDollarAmount = totalDollarAmount % 200;

loonies = totalDollarAmount/100;
totalDollarAmount = totalDollarAmount % 100;
}//end function
intmain()
{
//声明
int totalDollarAmount;
整数=0;
int 50=0;
整数二十=0;
整数十位数=0;
整数五=0;
整数=0;
int-toonies=0;
//提示输入

cout您的代码有一些问题

  • 您的
    cash
    函数接受8个参数,而您在每个函数调用中只传递一个参数
  • 您的
    cash
    函数返回
    int
    ,但在函数内部您不返回任何内容
  • 如果要从一个函数调用返回多个值,可以使用结构或类并返回其值,否则需要通过引用或指针传递参数
  • 使用错误的命令向变量输入值。请使用
    cin
    而不是
    cout
    输入值
  • 对代码的一个快速修复方法是将
    cash
    功能更改为

    作废现金(整数和百元、整数和五十元、整数和二十元、整数和十元、整数和五元、整数和五元、整数和五元、整数和五元、整数和五元、整数和五元、整数和五元、整数和总美元金额)

    然后在
    main
    功能中,执行以下更改:

  • 将输入提示更改为

    cout << "Please input a dollar amount: ";
    cin >> totalDollarAmount;
    

  • 另外,您的
    cash
    函数中可能仍然存在一些逻辑问题,例如第一行
    totalDollarAmount*100;
    ,您从未将其分配回变量。但是这些程序逻辑我将留给您来解决。

    请签出
    struct
    (或
    class
    )在你的书中。这段代码有几个不同的问题,没有一个与你要做的事情有任何本质的联系(这使得回答起来更困难,通常情况下,在将来也没那么有用)。因此,如果您完成正在编写的函数教程,然后在下次需要帮助时返回一个定义更严格的问题,您可能会得到更好的服务。请将您的
    //声明
    部分移到
    int main(){…}
    scope。变量被绑定到一个什么是错误的代码?我得到的唯一错误是调用values@taoufik我知道它们是绑定的,它们被从main调用到函数中,值存储在函数中的变量中,然后当我让它工作时,被发送回main,我做了这些修复,b但是它给了我所有值的0。那么我如何通过引用传递呢?@johnbowtome
    &
    符号表示需要通过引用传递值reference@johnbowtome您从不将任何值存储到
    totalDollarAmount
    变量。您需要使用
    cin
    ,而不是
    cout
    来存储值。修复了此问题,但是它仍然返回0,您的输入是什么?如果您输入
    7000
    ,它将显示50岁的
    1
    ,20岁的
    1
    。查看
    cash(hundreds, fifties, twenties, tens, fives, loonies, toonies, totalDollarAmount);