Thread: getchar() help

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    18

    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();
           }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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: Scanning new line.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    18
    Okay Thank you. I will try this

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why two getchar() ?
    By ekosix in forum C Programming
    Replies: 2
    Last Post: 06-05-2010, 01:13 PM
  2. Using getchar()
    By countchocula in forum C Programming
    Replies: 13
    Last Post: 04-23-2008, 08:07 AM
  3. getchar() and '\b'
    By snfiddy in forum C Programming
    Replies: 2
    Last Post: 11-30-2005, 05:52 PM
  4. getchar()
    By linuxdude in forum C Programming
    Replies: 7
    Last Post: 09-05-2004, 09:17 PM
  5. getchar
    By TyrioN in forum C Programming
    Replies: 5
    Last Post: 09-01-2004, 07:04 PM

Tags for this Thread