Can you please Fix this Code of C++ language? its very simple, but i just started to learn programming?

I wrote this code to get the Area of circle, but i don't know whats wrong with it, that i got many errors. Can you please fix these errors, and write me in detail, where i did mistakes (please mention the mistakes) . I'm learning functions. And my compiler is BloodShed Dev-C++ . i will be very thankful, if you will help me out.

the code is

double circleArea (double radius)
{
return (3.1415926 * radius * radius);
}

#include <iostream.h>
main ()
{

double rad1;
double rad2;
double ringArea;

cout << "Please enter the outer radius value";
cin >> rad1;

cout << "Please enter the outer radius value";
cin >> rad2;

ringArea = circleArea (rad1) - circleArea (rad2);

cout<< "Area of the ring having inner radius " << rad2 << " and outer radius " << rad1 <<" is " << ringArea;

}


//////////////////////////////////////…

and the Errors are

6 C:\Dev-Cpp\include\c++\3.4.2\backward\io… from C:\CircleArea.cpp In file included from C:/Dev-Cpp/include/c++/3.4.2/backward/io… from C:\CircleArea.cpp

6 C:\CircleArea.cpp from C:\CircleArea.cpp

C:\Dev-Cpp\include\c++\3.4.2\backward\ba… In function `int main()':

14 C:\Dev-Cpp\include\c++\3.4.2\backward\ba… `cout' undeclared (first use this function)

(Each undeclared identifier is reported only once for each function it appears in.)

15 C:\Dev-Cpp\include\c++\3.4.2\backward\ba… `cin' undeclared (first use this function)

6 C:\CircleArea.cpp In file included from C:\CircleArea.cpp

6 C:\CircleArea.cpp At global scope:

40 C:\Dev-Cpp\include\c++\3.4.2\backward\io… `cout' is already declared in this scope

41 C:\Dev-Cpp\include\c++\3.4.2\backward\io… `cin' is already declared in this scope

C:\CircleArea.cpp In function `int main()':

8 C:\CircleArea.cpp redefinition of `int main()'

3 C:\Dev-Cpp\include\c++\3.4.2\backward\ba… `int main()' previously defined here
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

double circleArea (double radius)
{
	return (3.1415926 * radius * radius);
}

int main ()
{

	double rad1;
	double rad2;
	double ringArea;

	cout << "Please enter the outer radius value";
	cin >> rad1;

	cout << "Please enter the outer radius value";
	cin >> rad2;

	ringArea = circleArea (rad1) - circleArea (rad2);

	cout<< "Area of the ring having inner radius " << rad2 << " and outer radius " << rad1 <<" is " << ringArea; 
        return 0;
}

1)You must include what you need at first, always.
2)main() function must return an int.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream.h>
double area(double r)
{
return (3.1415926*r*r);
}
void main()
{
double r,a;
cout<<"Enter outer radius :";cin>>r;
a=area(r);
cout<<"Enter inner radius :";cin>>r;
a=a-area(r);
cout<<"The area is "<<a;
}

most efficient... least space occupied.
@mindfog:always main() need not have int.... different compilers work differently...
turbo c++ allows main(), void main() but dev c++ does not...
Last edited on
closed account (zb0S216C)
Your header module filename is wrong. It should be <iostream>. Also, you need to specify which parts of the standard namespace you're planning on using such as cout. You can do this by using the using directive. For example:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

using std::cout; // the 'std' is the identifier of the standard namespace.

int main( )
{
    cout << "..." << endl;

    // Optionally, you could do this:
    std::cout << "..." << std::endl;
}


int must be main's return type-specifier. Don't forget to initialize objects (variables) before you use them. There's a difference between initialization and assignment.

Initialization: The operation that assigns a value to a variable at the point of declaration.
Assignment: The operation that assigns a value to a variable after declaration.


Wazzak
Last edited on
no i totally disagree... try using borland c++ compiler... it works with oly iostream.h and also works with void main()
Try to follow the standard. If your compiler fails at that you may want to change it.
http://www.cplusplus.com/forum/articles/36896/ (and next)
+1 ne555

- main should always return an int.
- <iostream.h> should not be used (deprecated)
- <iostream> is standards compliant
- mindfrog's first reply has the best advice to the OP's problem.
Last edited on
@rambo1177:
Turbo C++ is extremely outdated.
I must use it because it is the one used in school, but even then it remains outdated.
using iostream.h won't work in modern compilers (MS VC++ for example)
Topic archived. No new replies allowed.