Hi,

I've been reading "C++ In 21 Days", I read functions a while back and decided to write some, i wrote the code and it gave me a error, so i copied and pasted code from the book and still got a error, you must see from my point of view if I'm learning a language i don't want to be tought the wrong things, thats like learning French and saying "Hallo" is hello, though the real hello is "Bonjour".

Heres the code non the less..

#include <iostream>

using namespace std;

unsigned short FindArea();

int main()
{
    unsigned short width, lenght, area;
    
    cout << "How Wide Is Your Yard? : ";
    cin  >> width;
    cout << endl << "How Long Is Your Yard? : ";
    cin  >> lenght;
    
    area = FindArea(width, lenght);
    
    cout << endl << endl << "Your Yard Is " << area << " Square Feet!";
}

unsigned short FindArea()
{
         unsigned short l, w;
         
         return l * w;
}

"too many arguments to function `short unsigned int FindArea()'

Recommended Answers

All 11 Replies

If that's the entire example and there's no more code somewhere else and you got that out of a book, then yes, the book is wrong. Get another book. Check again to make sure there isn't more to the example than the 26 lines you posted. It's hard to imagine that example would make it into a textbook if that's the whole example.

You're declaring the function like this: unsigned short FindArea(); The function expects 0 parameters and returns an unsigned short.

But when you call it : area = FindArea(width, lenght); You give two parameters to the function (width and length), so that's why the compiler is complaining.
- To solve the problem you need to change 3 things in your code:
The prototype (line 5). Change it to unsigned short FindArea(unsigned short, unsigned short); - line 21 change it to: unsigned short FindArea(unsigned short w,unsigned short l) - Delete line 23

If this code really came out of your book, you should burn it immediately and then throw the ashes in the river.

Hi,
thats like learning French and saying "Hallo" is hello, though the real hello is "Bonjour".

You're right. 'Hallo' is Dutch for Hello ;)

[edit] Too slow again

Looks to me like you copied it incorrectly. Here's the code from the link:

USHORT FindArea(USHORT l, USHORT w)

not

unsigned short FindArea()

According to what i have learnt, i havent come across anything like "unsigned" i would suggest you use integer or double type...i have modified the code for you(but have not tested it) try it and tell me how it works..i have also commented on what needs to be clarified

#include <iostream>

using namespace std;

double FindArea(double& width, double& lenght); //declare the function as double

int main()
{
    double width, lenght, area; //declare the variables as double
    
    cout << "How Wide Is Your Yard? : ";
    cin  >> width;
    cout << endl << "How Long Is Your Yard? : ";
    cin  >> lenght;
    
    area = FindArea(width, lenght);
    
    cout << endl << endl << "Your Yard Is " << area << " Square Feet!";
}

double FindArea(double& width, double& lenght) //function gets arguments from main
{
       double area;
       // i ommitted the l and w because they do not have any values
       // i think you meant to use width and lenght
      area = lenght * width;
  
         return area;
}
#include <iostream>

using namespace std;

double FindArea();

int main()
{
    double width, lenght, area;
    
    cout << "How Wide Is Your Yard? : ";
    cin  >> width;
    cout << endl << "How Long Is Your Yard? : ";
    cin  >> lenght;
    
    area = FindArea(width, lenght);
    
    cout << endl << endl << "Your Yard Is " << area << " Square Feet!";
}

double FindArea(double& l, double& w)
{        
         return l * w;
}

NOW I GET to many arguments to function `double FindArea()'

You didn't copy-paste it, so you have an error.
The code is no good any way. Go to your other thread and just make the adjustments I mentioned there

loose the l and w and replace it with lenght and width...the function expects to get arguments from the main but l and w do not exist in the main program

#include <iostream>

using namespace std;

double FindArea(double& l, double& w);

int main()
{
    double width, lenght, area;
    
    cout << "How Wide Is Your Yard? : ";
    cin  >> width;
    cout << endl << "How Long Is Your Yard? : ";
    cin  >> lenght;
    
    area = FindArea(width, lenght);
    
    cout << endl << endl << "Your Yard Is " << area << " Square Feet!";
}

double FindArea(double& l, double& w)
{        
         return l * w;
}

NOW I GET to many arguments to function `double FindArea()'

Well you have changed the attributes over here. in

double FindArea(double& l, double& w)
{        
         return l * w;
}

But there needs to be another change in line 5 of your code.

double FindArea();

This has to be changed into this to tell the computer that such a function exists.

double FindArea(double& l, double& w);

So the actual code should be something like this.

include <iostream>

using namespace std;

double FindArea();

int main()
{
    double width, lenght, area;
    
    cout << "How Wide Is Your Yard? : ";
    cin  >> width;
    cout << endl << "How Long Is Your Yard? : ";
    cin  >> lenght;
    
    area = FindArea(width, lenght);
    
    cout << endl << endl << "Your Yard Is " << area << " Square Feet!";
}

double FindArea(double& l, double& w)
{        
         return l * w;
}

I have not tested the code out but now it should work fine.

hi, below is listing 5.1 taken from second version of c++21d which you can found here:
http://www.angelfire.com/art2/ebooks/teachyourselfcplusplusin21days.pdf

All seems to be ok there. Maybe you have incompletely copied the example. functions can be overloaded, so it s possible that there is a second function FindArea what will meet your function call.

Listing 5.1. A function declaration and the definition and use of that
function.
1: // Listing 5.1 - demonstrates the use of function prototypes
2:
3: typedef unsigned short USHORT;
4: #include <iostream.h>
5: USHORT FindArea(USHORT length, USHORT width); //function
prototype
6:
7: int main()
8: {
9: USHORT lengthOfYard;
10: USHORT widthOfYard;
11: USHORT areaOfYard;
12:
13: cout << "\nHow wide is your yard? ";
14: cin >> widthOfYard;
15: cout << "\nHow long is your yard? ";
16: cin >> lengthOfYard;
17:
18: areaOfYard= FindArea(lengthOfYard,widthOfYard);
19:
20: cout << "\nYour yard is ";
21: cout << areaOfYard;
22: cout << " square feet\n\n";
23: return 0;
24: }
25:
26: USHORT FindArea(USHORT l, USHORT w)
27: {
28: return l * w;
29: }
Output: How wide is your yard? 100
How long is your yard? 200
Your yard is 20000 square feet
Analysis: The prototype for the FindArea() function is on line 5. Compare the prototype with the definition of the function on line 26. Note that the name, the return type, and the parameter types are the same. If they were different, a compiler error would have been generated. In fact, the only required difference is that the function prototype ends with a semicolon and has no body. Also note that the parameter names in the prototype are length and width, but the parameter names in the definition are l and w. As discussed, the names in the prototype are not used; they are there as information to the programmer. When they are included, they should match the implementation when possible. This is a matter of good programming style and reduces confusion, but it is not required, as you see here.
The arguments are passed in to the function in the order in which they are declared and defined, but there is no matching of the names. Had you passed in widthOfYard, followed by
lengthOfYard, the FindArea() function would have used the value in widthOfYard for
length and lengthOfYard for width. The body of the function is always enclosed in braces,
even when it consists of only one statement, as in this case.

krs, cliff

Ok, sorry about that, is it because i didn't use typedef?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.