我目前正在制作一个非常基本的程序(是的,我是个新手),而且我在传递参数到过程和从过程中传递参数方面遇到了困难。
void processAPrice();
void getPriceInPounds(priceInPounds);
void convertPriceIntoEuros(int priceInPounds);
void showPriceInEuros();
void calculateSum();
void produceFinalData();
int main()
{
char answer('Y');
int numberOfPrices(0);
while (answer = 'Y')
{
processAPrice();
numberOfPrices++;
cout << "Continue? (Y/N)";
cin >> answer;
}
if (numberOfPrices > 0)
produceFinalData();
system("PAUSE"); //hold the screen until a key is pressed
return(0);
}
void processAPrice() //
{
getPriceInPounds(priceInPounds);
convertPriceIntoEuros(priceInPounds);
showPriceInEuros();
calculateSum();
}
void getPriceInPounds(int priceInPounds) //
{
int priceInPounds;
cout << "Enter a price (in Pounds): /234";
cin >> priceInPounds;
}
void convertPriceIntoEuros(int priceInPounds) //
{
const int conversionRate(0.82);
int priceInEuros = (priceInPounds / conversionRate);在processAPrice过程中,我正在调用getPriceInPounds过程,但是我一直收到一个错误,说明priceInPounds是一个未声明的标识符。我猜想这是因为我在processAPrice过程中的参数中得到了它,但是如果我去掉它,我肯定不能将priceInPounds变量传回给processAPrice?
有人能解释一下如何正确地做到这一点吗?基本上,我需要它,以便将变量priceInPounds传递回processAPrice,这样我就可以将相同的变量传递给convertPriceIntoEuros。
谢谢:)
我用的是VS13和c++!
发布于 2014-02-02 18:29:52
函数声明中缺少参数的类型。你需要
void getPriceInPounds(int priceInPounds);
^^^另一方面,该函数根本不需要参数,因为您不使用它。在我看来,你想输入价格,然后把它还给来电者。在这种情况下,您的函数可以如下所示:
int getPriceInPounds()
{
int priceInPounds;
cout << "Enter a price (in Pounds): /234";
cin >> priceInPounds;
return priceInPounds;
}
int convertPriceIntoEuros(int priceInPounds) //
{
const int conversionRate(0.82);
return priceInPounds / conversionRate;
}你可以这样称呼它:
int pounds = getPriceInPounds();
int euros = convertPriceIntoEuros(pounds);诸若此类。
https://stackoverflow.com/questions/21514471
复制相似问题