Tuesday, July 28, 2009

Very basic C++ question to do with function?

My program, very, very basic, says that "Function should return a value" (I have a very old compiler - Borland TurboC++)





int main()


{ //prints T as a block letter


cout %26lt;%26lt;"*****" %26lt;%26lt; endl;


cout %26lt;%26lt;" * " %26lt;%26lt; endl;


cout %26lt;%26lt;" * " %26lt;%26lt; endl;


cout %26lt;%26lt;" * " %26lt;%26lt; endl;


cout %26lt;%26lt;" * " %26lt;%26lt; endl;


cout %26lt;%26lt;" * " %26lt;%26lt; endl;


}





What am I missing? Ok, my spacing is correct when I type the program, but it seems to change when I publish... !?!

Very basic C++ question to do with function?
you are writing a program in cpp





and the main type is int and it should return a value...





you should return either 1 or 0 at the end of program.


returning 0 compiles your program and 1 doesn't compile your program
Reply:*int* main().





... your main function is not returning a value.





"Whoever can tell me why gets my best answer vote!!! I'm going bald tearing my hair out over here... :)"





My guess is that you are not setting up your project correctly. I do not use Borland, but there must be an option to create a 'New Console Project'. You are probably creating a blank or empty project. By default this will *not* create a console window, but simply generate an executable. When you run the program it does print out statements, but it has no display device specified.





Check the borland tutorials for a console project.





Btw, do NOT use void as the return type for the main declaration. This is bad programming. All applications should return a status value to the operating system: 0 - good exit, anything else - bad.
Reply:#include %26lt;iostream%26gt;


using namespace std;





int main()


{ //prints T as a block letter


cout %26lt;%26lt;"*****" %26lt;%26lt; endl;


cout %26lt;%26lt;" *" %26lt;%26lt; endl;


cout %26lt;%26lt;" *" %26lt;%26lt; endl;


cout %26lt;%26lt;" *" %26lt;%26lt; endl;


cout %26lt;%26lt;" *" %26lt;%26lt; endl;


cout %26lt;%26lt;" *" %26lt;%26lt; endl;


system("PAUSE");


}
Reply:use void main() instead of int main() and message you are getting(i.e "Fucntion should return a value") is a Waring and not an error.
Reply:int main()


{ //prints T as a block letter


cout %26lt;%26lt;"*****" %26lt;%26lt; endl;


cout %26lt;%26lt;" * " %26lt;%26lt; endl;


cout %26lt;%26lt;" * " %26lt;%26lt; endl;


cout %26lt;%26lt;" * " %26lt;%26lt; endl;


cout %26lt;%26lt;" * " %26lt;%26lt; endl;


cout %26lt;%26lt;" * " %26lt;%26lt; endl;


return 0; %26lt;%26lt;- Add this line to remove warning: Function should return a value.


}





Hope this helps.
Reply:Add "return 0;" at the very end of the function. Changing main to return void instead of int works too.


No comments:

Post a Comment