Thread: loop not working correctly

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

    Question loop not working correctly

    i can't figure this out. the program loops but does not let you make another choice and just goes straight to press any button to continue,end. i'm new to all this so be gentile.
    Code:
    // Multi Function Calculator
    
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #define Pi 3.14159
    int main(void)
    
    {
        
    
    char num,ans;
    float voltage, resistance , current, amps, miles, km;
    float r, A, in, ft;
    double F, C;
        
        do{
    
        printf("Please choose from following calculcations.\n");
        printf("Choose 1 to calculate Voltage.\n");
        printf("Choose 2 to calculate Current.\n");
        printf("Choose 3 to calculate Resistance.\n");
        printf("Choose 4 to calculate Miles to Kilometers.\n");
        printf("Choose 5 to calculate Kilometers to Miles.\n");
        printf("Choose 6 to calculate Fahrenheit to Celisus.\n");
        printf("Choose 7 to calculate Celisus to Fahrenheit.\n");
        printf("Choose 8 to calculate the Area of a Circle.\n");
        printf("Choose 9 to calculate Inches to Feet.\n");
        printf("Press enter to quit.\n");
    
    
        scanf("%c",&num);
        switch(num)
    
    {        
    
    case '1' : // Volatage 
            printf("Enter the current in amps =>");
            scanf("%f",&amps);
            printf("Enter the resistance in ohms =>");
            scanf("%f",&resistance);
            voltage = amps * resistance;
            printf("\nThe voltage is %0.2f volts.\n\n",voltage);
            break;
    
    case '2' : // Current
            printf("Enter the voltage in volts =>");
            scanf("%f",&voltage);
            printf("Enter the resistance in ohms =>");
            scanf("%f",&resistance);
            current = voltage / resistance;
            printf("\nThe current is %0.2f amps.\n\n",current);
            break;
    
    case '3' :  // Resistance
            printf("Enter the voltage in volts =>");
            scanf("%f",&voltage);
            printf("Enter the current in amps =>");
            scanf("%f",&amps);
            resistance = voltage / amps;
            printf("\nThe resistance is %0.2f ohms.\n\n",resistance);
            break;
    
    case '4' :  // Miles to KM
            printf("Enter number of miles =>");
            scanf("%f", &miles);
            km = miles*1.609334;
            printf("\n%.0f miles is %.2f kilometers.\n\n", miles, km);
                break;
    
    case '5' :  // KM to Miles
            printf("Enter number of kilometers =>");
            scanf("%f", &km);
            miles = km*0.6214;
            printf("\n%.0f kilometers is %.2f miless.\n\n", km, miles);
                break;
    
    case '6' :  // F* to C*
            printf("Enter degrees Fahrenheit =>", F);
            scanf("%lf", &F);
            C = (F-32)*5/9;
            printf("\n%.0lf degrees Farenheit is %.2lf degrees Celsius.\n\n", F, C);
            break;
    
    case '7' :  // C* to F*
            printf("Enter degrees Celsius =>", C);
            scanf("%lf", &C);
            F = (C*(9/5))+32;
            printf("\n%.0lf degrees Celsius is %.2lf degrees Farenheit.\n\n", C, F);
            break;
    
    case '8' :  //Area of circle
            printf("Enter radius of the circle =>", r);
            scanf("%f", &r);
            A = Pi*r*r;
            printf("\nThe area of the circle is %.2f.\n\n", A);
            break;
    
    case '9' : // Inches to feet
            printf("Enter number of inches =>", in);
            scanf("%f", &in);
            ft = in/12;
            printf("\n%.0f inches is %.2f feet.\n\n", in, ft);
            break;
    
         
            default : exit(0);
        
        }
        
        {
                
                printf("Do you want another calculation?(Y/N)=>",ans);
                scanf(" %c",&ans);
                printf("\n");printf("\n");
                system("cls");
    }
            } while (ans == 'y');
    
    return(0) ;
    
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    This is really the only line of your code; I looked twice at.
    Code:
    scanf("%c",&num);
    It is normal for beginnings to forgot about the newline/linefeed char.

    Adding a space before the "%" percent sign skips over white-space chars like newlines.

    As like this.
    Code:
    scanf(" %c",&num);
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    2
    awesome, thanks. i've written this out 3 times and was going crazy. now i can sleep at nite.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. substr in a for loop isn't working correctly
    By jeremy duncan in forum C++ Programming
    Replies: 28
    Last Post: 06-03-2012, 12:43 AM
  2. Loop not working Correctly
    By patneel in forum C Programming
    Replies: 4
    Last Post: 03-15-2012, 07:25 AM
  3. Pointers are not working correctly
    By adrian_fpd in forum C Programming
    Replies: 8
    Last Post: 11-17-2008, 07:55 PM
  4. function not working correctly
    By mackieinva in forum C Programming
    Replies: 0
    Last Post: 09-29-2007, 08:22 PM
  5. File I/O not working correctly
    By gL_nEwB in forum C++ Programming
    Replies: 4
    Last Post: 05-27-2006, 10:29 PM