Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Maul

Functions

This topic is 5926 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Just wondering why this doesn''t work. Thanks. #include <iostream.h> int x; int true(); int main() { cout<<"Number greater then 100: "; cin>>x; true(); return 0; } int true() { if (x > 100) cout<<"Thanks."; else main(); }

Share this post


Link to post
Share on other sites
Advertisement

  1. In C++ you cannot call main.
  2. In C++ ''true'' is a keyword (and it''s a macro in C).
  3. There is no such header as ''iostream.h''
  4. ''true'' (after you rename it) must return an integer.


Share this post


Link to post
Share on other sites
quote:
Original post by Null and Void
There is no such header as 'iostream.h'

Of course there is such a header, it's just that relying on it's existence or behaviour is a pretty poor assumption.

[edited by - SabreMan on July 30, 2002 6:31:53 AM]

Share this post


Link to post
Share on other sites
1. After fixing it, I just did ^^
2. This was the problem. Thanks.
3. Errr
4. No it doesn''t, or it didn''t and worked.

It appears that your only correct statement was 2, but nevertheless it helped me. Thank you for your help^^

Share this post


Link to post
Share on other sites
quote:
Original post by Maul
It appears that your only correct statement was 2, but nevertheless it helped me.

So you think I''m spouting nonsense ? Change #3 to "There is no such header as ''iostream.h'' required as part of standard C++" (thanks SabreMan) and all of those are true.

Share this post


Link to post
Share on other sites
Not nonsense, but in this case your wrong ^_~
To prove it, complile this:

#include <iostream.h>
int x;
int boo();

int main()
{

cout<<"Number greater then 100: ";
cin>>x;

boo();

return 0;
}

int boo()
{
if (x > 100)
cout<<"Thanks.";
else
main();
}


The only thing I changed was renaming the function true() to boo().

Share this post


Link to post
Share on other sites
Just becasue the code compiles it doesn''t mean that it''s corect.
You should be geting a warning about not returning anything in boo, cause Null and Void is right, a function declared as int funcName() must return an integer. If you don''t wish to return a value you should use void instead of int in the declaration.

Share this post


Link to post
Share on other sites
quote:
Original post by Maul
Not nonsense, but in this case your wrong ^_~

Other than the minor correction I made, he is most certainly correct. The C++ Standard forbids making calls to main().

Any function with a non-void return type must return a value of that type, or with a defined and accessible conversion to that type. The only exception to that rule, is main(), which must be declared to return int, and if a return value is omitted, the compiler will assume a "return 0".

Share this post


Link to post
Share on other sites
#include <iostream>

void check(int x) //Learn to name functions and use arg
{
if(x>100)
cout << "Thanks";
else
cout << "Hey!";
}

int main()
{
int x;
cout<<"Input a number larger than 100";
cin>>x;
check(x);
return 0
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!