Thread: A problem with my program.

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    242

    A problem with my program.

    I tried to create a program that gets an integer and returns 1.
    Well, here it is:
    Code:
    int Option1(int num)
    {		
    	if(num%2 == 0)
    	{
    		num = num/2;
    		printf("\nNext value: %d", num);
    		Option1(num);
    	}
    	if(num%2 != 0)
    	{
    		num = num*3+1;
    		printf("\nNext value: %d", num);
    		Option1(num);
    	}
    	
    	if(num == 1)
    	{
    		printf("\nFinal value: 1...DONE\n");
    	}
    	
    	return 0;
    }
    
    int main(void)
    {
    	int chosen_opt;
    	int num;
    	
    	printf("Loops and conditions:\n");
    	printf("----------------------------\n");
    	
    	printf("Choose an option:\n");
    	printf("#1 -Gets an integer and returns 1.\n");
    	printf("#2\n");
    	printf("#3\n");
    		
    	chosen_opt = scanf("%d", &chosen_opt);
    	if(chosen_opt == 1) 
    	{
    		 printf("\nEnter a number:\n");
    		 num = getchar();
    		 Option1(num);
    	}
    	
    	if(chosen_opt == 2) printf("\nIN CONSTRUCTION!!\n");
    	if(chosen_opt == 3) printf("\nIN CONSTRUCTION!!\n");
    	if(chosen_opt == 4) { printf("\nExiting in 5 seconds...\n"); sleep(5); }
    
    
    	getchar();
    	return 0;
    }
    I dunno why, but it runs an eternal loop (:O), why?
    Last edited by eXeCuTeR; 11-22-2007 at 01:05 PM.

  2. #2
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Code:
    num = getchar();
    will always obtain the '\n' left behind by the scanf() function called previously.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by Aia View Post
    Code:
    num = getchar();
    will always obtain the '\n' left behind by the scanf() function called previously.
    Huh?

  4. #4
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Code:
    scanf("%d", &chosen_opt);
    When you enter an integer you are pressing ENTER and that's like '\n', scanf() reads the integer and leaves the new line in the buffer.
    Code:
    num = getchar();
    Comes along and read that '\n' character. That's the one you're processing in the Option1 function. Since is not a 0 it keeps looping by vitue of the the call to itself in the second if.

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    I didn't test the program, but here's what i think it's doing... (by the way, when you have this kind of problem (infinite loop), you should try using your debugger in mode "step by step", can be really useful, in fact, it will give you the answer right over so...).

    Your fonction is looping infinitely because you are telling it to do so... you'll never converge with a condion such as
    Code:
    int Option1(int num)
    {		
    	if(num%2 == 0)
    	{
    		num = num/2;
    		printf("\nNext value: %d", num);
    		Option1(num);
    	}
    	if(num%2 != 0)
    	{
    		num = num*3+1;
    		printf("\nNext value: %d", num);
    		Option1(num);
    	}
     
    	// ...
    }
    Take num == 1 and look...

    Code:
        1 % 2 == 1
    =     <enter second if>
        4 = 3 + 1
    =     <recursive call with num == 4>
        4 % 2 == 0
    =     <enter the first if>
        2 = 4 / 2
    =     <recursive call with num == 2>
        2 % 2 == 0
    =     <enter first if>
        1 = 2 / 2
    =     <recursive call with num == 1>
        1 % 2 == 1
    =     <enter second if>
        4 = 3 + 1
    =     <and on and on and on....>
    Also, this will not resolve your problem but consider an else-if instead of two if.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Oh OK thanks
    How do I fix it now?
    I'm trying to think on many ways but can't find the right answer.
    I tried also:
    Code:
    if(num == 1) printf("w00t");
    else
    {
    if...
    if...
    }
    Last edited by eXeCuTeR; 11-22-2007 at 01:28 PM.

  7. #7
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Code:
    chosen_opt = scanf("%d", &chosen_opt);
    Do you know what scanf returns?
    It returns the total number of items successfully read, EOF when the input stream reached its end, or a negative number if an error occurs.

    No matter what integer you enter chosen_opt will always be 1 if its successful.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Oh, OK
    But how do I fix the eternal loop?

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Please help!

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Don't bump your threads - see the rules.
    foxman all but gave you the answer already, test the exit condition before you decide how to recurse.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    The exit condition? When you press 4?

    This is my code atm:
    Code:
    #include <stdio.h>
    #include <unistd.h>
    
    int Option1();
    
    int Option1()
    {
    	int num;
    	
    	printf("\nEnter a number:\n");
    	num = getchar() - 48;
    	if(&num==NULL) if(num < 0) printf("\nOnly native numbers are acceptable!\n"); // why &num == NULL and not num = NULL? I just tried it and it was compiled successfully.
    	
    	if(num != 1 && num > 0)
    	{
    		if(num&#37;2 == 0)
    		{
    			num = num/2;
    			printf("\nNext value: %d", num);
    		}
    		if(num%2 != 0)
    		{
    			num = num*3+1;
    			printf("\nNext value: %d", num);
    		}
    		if(num == 1) printf("\nFinal value: 1...\nDONE!\n");
    	}
    	return 0;
    }
    
    int main(void)
    {
    	int chosen_opt;
    	
    	printf("Loops and conditions:\n");
    	printf("----------------------------\n");
    	
    	printf("Choose an option:\n");
    	printf("#1 -Gets an integer and returns 1.\n");
    	printf("#2\n");
    	printf("#3\n");
    		
    	chosen_opt = getchar() - 48;
    	if(chosen_opt == 1) Option1();
    	if(chosen_opt == 2) printf("\nIN CONSTRUCTION!!\n");
    	if(chosen_opt == 3) printf("\nIN CONSTRUCTION!!\n");
    
    
    	getchar();
    	return 0;
    }
    And it's not working well =[
    I've been trying for hours to fix it, but failed..
    Last edited by eXeCuTeR; 11-22-2007 at 02:29 PM.

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Please, for the love of your local deity print '\n' AFTER the string not before it.

    Don't do this:

    Code:
    printf("\nHello world!");
    Instead do this:

    Code:
    printf("Hello world!\n");
    Putting newlines BEFORE strings defies all logic and antagonizes the stdio buffer layer's natural buffering strategy.

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    I fixed it.
    But how do I fix my program now? =[

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You basically rewrote the code and now you have a completely different question.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Kinda, lol.
    I don't wanna open a lot of topics so I did this.
    Anyways,
    Could you help me with that?

    New code:
    IT WORKS NOW!
    2 questions, please answer these I have no idea:
    1. how to get more than 1 digit?
    2. how to make the program stop.
    for example,
    look at the if(num <= 0)...i typed exit(3); because I didn't know what to stop the program and don't let it execute the OddNumbers and EvenNumbers function.
    PLEASE HELP!!
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int OddNumbers(int num);
    int EvenNumbers(int num);
    void Result1();
    
    int OddNumbers(int num)
    {
    	num *= 3;
    	num++;
    	if(num == 1) Result1();
    	else printf("Next value: &#37;d\n", num);
    	if(num%2 == 0) EvenNumbers(num);
    	if(num%2 != 0) OddNumbers(num);
    	
    	return 0;
    }
    int EvenNumbers(int num)
    {
    	num /= 2;
    	if(num == 1) Result1();
    	else printf("Next value: %d\n", num);
    	if(num%2 != 0) OddNumbers(num);
    	if(num%2 == 0) EvenNumbers(num);
    		
    	return 0;
    }
    
    void Result1()
    {
    	printf("Final value: 1...\nDONE!\n");	
    	exit(3);
    }
    
    int main(void)
    {	
    	printf("Loops and conditions:\n");
    	printf("----------------------------\n");
    	
    	int num;
    
    	printf("Enter a number:\n");
    	num = getchar() - 48;
    	if(num == 1) Result1();
    	if(num <= 0) { printf("ERROR::Only native numbers (doesn't include 0)!\n"); exit(3); }
    	if(num != 1 && num%2 == 0) EvenNumbers(num);
    	if(num != 1 && num%2 != 0) OddNumbers(num);
    
    	getchar();
    	return 0;
    }
    Last edited by eXeCuTeR; 11-22-2007 at 04:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 11:10 AM
  2. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 02:34 AM
  3. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  4. Replies: 20
    Last Post: 06-12-2005, 11:53 PM