# getchar() help

• 02-26-2013
Zachary May
getchar() help
basically i have to do a summation of e, which is 1/k! and i need to allow the user to input a positive integer which i did. The second thing i need to do is once the total is given, i need to allow the user to enter another value or hit the return key to end the program. I know this has something to do with the getchar() function but i don't quite understand what to do.

The while(user>100); i put in temporary until i got the getchar() to work
Code:

```#include <stdio.h> #include <math.h> #include <stdlib.h> main() {       double total,f;       int n,user,c;       printf("This program will take the the summations of 1/k! \n");       printf("from zero to upper limit entered by the user\n\n");       {             do             {             printf("Please enter a postive integer:");             scanf("%d ",&user);             if (user>0){                       for(n=1,f=1,total=0;n<=user;++n)                           {                           f=f*n;                           total = total + (1/f);                           }                           printf("\nThe total of the summation is: %f\n\n",1+total);                         }           else                       printf("Please enter a positive integer");                       break;           printf("If you wish to quit, hit 'enter', if not type any other key\n");             }while(user>100);           getchar();       } }```
• 02-27-2013
Salem
First, learn how to indent code.
Code:

```#include <stdio.h> #include <math.h> #include <stdlib.h> main() {   double total, f;   int n, user, c;   printf("This program will take the the summations of 1/k! \n");   printf("from zero to upper limit entered by the user\n\n");   {     do {       printf("Please enter a postive integer:");       scanf("%d ", &user);       if (user > 0) {         for (n = 1, f = 1, total = 0; n <= user; ++n) {           f = f * n;           total = total + (1 / f);         }         printf("\nThe total of the summation is: %f\n\n", 1 + total);       } else         printf("Please enter a positive integer");       break;       printf("If you wish to quit, hit 'enter', if not type any other key\n");     } while (user > 100);     getchar();   } }```
Now, do you see the break on line 22.
This skips line 23 and 24, as it unconditionally breaks out of your while loop.

The else on line 20,21 should have braces as well, for clarity.

Now, using "hit enter" is problematic at the moment, because the scanf("%d",&user) will leave '\n' on the input stream to begin with. So simply calling getchar() to read a newline will immediately result in you thinking the program is over.

Perhaps to begin with, consider making "Enter 0 to quit" as your exit condition, as this is very simple to implement in your given program.
• 02-27-2013
anduril462
Mixing input methods (formatted, like scanf; character based, like getchar; and line-based, like fgets) generally causes problems. Getting them to all behave as expected with the new line character (placed in the input stream whenever you press "enter") is tricky.

My preferred method is to use fgets to read a whole line, then use functions to parse out the data I want. I like strtol or strtoul for converting to long/unsigned long (works for int and short too), and strtod for doubles/float. They make error checking easy. If I'm after character or string data, I can simply look at the whole input buffer, or a single char (e.g. buf[i] or buf[7]), or use sscanf (like scanf but for scanning strings instead of stdin) to parse out a myriad of data from the line.

Just remember, fgets leaves the newline in there, so you have to remove it, preferably before you use strtol/strtod/sscanf/etc. The quick-and-easy solution is found in this related FAQ article: FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com. A more thorough/robust way to strip the new line is discussed in this thread: http://cboard.cprogramming.com/c-pro...-new-line.html.
• 02-27-2013
Zachary May
Okay Thank you. I will try this