Dimple problem with if/else and looping

This is a discussion on Dimple problem with if/else and looping within the C Programming forums, part of the General Programming Boards category; I have to create a program that takes one integer (the value of the integer will be 1, 2, or ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    34

    Dimple problem with if/else and looping

    I have to create a program that takes one integer (the value of the integer will be 1, 2, or 3). The program behaves differently depending on the value of the integer. If the number is 1, then get 2 integers from the user, and print the remainder of the first number divided by the second. If the number given is 2, then take a double from the user and print the double rounded up to the next integer. If the number given is 3, read an int from the user and print whether the number given is odd or even.

    I have created the following code but I am having trouble with the 3 different numbers running 3 different jobs. - rounding to the next integer etc

    Code:
    #include <stdio.h>
    
    int main() 
    {
    	int x, y, z;
    	double d;
    	
        	printf("Please enter in 1, 2 or 3: ");
    		scanf("%d", &x);
    		
    		if (x == 1)
    {
    			printf("Please enter in 2 new integers: ");
    			scanf("%d%d", &x, &y);
    			printf("%d", x % y);
    }			
    		else if (x == 2)
    		
    			{
                printf("Please enter in a double with a decimal: ");
    			scanf("%f", &d);
                printf("%f\n", d); 
                }
    		else (x == 3);
    	 {
                printf("Please enter in an integer: ");
                scanf("%d", x);    
                } 
                   if (x % 2 == 0)
                   {
    			    printf("%d is even\n", x); 
                   }
    			
    		        else if (x % 2 != 0)
    			{
    			printf("%d is odd\n", x); 
                }
    	}
         
    	return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > else (x == 3);
    else doesn't have a condition.

    > scanf("%d", x);
    Compare this with your other scanf calls.

    Finding problems is easier with a consistent indent policy.
    SourceForge.net: Indentation - cpwiki
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I'm not sure what you are having problems with exactly, but here you miss an ampersand at least.

    Code:
            else (x == 3);
            {
                    printf("Please enter in an integer: ");
                    scanf("%d", x);
            }

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    34
    Quote Originally Posted by Subsonics View Post
    I'm not sure what you are having problems with exactly, but here you miss an ampersand at least.
    first off my program wont even compile.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,614
    Quote Originally Posted by benrogers View Post
    first off my program wont even compile.
    Give the errors and indent your code right to get further help.

    Tim S.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by benrogers View Post
    first off my program wont even compile.
    Well, then why didn't you say so. My point was that your problem description was pathetic.

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    34
    Code:
    #include <stdio.h>
    
    int main() 
    {
    	int x, y, z;
    	double d;
    	
        	printf("Please enter in 1, 2 or 3: ");
    		scanf("%d", &x);
    		
    		if (x == 1)
    {
    			printf("Please enter in 2 new integers: ");
    			scanf("%d%d", &x, &y);
    			printf("%d", x % y);
    }			
    		else if (x == 2)
    		
    			{
                printf("Please enter in a double with a decimal: ");
    			scanf("%f", &d);
                printf("%f\n", d); 
                }
    		else (x == 3);
    	 {
                printf("Please enter in an integer: ");
                scanf("%d", x);    
                } 
                   if (x % 2 == 0)
                   {
    			    printf("%d is even\n", x); 
                   }
    			
    		        else if (x % 2 != 0)
    			{
    			printf("%d is odd\n", x); 
                }
    	}
         
    	return 0;
    }
    40 expected unqualified-id before "return"
    40 expected unqualified-id before "return"
    41 expected declaration before '}' token

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Code:
    #include <stdio.h>
    
    int main()
    {
            int x, y, z;
            double d;
    
            printf("Please enter in 1, 2 or 3: ");
            scanf("%d", &x);
    
            if (x == 1)
            {
                    printf("Please enter in 2 new integers: ");
                    scanf("%d%d", &x, &y);
                    printf("%d", x % y);
            }
            else if (x == 2)
            {
                    printf("Please enter in a double with a decimal: ");
                    scanf("%lf", &d);    // %f changed to %lf
                    printf("%lf\n", d);  // ditto
            }
            else // redundant check removed here
            {
                    printf("Please enter in an integer: ");
                    scanf("%d", &x); // ampersand added here   
            }
    
            if (x % 2 == 0)
            {
                    printf("%d is even\n", x);
            }
            else if (x % 2 != 0)
            {
                    printf("%d is odd\n", x);
            }
    
    // an extra brace found here removed
    
            return 0;
    }

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    34
    Quote Originally Posted by Subsonics View Post
    Code:
    #include <stdio.h>
    
    int main()
    {
            int x, y, z;
            double d;
    
            printf("Please enter in 1, 2 or 3: ");
            scanf("%d", &x);
    
            if (x == 1)
            {
                    printf("Please enter in 2 new integers: ");
                    scanf("%d%d", &x, &y);
                    printf("%d", x % y);
            }
            else if (x == 2)
            {
                    printf("Please enter in a double with a decimal: ");
                    scanf("%lf", &d);    // %f changed to %lf
                    printf("%lf\n", d);  // ditto
            }
            else // redundant check removed here
            {
                    printf("Please enter in an integer: ");
                    scanf("%d", &x); // ampersand added here   
            }
    
            if (x % 2 == 0)
            {
                    printf("%d is even\n", x);
            }
            else if (x % 2 != 0)
            {
                    printf("%d is odd\n", x);
            }
    
    // an extra brace found here removed
    
            return 0;
    }
    Thanks for this.

    - when entering either 1,2 or 3 it always says the number is either odd or even.
    - when entering 1 it does not show the remainder.

    when the number 3 is entered everything is fine.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,520
    Because your odd/even check is outside the else condition. Also, the if (x %2 != 0) is redundant, so ditch it.

  11. #11
    Registered User
    Join Date
    Jan 2011
    Posts
    34
    Quote Originally Posted by anduril462 View Post
    Because your odd/even check is outside the else condition. Also, the if (x %2 != 0) is redundant, so ditch it.
    Alright fixed that.

    Updated code:
    Code:
    #include <stdio.h>
    
    
    int main()
    {
            int x, y, z;
            double d;
    
            printf("Please enter in 1, 2 or 3: ");
            scanf("%d", &x);
    
            if (x == 1)
            {
                    printf("Please enter in 2 new integers: ");
                    scanf("%d%d", &x, &y);
                    
                    z = x % y;
                    
                    printf("%d", z);
            }
            
    	else if (x == 2)
            {
                    printf("Please enter in a double with a decimal: ");
                    scanf("%lf", &d);    
                    printf("%lf\n", d++);  
            }
            
    	else 
            {
                    printf("Please enter in an integer: ");
                    scanf("%d", &x);  
    
            if (x % 2 == 0)
            {
                    printf("%d is even\n", x);
            }
            
    	else
            {
                    printf("%d is odd\n", x);
            }
    }
    
            return 0;
    }
    The remainder isn't working when you press 1 and enter in 2 integers. Everything else works.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,520
    Remainder worked fine for me. What test data did you use? Your rounding (item 2) however, does not work at all. You just print the number as a float. Your problem specifies this number be "rounded up to the nearest integer". Do you mean always round up, like 123.4 rounds up to 124? If so, look at ciel(). If you mean the more traditional "round to nearest", look at round().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looping problem
    By main() in forum C Programming
    Replies: 4
    Last Post: 05-27-2010, 02:28 AM
  2. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 11:53 AM
  3. Problem with looping.
    By rina in forum C Programming
    Replies: 9
    Last Post: 10-05-2005, 01:21 AM
  4. looping problem
    By chris285 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 11:03 AM
  5. problem with looping
    By bajanstar in forum C Programming
    Replies: 7
    Last Post: 03-09-2005, 12:40 PM

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