Thread: getchar

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  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, 12:49 AM
  3. getchar() problem from K&R book
    By anemicrose in forum C Programming
    Replies: 13
    Last Post: 04-04-2004, 11:06 PM
  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