gpt4 book ai didi

c++ - 将英镑转换为欧元 - 使用参数

转载 作者:行者123 更新时间:2023-11-28 06:28:07 26 4
gpt4 key购买 nike

我已经用 C++ 启动了一个项目来练习参数的使用,但我在我的代码中遇到了一个问题,我不太清楚为什么我会得到我得到的值。

这是我的代码:当我运行它时,我从 displayFinalData 输出的值是 0,而不是我期望的值(应该是任何转换等)。

#include <iostream>

using namespace std;

int main()
{
int numInEuros = 0;
char answer = 'Y';

double sumEuro = 0;
double priceEuro = 0;
int number = 0;

while ((answer == 'Y') || (answer == 'y'))
{
void processAPrice();
processAPrice();

void calculateSum(double priceInEuros, double& sumInEuros);
calculateSum(priceEuro, sumEuro);

cout << "\nDo you wish to continue? (Y/N): ";
cin >> answer;

number += 1;
}

void displayFinalData(double sumInEuros, int number);
displayFinalData(sumEuro, number);

system("pause");
return 0;
}

void processAPrice()
{
double pricePounds;
double priceEuro;

void getPriceInPounds(double& priceInPounds);
getPriceInPounds(pricePounds);

void convertPriceIntoEuros(double priceInPounds, double& priceInEuros);
convertPriceIntoEuros(pricePounds, priceEuro);

void showPriceInEuros(double priceInPounds, double priceInEuros);
showPriceInEuros(pricePounds, priceEuro);
}

void getPriceInPounds(double& priceInPounds)
{
cout << ("\nEnter price in pounds: \x9C");
cin >> priceInPounds;
}

void convertPriceIntoEuros(double priceInPounds, double& priceInEuros)
{
double conversionRate = 0.82;
priceInEuros = (priceInPounds / conversionRate);
}

void showPriceInEuros(double priceInPounds, double priceInEuros)
{
cout << ("\nThe Euro value of \x9C") << priceInPounds << " is EUR " << priceInEuros << "\n";
}

void calculateSum(double priceInEuros, double& sumInEuros)
{
sumInEuros += priceInEuros;
}

void displayFinalData(double sumInEuro, int number)
{
cout << "\nThe total sum is: EUR " << sumInEuro << "\n";
cout << "\nThe average is: EUR " << (sumInEuro / number) << "\n\n";
}

最佳答案

您在 processAPrice() 函数中单独声明了 priceEuros 变量,并在用户输入以英镑为单位的价格后更新该变量,但 main() 中的 priceEuro 变量未被触及,并不断将您的 sumEuro 递增 0。

如果您将 main 中的 priceEuros 变量传递给 processAPrice() 函数,它将起作用

void processAPrice(double& priceInEuros)
{
double pricePounds;

void getPriceInPounds(double& priceInPounds);
getPriceInPounds(pricePounds);

void convertPriceIntoEuros(double priceInPounds, double& priceInEuros);
convertPriceIntoEuros(pricePounds, priceInEuros);

void showPriceInEuros(double priceInPounds, double priceInEuros);
showPriceInEuros(pricePounds, priceInEuros);
}

关于c++ - 将英镑转换为欧元 - 使用参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28155292/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com