0

我对 C++ 很陌生,我试图弄清楚如何从包含多个变量的函数中获取特定变量。

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

我知道我的代码可以减少很多,但是嘿,我在这里学习。因此,非常感谢任何关于任何部分的建议。代码审查帖子在这里,供参考。

代码

#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.输入每月总销售额:20000
2.输入县税率:0.02
3.输入每月总销售额:20000
4.输入国家税率:0.04
5.输入每月总销售额:20000
6.输入县税率:0.02
7.输入每月总销售额:20000
8.输入国家税率:0.04
[...ETC...]
17.县税金额为400
18.国税金额为800
19.总销量1200
4

4 回答 4

2

我认为您缺少的概念是您可以将值传递给函数。只需调用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,即你必须使用float&notfloat作为参数userInput

于 2013-09-13T16:08:20.037 回答
1
  1. 您可以定义全局变量并在函数中更改这些变量。全局变量在 main 之前定义,可以在任何地方访问。
  2. 您可以让该函数返回一个浮点数组。
  3. 您可以通过引用传递函数中的浮点数,并对它们进行更改,这将反映在函数之外。将您的 userInput 函数更改为:

    userInput(float &totalMonthlySales, float &countyTaxRate, float &stateTaxRate)

于 2013-09-13T16:10:21.013 回答
1

创建一个包含这 3 个值的结构。userInput 返回这个结构体的值,其他的都通过这个结构体

struct Input
{
    float  totalMonthlySales,
           countyTaxRate,
           stateTaxRate;
};

Input userInput()
{
    Input returnValue;

    cout << "Enter the Total Monthly Sales:\t";
    cin >> returnValue.totalMonthlySales;
    cout << "Enter the County Tax Rate:\t";
    cin >> returnValue.countyTaxRate;
    cout << "Enter the State Tax Rate:\t";
    cin >> returnValue.stateTaxRate;

    return returnValue;
}

void main()
{
    Input sharedInput = userInput();
    float countyTax = countySalesTax(sharedInput);
    float stateSalesTax = stateSalesTax(sharedInput);
    float totalSalesTax = totalSalesTax(sharedInput);
    display(countyTax, stateSalesTax, totalSalesTax);
}

float countySalesTax(Input inputData)
{

    float countyTax;
    countyTax = inputData.totalMonthlySales * inputData.countyTaxRate;
    return countyTax;
}
//... and so on

其他人会建议您使用“按引用传递”,因为它比我所做的更有效,但它是相同的模式,有一些陷阱。了解此版本后,请查看 pass by reference 以了解更典型的实现

于 2013-09-13T16:10:52.057 回答
0

您可能不希望地多次调用这些函数。每次编写countySalesTax()都会有一个函数调用,特别是如果你这样做了countyTax = countySalesTax()。您可能想要做的是只调用这些函数中的每一个,您可以执行它,main然后将您找到的内容传递给其他函数。

请注意,您在 main 当前调用的函数和它们返回的浮点数被丢弃,因为它没有被分配。

于 2013-09-13T16:10:03.273 回答