Reputation: 9
Why is it saying main must return a value? I have tried almost nothing because i dont know much about c++ and I dont understand the meaning behind the error msg
#include<iostream>
using namespace std;
int main()
{
// one acre equals 43560 square feet
double oneacre = 43560;
// one square meter equals 10.7639 square feet
double squaremeter = 10.7639;
double halfacre = 0.5;
double sfiha = oneacre * halfacre;
double smiha = sfiha * squaremeter;
cout << "in half acre there is" << sfiha << "square feet" << endl;
cout << "in half acre there is" << smiha << "square meters" << endl;
}
Upvotes: 0
Views: 50
Reputation: 3
Add "return 0" at last line of the main's body.Since main is also a function and it's return type is "int". You've written int main()..So main will only end successfully by returning any integer value at last within the main's body. Generally 0 is returned as it signifies that program executed correctly and without errors. But you can try returning any integer value. Even -10 in place of 0 will work.. With return it also returns the control from the program's console to the OS or the IDE you're using . You can return at any point,not necessarily at the last statement.Any statement after return will not be executed. Because control will no longer with program it'll be with OS again.You can try printing anything with printf in the next statements i.e., after return 0 and that won't be printed,you'll see. Give it a try!
You'll learn more about this return statement and return type i.e., int before main,when you'll study about functions. For now you think of it as it signifies the last statement to be executed,or ending of the program.
Upvotes: 0
Reputation: 2483
The main function returns an int
(as declared in the prototype int main()
.
This becomes the exit status of your program after it's run. You just need to add a line before the end of your main function to return an int.
Returning a 0 normally means no error so simply add return 0;
after the last cout
line.
Upvotes: 1