0
#include <stdio.h>
   main() {
    double fahr,cel;
    char again;

    do{
    printf("Enter Dgree");
    scanf("%lf",&fahr);
    printf("In Celsius that is %.1lf\n",(fahr-32)*5.0/9.0);
    printf("agian?");
    scanf("%c",&again);
    } while (again =='y');
}

I don't understand why this won't loop
any ideas?

Edited by Nick Evan: Fixed formatting

4
Contributors
4
Replies
7
Views
6 Years
Discussion Span
Last Post by Xeno86
0

Replace like:

char again[1];
scanf("%c",again);
} while (*again =='y');

But if I put printf after 2nd scanf, its crashing. But the same access in the next line (inside while) is working.
like

main() {
    double fahr,cel;
    char again[1];

    do{
        printf("Enter Dgree : ");
        scanf("%lf",&fahr);
        printf("In Celsius that is %.1lf\n",(fahr-32)*5.0/9.0);
        printf("agian?");
        scanf("%s", again);
        printf("%s", *again);
    } while (*again =='y');
}

Can anyone explain..

0

Hello xeno86,
Use fflush(stdin) before scanf for again as charley bones said. The loop doesn't work because as you press enter('\n') the '\n' is copied onto the variable again. You can also use flushall() before the scanf() for again.

Edited by Arbus: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.