Can anyone tell me why it displays more than 1 line of the same?

This is a discussion on Can anyone tell me why it displays more than 1 line of the same? within the C Programming forums, part of the General Programming Boards category; This program everytime twice displays the line for the user to enter C, D or F. Why is that? Also, ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    Can anyone tell me why it displays more than 1 line of the same?

    This program everytime twice displays the line for the user to enter C, D or F. Why is that?
    Also, if the user enter more than 1 letter for the first prompt, the C, D, F line is printed multiple times in relation to how many letter the user entered.
    Help Appreciated

    Code:
    #include <stdio.h>
    #include <process.h>
    #include <conio.h>
    
    int sort_bytes();
    int create_screen();
    int initialise_com_port();
    
    char input;
    char input2;
    
    int main(void)
    {
       initialise_com_port();
    
       printf("This is the Data Packet Analyser program\n");
       printf("This program does...\n");
       printf("Enter 'C' to make a capture: ");
       scanf("%c", &input);
    
       sort_bytes();
       create_screen();
    
       getch();
       return 0;
    }
    
    
    int initialise_com_port()
    {
       printf("\nHello from initialise_com_port\n");
    }
    
    int sort_bytes()
    {
       printf("\n\nHello from sort_bytes!");
    }
    
    int create_screen()
    {
       printf("\n\nHello from create_screen!\n");
       do
       {
          printf("\n\nEnter 'C', 'D', or 'F': ");
          scanf("%c", &input2);
    
          switch ( input2 )
          {
             case 'C' : printf("\nFunction sort_bytes and function create_screen");
                        printf(" is called in.");
                break;
             case 'D' : printf("\nFunction create_file is called in");
                break;
          }
       }
       while ( input2 != 'F');
    
       printf("You have decided to exit from the program\nPress Enter");
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,263
    Your first scanf() call in the main() function reads a character from the stdin buffer. Anything else in the buffer gets read by the scanf() function in the create_screen() function which is why the do...while loop goes through a couple times.

    One option would be to clear out the input buffer with a fgets() call after a scanf() call.

  3. #3
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    how come 'scanf()' eliminates when it finds a '\n' in stdin in the first loop??? i thought it doesn't care about it

    oh and here are some tips for your code :

    those 3 functions :
    Code:
    int sort_bytes();
    int create_screen();
    int initialise_com_port();
    should have the type 'void' not 'int' because they won't return any value. They will just do their stuff and quit their functions without returning any values.


    When asking for single characters as input , use two cases for each character instead of one coveing both lower-case and upper-case letters. So this :
    Code:
    case 'C' : printf("\nFunction sort_bytes and function create_screen");
                        printf(" is called in.");
    should be :
    Code:
    case 'c' :
    case 'C' :
           printf("\nFunction sort_bytes and function create_screen");
           printf(" is called in.");
    you checked for only upper-case letters when people mostly type lower case letters by default.

    another thing , you can go to other lines in your program with using only one 'printf()' instead of typing it again on each line. It can be done this way :
    Code:
    printf("First line\n"
           "second line\n"
           "third line\n");
    and finally , you've included 2 useless header files , conio.h and process.h. You've only used functions that are declared in stdio.h so it should be there alone.
    Last edited by Brain Cell; 10-06-2004 at 08:27 AM.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding line numbers and concatenating a filename
    By durrty in forum C Programming
    Replies: 25
    Last Post: 06-28-2008, 04:36 AM
  2. Reading a buffer line by line, while using system read
    By Hammad Saleem in forum C Programming
    Replies: 9
    Last Post: 05-27-2008, 06:41 AM
  3. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 09:45 PM
  4. Trouble replacing line of file
    By Rpog in forum C Programming
    Replies: 4
    Last Post: 04-19-2004, 11:22 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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