[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 325
  • Last Modified:

Errors C2061 and C2082

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;
      }
0
Cynde
Asked:
Cynde
  • 4
  • 3
1 Solution
 
chensuCommented:
It should be

double stock_price(int dollar, int numerator, int denominator)
{
int number_of_shares;
double price;

price = dollar + (numerator/denominator);
return price * number_of_shares;
}
0
 
chensuCommented:
Error C2061:

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.
0
 
mikeblasCommented:

// 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_shares, dollar_portion,
      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)/denominator;
   return price * number_of_shares;
}

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
mikeblasCommented:

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
0
 
chensuCommented:
Cynde,

Please accept mikeblas's complete answer.
0
 
mikeblasCommented:
Thanks.

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

..B ekiM
0
 
CyndeAuthor Commented:
Thanks Mike (and Chensu) - did I tell that I'm a REAL NEWBIE to C++?  I didn't copy the problem from the book, I followed it's example on just the function part of it and I misinterpreted it.  Our class just learned functions yesterday so it's my fault there for not giving you the right information.  So the book is okay.  
In 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
0
 
mikeblasCommented:
> 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?  

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
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now