Area of a Shape

So I wrote my code out and when i compile it, it says that calcArea is not declared. Can somebody help explain the problem?

/****************************************************
Method: Calculate the area of a shape
Parameters: Length, Width
Return Value: Area
Preconditions: Width and Length must be greater than 0
Postconditions: Area will be true
****************************************************/

#include <iostream>
using namespace std;

int main(){
calcArea();
return 0;
}

void calcArea(){
//Declare variables
double length,width,area;
int a,b;
//Get input value
cout << "Please enter length: ";
cin >> length;
//Validate input
while (a == 0){
if (length <= 0){
cout << "invalid input. Try again." << endl;
}else{
a = 1;
}
}
//Get input value
cout << "Please enter width: ";
cin >> width;
//validate input
while(b==0){
if(width <= 0){
cout << "Invalid input, Try agian." << endl;
}else{
b = 1;
}
}
//calculate area
area = length * width;
cout <<"The area of the shape is :" << area;
}
You declare calcArea after main, so the compiler comes across the main function first before calcArea.

To fix this you can declare calcArea before main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// #include's

void calcArea(); // Here is the fix

int main()
{
    calcArea();
    // ...
}

void calcArea()
{
    // definition
}
Last edited on
Line 5: calcArea() is undefined. Because calcArea() follows main, you need a function prototype for it.

Line 14: a and b are uninitialized (garbage) variables.

Line 19: Unlikely your loop is going to execute. You're comparing a garbage variable in your while condition.

Line 31: ditto.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.


Topic archived. No new replies allowed.