Thread: character handling

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    39

    character handling

    I am trying to make a program with the functionality that it stops to ask the user to press enter after every 24 entries. I have tried to use both getchar and scanf to do this, but it seems as if some other newline charachter is being read. Here is a sample of the code:

    Code:
    #include <stdio.h>
    
    int main(void){
                   int i,n,j=0;
                   char ch;
    
                    printf("****Square2 v2.01****\n");
                   printf("\nPlease enter the number of squares you wish to display:");
                   scanf("%d",&n);
    
                   for(i=1;i<=n;i++){
                                if(i%24==0){
                                        while((ch=getchar()) !='\n'){
                                                                                                                       printf("\n\nPlease hit enter to continue:\n\n");
                                                              }
                                        }
    
    
                                printf("%10d\t%10d\n",i,i*i);
                              }
    
              return 0;
    
                 }
    I have ruled out the new line charchters already present within the existing printf calls. Anyone have any ideas?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You use scanf to get the number of squares to display, so the user types a number, followed by an enter, like 123<enter>. The scanf stops after the '3' character and leaves the new line there to be processed later. Your first call to getchar will see that new line and continue on to the next 24 lines of output. scanf may behave the same way, depending on what format specifier you use ("%c" perhaps?). Read this: FAQ > Flush the input buffer - Cprogramming.com.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    39
    But shouldn't either getchar or scanf not run until the if statement is true?

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    39
    Thank you @anduril462. I decided to flush out the character by adding an extra call of getchar.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Right, but that newline doesn't go anywhere while you're printing output for i values 1..23. It's sitting in the input buffer. Also, printf works on stdout, the output stream (usually your monitor/console/command prompt). scanf, getchar and your other input routines work on stdin, the standard input stream (usually your keyboard). The two can never be mixed up*, so a newline from a printf can never screw up scanf or getchar.

    Also, you did not very clearly describe your problem, so I took a best guess, partly based on the fact that the problem I described is an incredibly common misunderstanding newbies seem to have. Perhaps describe in more detail exactly how it misbehaves. Does it only skip the first prompt? Does it skip all the prompts (if you give an large number like 100)?

    Tell me:
    1. What is the exact input you give it
    2. What is the exact output/behavior you expect
    3. What is the actual output/behavior you see. Copy and paste from your terminal if necessary.

    Also, your indentation is crap, you should fix it. You should browse through the FAQ articles we have here, the may help you fix this little problem you're having (perhaps it's one of the other "classics", like console window disappearing). Finally, try printing a prompt so the user knows to hit enter to see the next 24 results. That will help us figure out how many times it's reading an enter character that you think shouldn't be there.

    * You can do some crazy stuff to reroute stdin and stdout, but for all intents and purposes (especially yours while you learn C) they can't be mixed up.

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    39
    I am reteaching myself C from the ground up. I found a programming project in the book that stated to modify an old piece of code that printed rows of squares(to be shown below) so that after every 23rd square the program prompts the user to press the "Enter" key to continue. Now when I originally tried to write the program, upon adding in lines of code to try and test what was going on, I found that the newline character was being read by getchar (as well as scanf in slightly different versions that I tried). That is when I posted. Upon reading your reply post, I got the idea to use an extra call of getchar to assign the newline character to a char variable then assign an arbitrary character to that variable thus clearing the old newline character.

    Also, I'm not quite sure what you're referring to concerning the newline character originating from the input line. Wouldn't the if statement on line 12 of the code posted previously limit any "seen" newline charachters to those that come from the call of printf on line 19 of the same code above?


    original code that just prints squares:

    [code]
    #include <stdio.h>

    int main(void){
    int i,n;

    printf("\nPlease enter the number of squares you wish to display:");
    scanf("%d",&n);

    for(i=1;i<=n;i++)
    printf("%10d\t%10d\n",i,i*i);


    return 0;

    }

    [\code]

    New functional code that I have written
    Code:
    #include <stdio.h>
    
    
    int main(void){
                            int i,n,j=0;
                            char ch='A';
    
                            printf("****Square2 v2.01****\n");
                            printf("\nPlease enter the number of squares you wish to display:\n");
                            scanf("%d",&n);
                            printf("\n");
                       
    
                             for(i=1;i<=n;i++){
                                                      if(i%24!=0)
                                                          printf("%10d %10d\n",i,i*i);
    
                                                      else{
                                                              if(j==0){
                                                                          ch=getchar();
                                                                          j++;
                                                                         }                
                                                               
    
                                                               printf("Please press enter:");
                                                               ch='A';
                                                               while(ch!='\n')
                                                               ch=getchar();
                                                               printf("%10d %10d\n",i,i*i);            
                                                               }
                   
                                                             }
                                  
    
    
                              printf("\n****Program Terminated****\n");
    
                              return 0;
    
                     }


    What I would expect from the program I posted before was that after 23 entries, the program would prompt the user to press enter to continue. What I was seeing before I made the modification that fixed it was that the program was reading a new-line character from somewhere before (even when I had taken out all occurences of it written in the code) after the prompt was displayed, the proceeded to display the rest of the entries starting with entry 24 on the same line as the prompt was displayed. This is the expected output which I get with the corrected program with an entry of 28:

    maxwellboltzmannsky@ubuntu:~$ ./square2_v2.01
    ****Square2 v2.01****

    Please enter the number of squares you wish to display:
    52

    1 1
    2 4
    3 9
    4 16
    5 25
    6 36
    7 49
    8 64
    9 81
    10 100
    11 121
    12 144
    13 169
    14 196
    15 225
    16 256
    17 289
    18 324
    19 361
    20 400
    21 441
    22 484
    23 529
    Please press enter:
    24 576
    25 625
    26 676
    27 729
    28 784

    And this is what I got with the code similar to what I posted before (the same error occured):

    maxwellboltzmannsky@ubuntu:~$ ./square2_v2.01
    ****Square2 v2.01****

    Please enter the number of squares you wish to display:
    52

    1 1
    2 4
    3 9
    4 16
    5 25
    6 36
    7 49
    8 64
    9 81
    10 100
    11 121
    12 144
    13 169
    14 196
    15 225
    16 256
    17 289
    18 324
    19 361
    20 400
    21 441
    22 484
    23 529
    Please press enter: 24 576
    25 625
    26 676
    27 729
    28 784

    In the first the program prompted for the user to press enter. In the second, it just printed what is shown without prompting for enter.

    Also, I hope my indentation is a little better.
    Last edited by Codegeek892; 06-20-2012 at 06:25 PM.

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    39
    Sorry the code tags didn't work for the first bit.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Indentation is better. A bit large for my taste, but clear and consistent, which are the most important factors. Looks like the program works pretty well. My only "complaint" would be that you get 23 on the first screenful and 24 numbers for each subsequent screenful. When you want a newline, do something like:
    Code:
    if (i > 1 && i % 24 == 1)
    the > is to skip the prompt on the first screenful, then, == 1 since your loop starts at one and you want every 24 numbers after that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File handling, character check
    By aprop in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2010, 05:52 AM
  2. signal handling and exception handling
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 10:01 PM
  3. Replies: 17
    Last Post: 11-11-2007, 01:30 PM
  4. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM
  5. arry/character handling
    By itld in forum C++ Programming
    Replies: 6
    Last Post: 04-24-2002, 03:16 PM