Thread: getchar() problem.

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    getchar() problem.

    I'm trying to use getchar with scanf and I have this bug here that ignores the first whitespace character of the first loop. YES I want to use scanf and getchar TOGATHER in order to understand it better and NO i don't want to use fgets.

    I'm just curious what the bug is. I know its like < line of code:
    Code:
    /* This code prints out a multiplication table based on the user's input
    * it uses scanf and test for the num of return arguments so there is no undefined
    behaviour. It works on windows XP compiled with dev c++ 4.9.8.0. to port it simply
    remove the system calls. however  you won't have the pretty interface ;)
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #define MAXCLM 20 /* MAX number of clms (horizontal integers) */
    #define MAXCHOICE 40 /*MAX number of rows so no bigger than the screen */
    #define FLUSH while ( (getchar() !='\n')); 
    void Winconsole(void);
    
    int main(void){
        int choice, row, clm;
        
        Winconsole();
        /* problems: ignores first input unless its a 'q' */
        
        while ( (getchar()) != 'q'){
           if( (scanf("%d", &choice) !=1) || choice > MAXCHOICE || choice < 1){
              FLUSH
              printf("Invalid integer. Unable to print to screen\n");
              printf("Try again: ");
           }
           
           else {
              for (row = 1; row <= choice; row++){
                 for (clm = 1; clm  <= MAXCLM; clm++){
                    printf("%4d", row * clm);
                 }
              printf("\n");
              }//end of outer if
              
              printf("Enter another integer or [q] to quit: ");
           }//end of else
           
       }// end of while
           
       return 0;
    
    }//end of main
                    
     
    
    void Winconsole(){
        system("TITLE Multiplication Table 1.0 by bitshadow.");
        system("COLOR 64");
        printf("What Multiplication table would you like: ");
        }
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Can you explain your problem better? All I see is a fairly typical problem with scanf and getchar where a newline is left in the stream, causing it to appear as if the call to getchar was ignored.
    My best code is written with the delete key.

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    You've hit it on the head. I'm just trying to understand how to make the two work togather. As of now, scanf seems only well suited to being terminated properly when its a number.

    Is there a way to make it work? with the existing code.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is there a way to make it work? with the existing code.
    Add a FLUSH in the else block.
    My best code is written with the delete key.

  5. #5
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    okay i'm lost.
    I've checked the program state. but i'm still having problems. where in the else block???
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAXCLM 20 /* MAX number of clms (horizontal integers) */
    #define MAXCHOICE 40 /*MAX number of rows so no bigger than the screen */
    #define FLUSH while ( (getchar() !='\n')); 
    void Winconsole(void);
    
    int main(void){
      int choice, row, clm;
    
      Winconsole();
      /* problems: ignores first input unless its a 'q' */
    
      while ( (getchar()) != 'q'){
        if( (scanf("%d", &choice) !=1) || choice > MAXCHOICE || choice < 1){
          FLUSH
          printf("Invalid integer. Unable to print to screen\n");
          printf("Try again: ");
        }
        else {
          FLUSH // Here
          for (row = 1; row <= choice; row++){
            for (clm = 1; clm  <= MAXCLM; clm++){
              printf("%4d", row * clm);
            }
            printf("\n");
          }//end of outer if
    
          printf("Enter another integer or [q] to quit: ");
        }//end of else
    
      }// end of while
    
      return 0;
    
    }//end of main
    
    void Winconsole(){
      system("TITLE Multiplication Table 1.0 by bitshadow.");
      system("COLOR 64");
      printf("What Multiplication table would you like: ");
    }
    My best code is written with the delete key.

  7. #7
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    I put it there origianlly, however it doesn't fix the problem. it still ignore s the first input .
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >it still ignore s the first input
    You realize that you need to enter a single character to satisfy getchar before typing a number for scanf, right? Otherwise it would appear as if a single digit number is being ignored. Give it an input of 12 when the program starts. The table printed will only be for 2 because getchar eats the 1.
    My best code is written with the delete key.

  9. #9
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Yeah I understand that. that's the prob i've been having. :d
    but how does the FLUSH call solve it. and how can i solve it using getchar and scanf. or give me a bone or something
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but how does the FLUSH call solve it.
    It doesn't. That's a logic problem, not a getchar/scanf mix problem. Adding the FLUSH "fixes" the getchar/scanf mix problem, but the logic problem still exists. To fix it you would need to change the structure of your loop a bit:
    Code:
    while (1){
      if( (scanf("%d", &choice) !=1) || choice > MAXCHOICE || choice < 1){
        FLUSH
          printf("Invalid integer. Unable to print to screen\n");
        printf("Try again: ");
      }
      else {
        int ch;
        FLUSH // Here
        for (row = 1; row <= choice; row++){
          for (clm = 1; clm  <= MAXCLM; clm++){
            printf("%4d", row * clm);
          }
          printf("\n");
        }//end of outer if
    
        printf("Enter another integer or [q] to quit: ");
        if ((ch = getchar()) != 'q') {
          ungetc(ch, stdin);
        }
        else {
          break;
        }
      }//end of else
    }// end of while
    My best code is written with the delete key.

  11. #11
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Thank you ma'am.

    I think. I guess you get bored with simplistic problems sometimes you need to have a little fun.
    I understand
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I guess you get bored with simplistic problems sometimes you need to have a little fun.
    The who and the what now?
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 02:49 PM
  3. problem with parser code
    By ssharish2005 in forum C Programming
    Replies: 2
    Last Post: 12-02-2005, 07:38 AM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM