getchar() not accepting 'ans' value

This is a discussion on getchar() not accepting 'ans' value within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> #include<conio.h> #include<ctype.h> void main() { char ans; int sum,n; sum=0; do { printf("\n Enter The Number:"); scanf("%d",&n); sum=sum+n; ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Satara,MH,India
    Posts
    6

    getchar() not accepting 'ans' value

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<ctype.h>
    void main()
    {
     char ans;
     int sum,n;
     sum=0;
     do
     {
      printf("\n Enter The Number:");
      scanf("%d",&n);
      sum=sum+n;
      printf("Do you Want to Continue:");
      ans=getchar();
      }
    while(ans=='y')
    printf("sum=%d",sum);
    getch();
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Look up getchar() in your C library documentation... And whle you're at it look up scanf() too.

    The problem is likely that scanf() is leaving the enter key in the input queue and that's bypassing getchar().
    Last edited by CommonTater; 09-10-2011 at 02:41 PM.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Satara,MH,India
    Posts
    6
    i also trace the program all is working fine, when i input number e.g 5 they show me as like this output 'Do you want to Continue:sum=5'
    but i want give the choice for continue y or n. when i WANT TO enter y it WANT TO go to up again but the 'ans=getchar()' not working so the 'while(ans==y)' is not working
    IN BIG TROUBLE WHATS WRONG HERE BRO.....

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I'm going to say this one more time...


    Look up getchar() in your C library documentation... And whle you're at it look up scanf() too.

    The problem is likely that scanf() is leaving the enter key in the input queue and that's bypassing getchar().
    Don't be coming in here asking questions then totally ignoring the answers... That really ticks people off!

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Additionally, VOID MAIN IS WRONG! Read How to define main - FAQ Conio.h and getch() are both nonstandard and should be avoided. I am willing to bet you are using turboC too, get a new compiler. (Click on the Pelles C link in Tater's sig)
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accepting formatted input
    By fxtdr79 in forum C Programming
    Replies: 15
    Last Post: 06-28-2010, 02:25 PM
  2. accepting +123 help
    By peanut in forum C Programming
    Replies: 7
    Last Post: 11-01-2006, 11:48 PM
  3. Accepting Decimals
    By dragon_man in forum C++ Programming
    Replies: 3
    Last Post: 06-04-2003, 11:09 PM
  4. Accepting function key 'F12'
    By Fire Flash in forum C Programming
    Replies: 1
    Last Post: 03-18-2002, 04:32 PM
  5. accepting char and int
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-02-2001, 11:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21