do loop not working properly

My loop isnt working properly.At the end of the program, it dosen't ask whether i want to continue.PLEASE HELP

#include<stdio.h>
#include<math.h>

void main(void)
{
//declaration
float E,m,c;
int number;
char yesno;
//choose what to find
do
{
printf("Do you want to find the energy,mass or speed of light?E=1,M=2,L=3 :");
scanf("%d",&number);
switch(number)
{
case 1 : printf("This program calculate the energy based on the mass and the speed of light\n");
printf("Enter the values of mass and speed of light:");
scanf("%f%f",&c,&m);
E = m*c*c;
printf("The energy is %.2lf J.\n",E);
break;
case 2: printf("This program calculate the mass based on the energy and the speed of light\n");
printf("Enter the values of energy and speed of light:");
scanf("%f%f",&E,&c);
m=E/(c*c);
printf("The mass is %.2lf g\n",m);
break;
case 3 : printf("This program calculates the speed of light base on the energy and mass\n");
printf("Enter the values of energy and mass:");
scanf("%f%f",&E,&m);
c=sqrt(E/m);
printf("The speed is %.2lf ms per sec\n",c);
break;
default : printf("There is no such choice\n");
printf("byebye\n");
} // end switch
printf("Do you still want to continue?(Y/N)\n");// asking whether user wants to continue with the program anot.
scanf("%c",&yesno);
} while(yesno=='Y');// end loop
} // end main

Do this -> scanf("%c",&yesno); twice. The first call will get rid of the '\n' character in the input buffer, left there from the previous operations, and the second one will get the user's choice ;)
Last edited on
Thank you.But i still dont understand why this solves the problem.Why does the 1st call get rid of the \n character?and why does \n even need to be get rid of?
When you type in your answer to the question you type a *two* characters, the 'y' and an '\n' when you press the Return key.

scanf("%c,&yesno); only reads the 'y' but the '\n' is still sitting in the input buffer and it will be read next time you call scanf() instead of your answer 'y' or 'n'.
Last edited on
I actually got that.I UNDERSTOOD THAT.thanks so much
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <cstdio>

int main()
{
    int i;
    float f;
    char c;

//////////////////////////////////

    printf("enter an integer: ");

    //this scanf call leaves
    //a '\n' in the input buffer
    //(because you hit enter)
    scanf("%d",&i);

    printf("you entered %d\n\n",i);

//////////////////////////////////

    printf("enter an float: ");

    //this scanf call ignores
    //that '\n' (because a '\n'
    //can't be part of a float)
    //gets the float and leaves
    //another '\n' in the buffer...
    //(the enter you hit)
    scanf("%f",&f);

    printf("you entered %f\n\n",f);

//////////////////////////////////

    printf("hit enter to quit...");

    //this scanf call won't ignore
    //the '\n' in the buffer as it
    //is a valid value for a char.
    //Thus it'll get it and return.
    scanf("%c",&c);

    //That's why we need one more
    //call to actually make you
    //have to hit enter.
    scanf("%c",&c);

    return 0;
}

However in your case, since you want to get a 'Y' or 'N' and not a '\n', you could use scanf("\n%c",yesno);.
This will ignore all newline characters before your actual input.

Read here for more info -> http://www.cplusplus.com/reference/clibrary/cstdio/scanf/
Last edited on
Now i understood even better.lols.
Topic archived. No new replies allowed.