Problems relating to conditional statements and fuctions.

This is a discussion on Problems relating to conditional statements and fuctions. within the C Programming forums, part of the General Programming Boards category; Hi. I need help. I would like to have a function that returns to it's parent function if conditions are ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    5

    Question Problems relating to conditional statements and fuctions.

    Hi.
    I need help. I would like to have a function that returns to it's parent function if conditions are not met, however my code will not compile. I am using Xcode 3. Here is the non functioning code:
    Code:
    #include <stdio.h>
    
    void first(void);
    
    int main()
    {
    	first();
    	printf("first completed successfully");
    }
    void first(void){
    	int		welcome;	
    	scanf( "%d", &welcome );
    	fpurge( stdin );
    	if(welcome == ""){
    		return();
    	}
    	else{
    		first();
    	}
    				}
    Thanks to anyone who can help.
    Last edited by Agroking; 05-07-2010 at 06:37 AM. Reason: Forgot to say thank you!

  2. #2
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    Please, read a basic book on C or something. It would be much better than posting this here.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  3. #3
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Please, read a basic book on C or something. It would be much better than posting this here.
    Go easy, it's his/her first post and the code is at least a decent attempt.

    Generally speaking, when your code fails to compile you receive compiler warnings. You may not be able to understand them at this point in time, but if you post them along with your code it will greatly help the people that are trying to help you.

    Not sure why your 'welcome' variable is preceded by two tabs? I'm pretty sure this doesn't actually matter but it just seems weird.

    'welcome' is an integer, while comparing it to the empty string may work, it's counter-intuitive to do so. I'd imagine you want to compare it to 0.

    When you don't want to return a value, you can just use 'return;'. The parenthesis shouldn't be there.

    Calling 'first' recursively isn't the best here because the user may never enter zero and you'd run out of stack space. This is best implemented as a simple loop, but it should at least work for now.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    A couple of things more:

    1. In the code you say that your main returns an int, but you are not returning anything from it. So make sure you return an int from the main.

    2. The fpurge() function is non-standard, and is not recommended even on systems where it's provided.

  5. #5
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    >In the code you say that your main returns an int, but you are not returning anything from it. So make sure you return an int from the main.
    5.1.2.2.3 Program termination
    1 If the return type of the main function is a type compatible with int, a return from the
    initial call to the main function is equivalent to calling the exit function with the value
    returned by the main function as its argument;10) reaching the } that terminates the
    main function returns a value of 0.
    Here's a quote from the standard. Writing return 0; explicitly is not really necessary. However, unlike in C++, I'd recommend to explicitly state in C functions that there are no agruments by putting void between parentheses. Because if one declares a prototype with empty parentheses, any argument list could be written in function definition, causing no errors, much for compatibility with legacy code.
    Last edited by GL.Sam; 05-07-2010 at 08:55 AM.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21