将参数传递到过程和从过程传递

时间:2014-02-02 18:28:23

标签: c++ parameters

我目前正在制作一个非常基本的程序(是的,我是新手),我在向程序传递参数时遇到问题。

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 ++ btw!

1 个答案:

答案 0 :(得分:1)

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

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);

等等。