Archived

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

Maul

Functions

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
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
Andrew, that's not going to compile. You've either left out using namespace std; or forgot to put std:: in front of cout and cin.

[edit] Crap, I can't spell today.

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

[edited by - Chem0sh on July 31, 2002 7:22:04 AM]

[edited by - Chem0sh on July 31, 2002 7:22:27 AM]

Share this post


Link to post
Share on other sites
Well, I tried to compile your latter program, and this is what I saw:

Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland gamedevtest.cpp:
Error E2120 gamedevtest.cpp 21: Cannot call ''main'' from within the program in function boo()
Warning W8070 gamedevtest.cpp 22: Function should return a value in function boo()
*** 1 errors in Compile ***

Maybe your compiler is more lenient than most. What compiler are you using?

Share this post


Link to post
Share on other sites
quote:
Original post by Maul
Maybe you can''t call main(), but how come I did?

Your compiler can''t tell you about every single error in your program. It''s your responsibility to understand the programming language you are using - wrestling code past the compiler is not the correct way to achieve correctness.

Share this post


Link to post
Share on other sites
You just need to remove the "else main()" part. The function will return to it's caller when it has completed, and execution will continue from after the function call. Maybe this code will help:


  
#include <iostream>

using namespace std;

void f()
{
cout << "in function f()" << endl;
}

int main()
{
cout << "before function f()" << endl;
f();
cout << "after function f()" << endl;
}


[edited by - SabreMan on August 1, 2002 6:12:16 AM]

Share this post


Link to post
Share on other sites
However main can call itself. Ok, I don't really know why you would, and I'd definitely not recommend doing it, but it is possible.


    
// RecursiveMain.cpp : Defines the entry point for the console application.

#include <iostream>
using namespace std;

int main(void)
{
static int Num = 5;
if(Num > 0)
{
cout << "Num is " << Num << endl;
--Num;
main();
}
return 0;
}


The preceeding is what's known as "bad programming". Don't do it!

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

[edited by - Chem0sh on August 1, 2002 6:43:20 AM]

Share this post


Link to post
Share on other sites
Maul, since you posted this in the For Beginners section, and you're asking such questions, you have to trust the responses you get. It may compile, but that doesn't mean it's right.

For example you can compare strings (as in character arrays) using ==, but that doesn't mean it actually works properly. To demonstrate...

if ("boo" != "true")
{
std::cout < "oh wow, boo isn't true!" << std::endl;
}

This will compile, but it's just comparing the addresses of the arrays, not the contents.


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions


[edited by - siaspete on August 1, 2002 7:08:17 AM]

Share this post


Link to post
Share on other sites
Here is how I would suggest doing your program

Use a while loop in the main function. Have true return a boolean of true or false depending on whether the number is greater than 100. Then back in the main function, set a boolean equal to true called with the arguement 100. If it is true,break out of the loop, and the program will exit. Otherwise, keep going through the loop.

Also global variables in general are very bad practice. You may not understand that yet, but as you get more into object oriented design u''ll understand.

Make sure that you understand this code and dont just copy it blindly.


  
#include <iostream.h>

bool true(int number);

int main() {
bool greater;
while (true) /*this will keep the loop going indefinitely until the keyword break is found */
{
int x; // make x a local variable because globals are evil
cout << "Number greater than 100.";
cin >> x;
greater = true(x); // pass x by value to true
if (greater == true)
break;
else
continue; /*The continue doesn''t actually do anything here but is put there for emphasis*/

}
return 0;
}
bool true(int number) {
if (number > 100)
return true;
}


Feel free to email me at NYYanks432@hotmail.com if you have any questions

Share this post


Link to post
Share on other sites
quote:
Original post by Chem0sh
However main can call itself. Ok, I don''t really know why you would, and I''d definitely not recommend doing it, but it is possible.

C++ Standard, Section 3.6.1, para 3:

"The function main shall not be used (3.2) within a program [...]"

Section 3.2, para 2:

"[...] An object or overloaded function is used if its name appears in a potentially-evaluated expression [...]"

IOW, any call made to any form of main() is illegal.

Share this post


Link to post
Share on other sites
quote:
Original post by GameDev135
Here is how I would suggest doing your program

It would help if your example did not contain two errors which have already been corrected during this thread.

Share this post


Link to post
Share on other sites
yes, that was dumb of me....oh well.

So change the true function name to greaterThanHundred.
And then have it go


    
bool greaterThanHundred(int number) {
if (number > 100)
return true;
else
return false;
}


I tried......sorry.

Feel free to email me at NYYanks432@hotmail.com if you have any questions

[edited by - gamedev135 on August 1, 2002 10:37:42 AM]

Share this post


Link to post
Share on other sites