0

I'm writing a code of a program that reads an input choice from the user and depending on that it calculates (area, circumference, volume, or quit)

#include<stdio.h>

int main(void)
{
    double radius=0.0;
    double area=0.0;
    double volume=0.0;
    double height=0.0;
    double circumference=0.0;
    const double pi=13.459820934;

    char choice;

    do
    {
    printf("Choose what you want to do : \n"
           "Calculate Area          A\n"
           "Calculate Circumference C\n"
           "Calculate Volume        V\n"
           "Quit                    Q\n");
    /*scanf("%c",&choice);*/
    choice=getchar();

    printf("please enter radius : ");
    scanf("%lf",&radius);

    if(choice=='V')
    {
        printf("please enter height : ");
        scanf("%lf",&height);
    }

    area = pi * radius * radius;
    volume = height * area;
    circumference = 2 * pi * radius;

    switch (choice)
    {
    case 'A':
        printf("Area = %lf\n\n",area);
        break;
    case 'V':
        printf("Volume = %lf\n\n",volume);
        break;
    case 'C':
        printf("Circumference = %lf\n\n",circumference);
        break;
    default:
        printf("Not a good answer !!\n\n");
    }
    }while(choice!='Q');

return 0;
}

when I run the program it worked well for the first do loop but after pringting the choices again as it begins the loop for the second time, it didn't wait for me to enter my choice, it directly printed "please enter radius"

when I debugged the program, I found there was '\n' in choice when it began the loop for the second time, how can I remove this '\n' every loop? I knew that this '\n' resulted from reading the user choice followed by from the first time the loop ran, but I don't know why the choice is removed from choice while was not?

I've tried to initialize choice before displaying the choices to the user but the same problem happened

any help please? thanks in advance

Dipto
  • 2,720
  • 2
  • 22
  • 42
Salahuddin
  • 1,617
  • 3
  • 23
  • 37
  • possible duplicate of [How to clear input buffer in C?](http://stackoverflow.com/questions/7898215/how-to-clear-input-buffer-in-c) – Lee Duhem Jan 10 '14 at 13:21

1 Answers1

1

The \n character is leaved behind by the scanf call after pressing Enter key . You need to consume this \n before next iteration, otherwise this \n character will read by next scanf or getchar.

Use the snippet

int ch;
while((ch = getchar()) != EOF || ch != '\n');

just before the closing brace } of do-while or after the second scanf.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • can you tell me what does it do? – Salahuddin Jan 10 '14 at 13:15
  • while(getchar()!= '\n'); I used this snippet and it worked So any input the user input is followed by '\n', so the input itself is read from the buffer and put into a variable (her is choice) but the '\n' is left behind in the buffer so we have to pretend that we are reading the input again to read this '\n' and clean the buffer and this is the funtion of while(getchar()!='\n'); if I'm right, why '\n' wasn't read with the input itself and put into the variable ? why it was left behind – Salahuddin Jan 10 '14 at 13:36
  • Yes, you are right. `\n` is not read by `scanf` because `%lf` expects an argument of `double` type. It can skip ant number of white-spaces(tab, space, new-line etc). – haccks Jan 10 '14 at 13:41
  • so while(getchar()!='\n'); is just used to remove whitespaces and '\n'? – Salahuddin Jan 10 '14 at 13:45
  • so why we use it in this snippet? do { printf("please give the radius "); while(getchar() != '\n') ; } while(scanf("%Lf", &radius) != 1); this snippet is used to make sure that the user entered a real number As you said while(getchar()!='\n') is used to clean whitespaces and '\n', so how come there could be a white space in this snippet sorry for the bad formating, don't know how to format a code in a comment – Salahuddin Jan 10 '14 at 13:49
  • `scanf("%lf",&radius);` only reads the radius input by the user. It does not read `\n`. This `\n` is read by the next `scanf` or `getchar`. Read my answer again. – haccks Jan 10 '14 at 13:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44954/discussion-between-salahuddin-and-haccks) – Salahuddin Jan 10 '14 at 14:08