Question:
can you please Fix this Code of C++ language? its very simple, but i just started to learn programming?
anonymous
2011-07-17 17:02:25 UTC
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
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\iostream.h:31, from C:\CircleArea.cpp In file included from C:/Dev-Cpp/include/c++/3.4.2/backward/iostream.h:31, from C:\CircleArea.cpp

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

C:\Dev-Cpp\include\c++\3.4.2\backward\backward_warning.h In function `int main()':

14 C:\Dev-Cpp\include\c++\3.4.2\backward\backward_warning.h `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\backward_warning.h `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\iostream.h `cout' is already declared in this scope

41 C:\Dev-Cpp\include\c++\3.4.2\backward\iostream.h `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\backward_warning.h `int main()' previously defined here
Three answers:
modulo_function
2011-07-17 17:10:30 UTC
This should help:



) put the includes before the function definition.



) add

using namespace std;

so that cin and cout are defined

after the includes but before the functions.



) since you're using C++ it's usually better to use

#include

not iostream.h



) change

main()

to

int main()



Some of the error messages seem to imply that you might have more than one file that's being compiled and that the other file (unshown) has int main() in it.
anonymous
2011-07-17 17:20:03 UTC
You should not use Dev C++: it is very outdated and will not even compile modern C++ code. Use Code Blocks or Visual C++ 2010 Express Edition instead.



Regarding the errors in your code, make the following changes:



1) #include (not the .h, this is outdated) should be at the very top of the cpp file.

2) The next line right after that should be using namespace std;

3) main() should return an int.

4) At the end of main() just before the closing brace there should be the statement return 0;



I think I caught everything. But again, it is important to use something more modern than Dev C++.
cardejon
2016-10-14 11:46:18 UTC
gaining awareness of a thank you to study and understand compiler errors messages is all portion of the technique of gaining awareness of C++. have confidence me you will see numerous those as you bypass alongside, and a few of them could be incredibly cryptic. you basically ought to study a thank you to make certain what they're telling you. First, it rather is telling you the errors is at line 21, top? So start up there. i assume it rather is line 21: averageage= totalage / i ; next, it rather is telling you that 'totalage' is undeclared. it rather is the very first time the compiler has seen some thing pronounced as 'totalage', and it rather is not a variable announcement, so it basically would not understand what to make of 'totalage' you're in all probability objecting which you DO have a totalage variable, top? yet who do you have faith is nice, you, or the compiler? continuously start up with the thought the compiler is nice and you're incorrect. It makes it much less complicated. commonly, one among those ingredient is the consequence of a typo, and our eyes (or is it our concepts? our our ego?) refuse to work out what's glaring to the compiler. you think of you have a variable named totalage. bypass locate the announcement of it. Is it spelled a similar? precisely a similar? Letter for letter? No...it rather is not. C++ is case gentle while it comprises variable names. HTH


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...