This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

I've tried compiling the program below but I keep coming out with these two errors. Errors C2061 and C2082 for lines 32 and 34, respectively. C2061 is a syntax error : identifier 'numerator'. C2082 is redefinition of formal parameter 'dollar'. I followed the example in my book exactly so I don't know what I'm doing wrong or how to interpret the errors. I looked it up in Help but I'm still fuzzy on it. Here's the program:

/*This program will compute the value of a user's holding of one stock.

The user inputs the number of shares of stock and the price per share.

The price is inputted using three arguments: the whole dollar portion,

and the fraction portion which consists of inputting the numerator and

the denominator. The value of the stock is returned.*/

#include <iostream.h>

double stock_price(int dollar, int numerator, int denominator);

//to calculate the number of shares by its price

int main ( )

{

int dollar_portion, numerator_part, denominator_part, number_of_shares;

double result;

cout << "Please input the number of shares." << endl;

cin >> number_of_shares;

cout << "Please input the price of the stock as shown in the following example. ";

cout << "To input the example price of 79 5/8, type in 79, spacemark, 5, spacemark, then 8." << endl;

cin >> dollar_portion >> numerator_part >> denominator_part;

cout << "Press enter to calculate the value of your shares." << endl;

result = stock_price(dollar_portion, numerator_part, denominator_part);

cout << "The value of your stock is " << result;

return 0;

}

double stock_price(int dollar, numerator, denominator)

{

int number_of_shares, dollar, numerator, denominator;

double price;

price = dollar + (numerator/denominator);

return price * number_of_shares;

}

/*This program will compute the value of a user's holding of one stock.

The user inputs the number of shares of stock and the price per share.

The price is inputted using three arguments: the whole dollar portion,

and the fraction portion which consists of inputting the numerator and

the denominator. The value of the stock is returned.*/

#include <iostream.h>

double stock_price(int dollar, int numerator, int denominator);

//to calculate the number of shares by its price

int main ( )

{

int dollar_portion, numerator_part, denominator_part, number_of_shares;

double result;

cout << "Please input the number of shares." << endl;

cin >> number_of_shares;

cout << "Please input the price of the stock as shown in the following example. ";

cout << "To input the example price of 79 5/8, type in 79, spacemark, 5, spacemark, then 8." << endl;

cin >> dollar_portion >> numerator_part >> denominator_part;

cout << "Press enter to calculate the value of your shares." << endl;

result = stock_price(dollar_portion

cout << "The value of your stock is " << result;

return 0;

}

double stock_price(int dollar, numerator, denominator)

{

int number_of_shares, dollar, numerator, denominator;

double price;

price = dollar + (numerator/denominator);

return price * number_of_shares;

}

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

It is supposed to be a data type at the position of numerator and denominator.

Error C2082:

The variable dollar is already defined as a formal parameter.

// Chensu's fixes are incomplete and won't give you

// a working program. Here's a complete fix:

#include <iostream.h>

double stock_price(int number_of_shares, int dollar,

int numerator, int denominator);

//to calculate the number of shares by its price

int main()

{

int dollar_portion, numerator_part, denominator_part, number_of_shares;

double result;

cout << "Please input the number of shares." << endl;

cin >> number_of_shares;

cout << "Please input the price of the stock as shown in the following example. ";

cout << "To input the example price of 79 5/8, type in 79, spacemark, 5, spacemark, then 8." << endl;

cin >> dollar_portion >> numerator_part >> denominator_part;

cout << "Press enter to calculate the value of your shares." << endl;

result = stock_price(number_of_shar

numerator_part, denominator_part);

cout << "The value of your stock is " << result;

return 0;

}

double stock_price(int number_of_shares, int dollar, int numerator,

int denominator)

{

double price;

price = dollar + double(numerator)/denomina

return price * number_of_shares;

}

To make that code work, I had to fix lots of things--not just the errors chensu addressed. Either you did a terrible job of copying the code from your book, or the book is a real piece of trash! What's the name of that book? I'd like to have a look at it and write to the author.

> Errors C2061 and C2082 for lines 32 and 34, respectively.

> C2061 is a syntax error : identifier 'numerator'.

> C2082 is redefinition of formal parameter 'dollar'.

C2061 is caused by mixing formal and old-style parameter declaration schemes. As you declare the function stock_price, you provide a data type for the first parameter, but none of the others. Since this is C++, you need to provide data type for every single parameter.

C2082 is because you declared dollar as a parameter to the function, then reused the same identifier name for a local variable. You can't do that, since both the parameter and local are scoped to the function's lifecycle.

Further, your stock_price() function won't work. The one supplied by chensu still won't compile--number_of_shares isn't initailized, so the function will return a random value. You need to pass the number_of_shares you got from the user to your stock_price() function so it can do the math to find the total value of the holding.

Finally, the expression

price = dollar + (numerator/denominator);

isn't going to give you the answer you expect. numerator and demoniator are integers, so you'll never get a fractional result. For instance, if numerator is 3 and denomniator is 8, you'll get 0 instead of 0.375. You need to cast one of those values to a double so that you can get a floating-point result. I used C++-style casting syntax in my fixed code.

..B ekiM

More than anything, I'm dying to know what book provided such an incompetant example.

..B ekiM

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trialIn regards to the C2061 error, I now found that my mistake was that I did indeed confuse myself with variable declarations and data type parameters.

In regards to the C2082 error, I must admit, is still a bit fuzzy. If I declared dollar as a parameter to the function and then reused the same name for a local variable then why didn't I get an error for numerator or denominator? Or is it that when I get an error code it is talking about the entire line?

And my expression for

price = dollar + (numerator/denominator)

Thanks for the casting fix, I really appreciate it. How do I make the result come out as, for instance, 104.10 as opposed to 104.1? I would like to show the full figure to include the zero at the end.

Cynde

> for numerator or denominator?

Because your first error ruined the compiler's ability to asses the rest of the code on that line. Sometimes, the compiler can make a guess and recover and continue trying to find errors. But, for this case, it couldn't.

> I would like to show the full figure to include the zero at the end.

You need to set the width and precision on cout, using its width() and precision() member functions.

..B ekiM

C++

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

double stock_price(int dollar, int numerator, int denominator)

{

int number_of_shares;

double price;

price = dollar + (numerator/denominator);

return price * number_of_shares;

}