Advertisement
Lisaveta777

Short Artish with function

Aug 24th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.29 KB | None | 0 0
  1. #include <stdio.h>
  2. //short artishok with functions, some questions
  3. //1. why Pay attention prints, even when first in input is alpha? check in main()
  4. //2. how make while ... a, b, c, q  more compact?
  5. //
  6.  
  7.  
  8. char get_char(void);
  9. float  get_float (void);
  10.  
  11. int main()
  12. {
  13.     char ch;
  14.     float  fl;
  15.  
  16.     while((ch =get_char())!='q')
  17.     {
  18.         printf("%c\n",ch);//print proper input,rid of user mistakes
  19.         fl= get_float();
  20.         printf("%f\n",fl);
  21.  
  22.     }
  23.  
  24.  
  25.     return 0;
  26. }
  27. char get_char(void)
  28. {//input:38kn... - get rid of everything
  29.  //      kn... takes k, get rid of everything else
  30.  //      k     takes k
  31.     char c;
  32.     printf("Chose a,b,c or q?\n");
  33.     while((c=getchar())!= 'a' && c!='b' && c!='c' &&c!='q')
  34. //  while(!isalpha(c=getchar()))  //while first symbol in input is not alpha
  35.     {
  36.         //printf("Pay attention, you suppose to input a,b,c or q\n");
  37.  
  38.     }
  39.     while(getchar()!='\n')  //rid of rest of input, including '\n'
  40.         continue;
  41.         return c;
  42. }//end of get_char
  43.  
  44. float get_float(void)//this one is ok
  45. {
  46.     float amount;
  47.     int sc_res =0;
  48.     do
  49.     {
  50.         printf("how much of it do you want?\n");
  51.         sc_res = scanf("%f",&amount);
  52.         if(!sc_res)
  53.             scanf("%*s");
  54.     }while(!sc_res);
  55.     return amount;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement