case statement not being read

no idea why but for some reason whatever i enter in as a function either s, c, t or x. none of them work, it goes straight to default if i have one, and i not nothing happens, can any1 tell me why?


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


int main()
{
char loop = 1;
float angle;
float answer;
char functions;

printf("[S] to calculate sin(x)\n");
printf("[C] to calculate cos(x)\n");
printf("[T] to calculate tan(x)\n");
printf("[X] or [x] to exit the program\n\n\n\n");


while(loop)
{

printf("enter the angle in radians\n\n");
scanf_s("%f\n", &angle);
printf("enter the function\n\n");
scanf_s("%c\n\n", &functions);

switch(functions)

{
case 'S':
case 's':
answer=sin(angle);
printf("the sine of the angle = %8.2f\n\n\n", answer);
break;

case 'C':
case 'c':
answer=cos(angle);
printf("the cosine of the angle = %8.2f\n\n\n", answer);
break;

case 'T':
case 't':
answer=tan(angle);
printf("the tangent of the angle = %8.2f\n\n\n", answer);
break;

case 'X':
case 'x':
printf("you have ended the program please press any key to exit");
loop = 0;
break;

}
}


getchar();
getchar();
return 0;
}
hi putting flushall() will solve the problem

printf("enter the angle in radians\n\n");
scanf("%f\n", &angle);
flushall();
printf("enter the function\n\n");
scanf("%c\n", &functions);
I would also specify the buffer size for &function, which is 1 in present case
1
2
3
4
5
printf("enter the angle in radians\n\n");
scanf_s("%f", &angle);
flushall();
printf("enter the function\n\n");
scanf_s("%c", &functions, 1);
You are having trouble with formatted vs unformatted input.

Remember, every time the user gives input, he will follow that input with a newline (because he pressed the ENTER key). Hence, after every input, you need to get rid of that enter key.

I find it useful just to have a little function to do it.
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdio.h>
#include <conio.h>
#include <math.h>

void scaneol()
  {
  int c;
  while (1)
    {
    c = getchar();
    if ((c == '\n') || (c == EOF)) break;
    }
  }


int main()
{
char loop = 1;
float angle;
float answer;
char functions;

printf("[S] to calculate sin(x)\n");
printf("[C] to calculate cos(x)\n");
printf("[T] to calculate tan(x)\n");
printf("[X] or [x] to exit the program\n\n\n\n");


while(loop)
{

  printf("enter the angle in radians\n\n");
  scanf("%f", &angle);
  scaneol();

  printf("enter the function\n\n");
  scanf("%c", &functions);
  scaneol();

  switch(functions)
  {
    case 'S':
    case 's':
      answer=sin(angle);
      printf("the sine of the angle = %8.2f\n\n\n", answer);
      break;

    case 'C':
    case 'c':
      answer=cos(angle);
      printf("the cosine of the angle = %8.2f\n\n\n", answer);
      break;

    case 'T':
    case 't':
      answer=tan(angle);
      printf("the tangent of the angle = %8.2f\n\n\n", answer);
      break;

    case 'X':
    case 'x':
      printf("you have ended the program please press any key to exit");
      loop = 0;
      break;

  }
}


getchar();
getchar();
return 0;
}

BTW, I think your user interaction needs just a little bit of help. You might want to ask the function first, and only if it isn't 'X' (quit) ask for the angle.

It is also worth giving a little prompt to let the user know something is expected, such as a colon after an instruction, or (as I prefer) a greater-than sign. It is also a good idea to verify that the user input worked properly:
1
2
3
4
5
6
printf( "Please enter some number\n> " );
fflush( stdout );
i = scanf( "%f", &some_number ); 
scaneol();
if (i > 0) printf( "Good job! You entered %f.\n", f );
else       printf( "Fooey! That was not a number!\n" );

Hope this helps.

Oh, also, please avoid stuff like flushall() ... it is not a very portable function and exists only to avoid thinking... which paradoxically makes it newbie-desireable and then becomes a bad habit to break.

Good luck!
Thank you so much guys :)
Topic archived. No new replies allowed.