C++:从包含多个变量的函数中获取一个变量

标签 c++ function

我是 C++ 的新手,我正在尝试弄清楚如何从包含多个变量的函数中获取特定变量。

所以这里我有一个名为 userInput() 的函数,它提示用户输入 3 个值。然后,稍后会多次调用该函数,为每个特定函数提供用户输入的值。问题是,我尝试这样做的方式会导致整个 userInput() 函数重复,从而使输出多次询问值(见下文)。

我知道我的代码可以精简很多,但是嘿,我在这里学习。因此,非常感谢有关任何部分的任何建议。 Code Review post here , 供引用。

代码

#include <iostream>
using namespace std;

float countySalesTax(), stateSalesTax(), totalSales();
void userInput(), display();

int main()
{
    countySalesTax(), stateSalesTax(), totalSales(), display();
    return 0;
}

void userInput(float totalMonthlySales, float countyTaxRate, float stateTaxRate) // Not sure how to do multiple variables in a function
{
    cout << "Enter the Total Monthly Sales:\t";
    cin >> totalMonthlySales;
    cout << "Enter the County Tax Rate:\t";
    cin >> countyTaxRate;
    cout << "Enter the State Tax Rate:\t";
    cin >> stateTaxRate;
}

float countySalesTax()
{

    float countyTax;
    float totalMonthlySales, countyTaxRate, stateTaxRate;
    userInput(totalMonthlySales, countyTaxRate, stateTaxRate); // Trying to call variables from userInput function?     
    countyTax = totalMonthlySales * countyTaxRate;
    return countyTax;
}

float stateSalesTax()
{
    float stateTax;
    float totalMonthlySales, countyTaxRate, stateTaxRate;
    userInput(totalMonthlySales, countyTaxRate, stateTaxRate); // same
    stateTax = totalMonthlySales * stateTaxRate;
    return stateTax;
}

float totalSales()
{
    float totalSalesTax;
    float countyTax = countySalesTax();
    float stateTax = stateSalesTax();
    totalSalesTax = countyTax + stateTax;
    return totalSalesTax;
}

void display()
{
    float countyTax = countySalesTax();
    float stateTax = stateSalesTax();
    float totalSalesTax = totalSales();

    cout << "The amount of County Tax is\t" << countyTax << endl;
    cout << "The amount of State Tax is\t" << stateTax << endl;
    cout << "The amount of Total Sales Tax is\t" << totalSalesTax << endl;
}

输出

1. Enter the Total Monthly Sales:  20000
2. Enter the County Tax Rate:   0.02
3. Enter the Total Monthly Sales:   20000
4. Enter the State Tax Rate:    0.04
5. Enter the Total Monthly Sales:   20000
6. Enter the County Tax Rate:   0.02
7. Enter the Total Monthly Sales:   20000
8. Enter the State Tax Rate:    0.04
[...etc...]
17. The amount of County Tax is 400
18. The amount of State Tax is  800
19. The total sales is  1200

最佳答案

我认为您缺少的概念是您可以将值传递给函数。只需调用 userInput 一次,然后将输入的值传递给您的其他函数。像这样

float countySalesTax(float totalMonthlySales, float countyTaxRate);

int main()
{
    float totalMonthlySales, countyTaxRate, stateTaxRate;
    userInput(totalMonthlySales, countyTaxRate, stateTaxRate);
    float countryTax = countySalesTax(totalMonthlySales, countyTaxRate);
    float stateTax = stateSalesTax(totalMonthlySales, stateTaxRate);
    ...
    return 0;
}

void userInput(float& totalMonthlySales, float& countyTaxRate, float& stateTaxRate)
{
    cout << "Enter the Total Monthly Sales:\t";
    cin >> totalMonthlySales;
    cout << "Enter the County Tax Rate:\t";
    cin >> countyTaxRate;
    cout << "Enter the State Tax Rate:\t";
    cin >> stateTaxRate;
}


float countySalesTax(float totalMonthlySales, float countyTaxRate)
{
    float countyTax;
    countyTax = totalMonthlySales * countyTaxRate;
    return countyTax;
}


float stateSalesTax(float totalMonthlySales, float stateTaxRate)
{
    float stateTax;
    stateTax = totalMonthlySales * stateTaxRate;
    return stateTax;
}

另一个错误是,如果你想从 userInput 返回多个值,你必须使用 references,即你必须使用 float& 而不是float 用于 userInput 的参数。

关于C++:从包含多个变量的函数中获取一个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18790618/

相关文章:

c++ - 使用函数从数组中索引最小值的问题

c++ - 在priority_queue中使用greater<char>()

c++ - 如何在 Mac Os 上按名称获取进程?

c++ - 可以函数返回 0 作为引用

c++ - 通过文本迷宫打印到屏幕路径的算法

Solr中连接字段的函数

javascript - 尝试将此代码包装在命名函数中

c++ - C/C++ - (void) 如何不用作参数?

C++11 自动和 decltype

c++ - 我可以在父构造函数中多次重用函数的返回值吗?