Thread: ***need help****

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    2

    ***need help****

    try to make a simple cm to mm program, it works (sort of) but if someone can run this code for me and tell me where i went wrong it'll be greatly appreciated

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    int main()
    {
    int a,b;
    
    
    printf("1.enter CM\n ");
    printf("2.enter mm\n");
    scanf("%d",&a);
    
    
    switch (a){
         case 1:
             b=a*10;
             printf("%d mm ",b);
             break;
         case 2:
             b=a/10;
             printf("%d Cm",b);
             break;
         default:
            printf("Backup dancer!!!");
    
    
    }
    
    
    
    
    return 0;
     }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    It would be much easier if you told us what was wrong, instead of making us guess.

    Jim

  3. #3
    Registered User
    Join Date
    Feb 2015
    Posts
    33
    From what I can see you are trying to determine whether to do '1.enter cm' or '2.enter mm' by using the 'int a' to decide which switch to use, but you are then using the 'a' variable to be used as a integer to convert with b=a*10.

    The only possibility with the code at the minute is
    a = 1, so
    case 1 is when a = 1, and then b therefore is 1*10, because b=a*10.

    or
    a = 2, so
    case 2 is when a = 2, and then b therefore is 2/10.

    You need 'a' to decide which switch to use but then you also need to create another variable to use scanf because you are using 'a' for two different things, so you could do
    a = 1, do case 1
    c = 6 (Another variable to convert via input from scanf)
    b = c*10

  4. #4
    Registered User
    Join Date
    Nov 2015
    Posts
    10
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
     
     
     
    int main()
    {
    float enterednumber,result;
    char choose;
     printf("Choose:\n1.CM to mm\n2.mm to CM\n");
     scanf("%c",&choose);
    	if (choose=='1')
    	{
    printf("1.enter CM\n ");
    scanf("%f",&enterednumber);
     result=enterednumber*10;
    printf("%6.2fmm\n",result);
    }
    else if (choose=='2')
    	{
    printf("1.enter mm\n ");
    scanf("%f",&enterednumber);
     result=enterednumber/10;
    printf("%6.2fCM\n",result);
    }
    else
    printf("Wrong sign!\n ");
     
     
    
    
     
     
     
     
    return 0;
     }

Popular pages Recent additions subscribe to a feed