Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12

    Basic problem w/ getchar()..


    This code, should keep asking for a char until the user puts either an X or O. The problem is that it is counting 'char and enter(\n)' as 2 chars.. as it should. If I put another getchar(); under choice=getchar(), it works. But if the user puts "555555555555" for example, it will freak out..

    How can I just accept 1 char input, but make it stable/user friendly (for example to fix this the user can type a char and press ctrl Z.. but most people will press enter alone).

    Code:
    int main(int argc, char *argv[]) {
    	int choice=0; 
    
    	while(choice!='X' && choice!='O') {
    		printf("Pick either X, or O: "); 
    		choice=getchar(); 
    		printf("choice: %d\n", choice); 
    	}
    	return 0; 
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    118
    Rep Power
    13
    Why is "choice" an integer when you're scanning a character into it?
  4. #3
  5. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    b/c that's what getchar() returns.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    35
    Rep Power
    12
    Originally posted by m0dr0cker
    Why is "choice" an integer when you're scanning a character into it?
    putting a char in single quotes

    Code:
    int bob = 'a';
    converts the character "a" to an integer.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    118
    Rep Power
    13
    Um, I've always read my getchar() output to a character.
  10. #6
  11. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    Code:
    int main(int argc, char *argv[]) {
    
            int     choice = 0; 
    
            printf("Pick either X, or O: ");
            while( ((choice = getchar()) != EOF) && ((choice != 'X') && (choice != 'O')) ){ 
                    if(choice == '\n'){
                            printf("Pick X or O: ");
                            fflush(stdout);
                    }
            }
            
            printf("choice: %d\n", choice);
            
            return 0; 
    }
    eax<< the thing to note is that when using the stdio library stdout is line buffered. thus the the call to fflush() is necessar, or instead you could do setbuf(stdout, NULL) to make it unbuffered.
  12. #7
  13. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    Originally posted by m0dr0cker
    Um, I've always read my getchar() output to a character.
    man getchar()
    int getchar(void);
    RETURN VALUE
    fgetc(), getc() and getchar() return the character read as an unsigned
    char cast to an int or EOF on end of file or error.
    just b/c you do it doesnt mean its right. a character cannot hold the value -1...

    <:oo7:> cat char.c
    main()
    {
    char a, b, c;

    a = -1;
    b = 'h';
    c = '-1';

    printf("a=%c b=%c c=%c\n", a, b, c);
    }

    <:oo7:> gcc char.c
    char.c:7:13: warning: multi-character character constant
    char.c: In function `main':
    char.c:7: warning: overflow in implicit constant conversion
    <:oo7:> ./a.out
    a=(char u cant see on here) b=h c=1
    Last edited by infamous41md; October 6th, 2003 at 04:36 PM.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    35
    Rep Power
    12
    The fact that getchar() returns an int, is an ugly part of C.
    It returns an int because if it has to return every possible character plus EOF (-1), then you immediately have the problem that the number of return values is exactly one greater than the possible number of values that can be held by a char. Hence, C cannot reutrn a char for getchar().
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    35
    Rep Power
    12
    Originally posted by C_rookie
    The fact that getchar() returns an int, is an ugly part of C.
    It returns an int because if it has to return every possible character plus EOF (-1), then you immediately have the problem that the number of return values is exactly one greater than the possible number of values that can be held by a char. Hence, C cannot reutrn a char for getchar().
    ....I mean an int for getchar().
  18. #10
  19. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    W.R.T the original question, it will depend on your platform, and compiler (or at least its libraries). In Windows you can use the WIN32 console API: http://msdn.microsoft.com/library/de..._functions.asp

    Borland compilers and older Microsoft, and other compilers have a conio library that gives a little more flexibility than stdio. If you have this, you can use getch() instead of getchar(). It works in character rather than line mode.

    scanf() may work for you, try
    Code:
    char choice ;
    int check ;
    
    check = scanf( "%c", &choice ) ;
    
    if( check != 0 )
    {
        // choice contains a character here
    }
    else
    {
        // some error
    }
    This will still normally work in line mode however on most platforms.

    Clifford
  20. #11
  21. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12
    Thanks all.
  22. #12
  23. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12
    This messes up, first time around, it is good. Every other time around, it says: "Pick either X, or O: Pick X or O: "

    Code:
    int main(int argc, char *argv[]) {
        int choice = 0; 
        while(1) {
            printf("Pick either X, or O: ");
            while( ((choice = getchar()) != EOF) && ((choice != 'X') && (choice != 'O')) ) { 
                    if(choice == '\n') {
                            printf("Pick X or O: ");
                            fflush(stdout);
                    }
            }
            printf("choice: %d\n", choice); 
        }
        return 0; 
    }
  24. #13
  25. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    how does it messup? here is my output:
    <:oo7:> ./a.out
    Pick either X, or O: alsjdf
    Pick X or O: asdfasdfdsafsadf
    Pick X or O: asdfasdf
    Pick X or O: sadfsad
    Pick X or O: fAAAAAAAAAAAAAAAAAAA aaaaaa
    Pick X or O: alsjdflask;jflskdjflsdkjfalskfjlsdkjflsadfjasldfjlsadkfjsldkfj
    Pick X or O: a
    Pick X or O: aa
    Pick X or O: a
    Pick X or O: a
    Pick X or O: a
    Pick X or O:
    Pick X or O: alaskjflaks;jf fsdkljf ds jjk kjl jkl klj jl ljkljk jlk klj
    Pick X or O: X
    choice: 88
  26. #14
  27. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    try this:
    printf("Pick either X, or O: ");
    fflush(stdout, NULL);
    while(
  28. #15
  29. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12
    But with the new outer while(1) loop. After it says choice: 88, it says:

    Pick either X, or O: Pick X or O: a <--
    Pick X or O: aaaaaaa
    Pick X or O: xfdafasdfasdf
    Pick X or O: X
    choice: 88
    Pick either X, or O: Pick X or O: <--


    The arrows point to the lines that come out messed up. (This is using NULL in fflush, same output as without).
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo