getchar

This is a discussion on getchar within the C Programming forums, part of the General Programming Boards category; I am getting mad. I don't know why this isn't working It always repeats? I feel like an idiot Code: ...

  1. #1
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926

    getchar

    I am getting mad. I don't know why this isn't working It always repeats? I feel like an idiot
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void){
            int input;
            do{
            printf("1.  Add person\n");
            printf("2.  Remove Person\n");
            printf("3.  View List\n");
            printf("4.  Exit");
            input=getchar();
            }
            while(input!='1' || input!='2' || input!='3' || input!='4');
            if(input=='4'){
                    printf("Exiting. . .\n");
                    exit(0);
            }
            return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,746
    No matter what you type in, at least one of your != tests will be true
    So it goes round forever

    Try
    input == '1' || input == '2' || input == '3'
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Think about you giving input, what happens?

    1enter

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    thanx both of ya'll were right here it is now
    Code:
    int main(void){
            int input;
            do{
            printf("1.  Add person\n");
            printf("2.  Remove Person\n");
            printf("3.  View List\n");
            printf("4.  Exit");
            input=getchar();
            FLUSH;
            }
            while(input<'1' || input>'4');
            if(input=='1')
                    add_person();
            /*other if's to come*/
            if(input=='4'){
                    printf("Exiting. . .\n");
                    exit(0);
            }
            return 0;
    }
    and FLUSH is defined as
    Code:
    #define FLUSH  while(getchar()!='\n' && getchar()!=EOF)

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    and FLUSH is defined as
    Code:
    #define FLUSH  while(getchar()!='\n' && getchar()!=EOF)
    Except that's wrong. That reads two characters. If the first character in the input stream is a space, and the second is newline, it'll, well, break.

    Try:
    Code:
    #define FLUSH {int c=0;while((c=getchar()!='\n' && c != EOF );}
    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    #define FLUSH while(getchar()!='\n' && getchar()!=EOF)

    You have two calls to getchar() here which would be problematic, because the second call misses the first variable from the first call.

    You'll need to save the variable and then do the tests on it.
    eg.
    Code:
    int val;
    val = getchar();
    if (val == '\n' || val == EOF )
    {
       // Do whatever
    }

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    thanx. I got it changed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 02:49 PM
  2. getchar buffer size
    By oncemyway in forum C Programming
    Replies: 3
    Last Post: 08-02-2005, 01:49 AM
  3. getchar() problem from K&R book
    By anemicrose in forum C Programming
    Replies: 13
    Last Post: 04-05-2004, 12:06 AM
  4. help with getchar lol
    By Taco Grande in forum C Programming
    Replies: 5
    Last Post: 03-18-2003, 09:25 PM
  5. Can anybody take a look at this?
    By TerryBogard in forum C Programming
    Replies: 10
    Last Post: 11-21-2002, 01:11 PM

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