首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向过程传递参数和从过程传递参数

向过程传递参数和从过程传递参数
EN

Stack Overflow用户
提问于 2014-02-02 18:28:23
回答 1查看 120关注 0票数 0

我目前正在制作一个非常基本的程序(是的,我是个新手),而且我在传递参数到过程和从过程中传递参数方面遇到了困难。

代码语言:javascript
复制
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++!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-02 18:29:52

函数声明中缺少参数的类型。你需要

代码语言:javascript
复制
void getPriceInPounds(int priceInPounds);
                      ^^^

另一方面,该函数根本不需要参数,因为您不使用它。在我看来,你想输入价格,然后把它还给来电者。在这种情况下,您的函数可以如下所示:

代码语言:javascript
复制
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;
}

你可以这样称呼它:

代码语言:javascript
复制
int pounds = getPriceInPounds();
int euros = convertPriceIntoEuros(pounds);

诸若此类。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21514471

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档