Thread: need help fixing excercise program...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    10

    Question need help fixing excercise program...

    this is based on a problem from an exercise book i found...

    in essence you're supposed to write a program that receives an integer (not necessarily positive), and perform the following check on it:

    every digit in an odd place should be odd and vise versa.
    so for example:
    4723 is fine
    3301 is not
    digits are counted right to left and there's no restriction to the size of the input.

    okay so here's what i got:

    Code:
    #include <stdio.h>
    
    int run=1, input, fixed, i=1, mod, check=1;
    int sign_fix(int input);
    
    main()
    {
    	while(run>0)
    	{
    		printf("input an integer:\n");
    		scanf("%d",&input);
    		
    		fixed=sign_fix(input);
    		
    		while(fixed>=0,check=1) 
    		{
    			if((fixed%2)==i)  // checks whether %2 is even or odd. i changes after every successful incident
    			{
    				i=(i+1)%2;
    				
    				fixed=fixed/10;
    			}
    			else
    			check=0;
    		}
    		if(check>0)
    		printf("YES \n");    //conforms to the requirements
    		else
    		printf("NO \n");     // duh
    	}
    }
    
    int sign_fix(int input)     // checks whether input is positive or negative and returns accordingly
    {
    	if(input>=0)
    	return input;
    	else
    	return -input;
    }
    the code compiles fine, but the program freezes somewhere after the scanf

    anyone know what the deal is ?
    and, in general, how does one go about solving such problems ?
    any tricks ?
    Last edited by SymDePro; 05-07-2009 at 03:03 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    		while(fixed>=0,check=1)
    This is certainly not doing what you think it does, since it's pointless to do fixced >= 0 when you are doing while(1) which is what the second part does.

    The comma operator is probably the most commonly misunderstood part of the C language.

    What you probably meant is to use && to check if fixed is greater or equal to zero, AND that check is equal to 1 - which also means that you need to use "is equal"(==) rather than "assign"(=).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    10
    fixed the && and the ==

    lost the fixed >=0 completely...

    i also left out a contingency...
    the difference between receiving 0 and fix becoming 0...

    after i fixed that it worked fine...

    thanks mat(s/e)... (how terribly droll )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM