Character reading problem

This is a discussion on Character reading problem within the C Programming forums, part of the General Programming Boards category; Hi, Please take a look at this. It's only basic, since I'm doing my foundations course in C at university. ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    9

    Character reading problem

    Hi,

    Please take a look at this. It's only basic, since I'm doing my foundations course in C at university.

    When I run the compiled programme, I get the following output:

    Code:
    [ec09411@dil027 8_3]$ ./Lab8_3
    Enter an integer (): 4
    Even
    Continue (y/n): Enter an integer (): ^C
    [ec09411@dil027 8_3]$
    So I never get to input whether or not I want to continue (y/n). It just skips straight onto the while and loops round. Any ideas? Maybe it's just a tiny error, but I'll be damned if I can see it. Thanks!

    Code:
    #include <stdio.h>
    
    int even(int input);
    
    int main(void) {
        char contin;
        int input;
    
        do {
            contin = 0;
    
            printf("Enter an integer: ");
            scanf("%d", &input);
    
            if (even(input)) {
                printf("Even\n");
            } else {
                printf("Odd\n");
            }
    
            printf("Continue (y/n): ");
            scanf("%c", &contin);
        } while (contin != 'n');
    
        return 0;
    }
    
    int even(int input) {
        return !(input%2);
    }
    /*End of programme*/

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Code:
    #include <stdio.h>
    
    int even(int input);
    
    int main(void) {
        char contin;
        int input;
    
        do {
            contin = 0;
    
            printf("Enter an integer: ");
            scanf("%d", &input);
    
            if (even(input)) {
                printf("Even\n");
            } else {
                printf("Odd\n");
            }
    
            printf("Continue (y/n): ");
            scanf(" %c", &contin); //put a space before %c
        } while (contin != 'n');
    
        return 0;
    }
    
    int even(int input) {
        return !(input%2);
    }
    /*End of programme*/
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    999
    I was about to say that you need to fflush(stdin) before your 2nd scanf (it's reading your carriage return as the next char I think), but I forgot that's undefined. Check out this thread: alternative for fflush(stdin) on some better things to use than just plain scanf.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    9
    Awesome guys. Thank you. That's one to be passed around.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with reading in file...
    By Bizmark in forum C Programming
    Replies: 3
    Last Post: 03-27-2008, 02:11 PM
  2. Problem reading file
    By coder_009 in forum C Programming
    Replies: 10
    Last Post: 01-15-2008, 01:22 PM
  3. Character problem!!
    By cBegginer in forum C Programming
    Replies: 3
    Last Post: 09-03-2005, 12:51 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 04:23 PM

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