You are here:

C/C++ warning

Advertisement


Question
Hello sir!
kindly tell me the errors in my written code.

#include<iostream.h>
#include<conio.h>
voidmain()
{
clrscr();
int rad;
float area;
cout<<"Enter radius";
cin>>rad;
area=3.14*rad*rad;
cout<<area;
getch();
}
whenever i compile it...a warning occurs in the last line " } " & says function should return a vlaue. wat does it mean? wat should i do to make it correct?

Answer
Hello Vaibhav.

The error is that you have no space between void and main. You have written a function called voidmain which doesn't specify a return type. When a return type is not specified, C++ assumes that the return type is an integer. The error is saying that a value needs to be returned from the function called voidmain. Change the text
voidmain()
to
int main()
to declare the function main returning an integer.
The C++ standard says that it is invalid for main to have a void return type. The only valid return type for main is int. The standard says that the main function does not need to explicitly have a return statement, and if there is no return statement, a return of zero is assumed, but you have an older compiler, and so you will probably need to put in a return statement. I can tell you have an older compiler, because you are using <iostream.h> instead of <iostream>

The most correct code for your compiler is:

#include<iostream.h>
#include<conio.h>
int main()
{
   clrscr();
   int rad;
   float area;
   cout<<"Enter radius";
   cin>>rad;
   area=3.14*rad*rad;
   cout<<area;
   getch();

   return 0;
}

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.