Thread: do while problem

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

    Post do while problem

    Hi, I am very new to C and creating a program converting Celsius to Fahrenheit. I have a "do while" command so the user can loop the program if he/she choose to. The problem is that instead of looping the program it ignores the user input and continue the program.
    I hope someone can help me with this problem.
    Thanks in advance

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main ()
    
    
    {
        char temp ;
        int tempdegree ;
        float tempequals ;
        char restart;
        
        do
        {
            printf("Welcome to fahrenheit to celsius (vice versa) converter!\n\nPress F to convert fahrenheit to celsius or press C to convert celsius to fahrenheight: ") ;
            scanf ("%c", &temp);
            if (temp=='f' || temp=='F')
            {    
                system("cls");
                printf ("Please pick the temperature you want to convert:");
                scanf ("%d", &tempdegree);
                tempequals=0.555555556*(tempdegree-32);
                printf ("%d fahrenheit equals %f celsius.\n\n\nPress r to restart program", tempdegree, tempequals);
                scanf ("%c", &restart);
            }    
            else if (temp=='c' || temp=='C')
                {    
                    system("cls");
                    printf ("Please pick the temperature you want to convert:");
                    scanf ("%d", &tempdegree);
                    tempequals=(1.8*tempdegree)+32;
                    printf ("%d celsius equals %f Fahrenheit.\n\n\nPress r to restart program.", tempdegree, tempequals);
                    scanf ("%c", &restart);
                } 
        
        } while (restart=='r' || restart=='R');        //instead of looping when inserted "R" the program continues. 
        
        getchar ();
        
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    scanf is tricky with whitespace. scanf %c will give you the linefeed from the previous scanf which is not an 'r' or 'R'. change scanf("%c"... to scanf(" %c"... (add a space before %c)

    edit : do that for all the scanf("%c's including the top one

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    2
    Thank you so much for your help dmh2000, it worked perfectly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp problem, whats the problem, i cant figure it out!
    By AvaGodess in forum C Programming
    Replies: 14
    Last Post: 10-18-2008, 06:45 PM
  2. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  3. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 09:40 AM
  4. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM

Tags for this Thread