user19851608
user19851608

Reputation:

If statement not properly executing

I'm new to C and was doing a C programming exercise. The exercise's objective was to print out a table of squares. The catch is that after every 24 squares, the program pauses and displays the message:

Press Enter to continue

The program should use a getchar function to read a character and only continue when the user presses the Enter key.

I've implemented the following code

#include <stdio.h>

int main(void) {
    int i, n, mark;
    char ch;

    printf("This program prints a table of squares.\n");
    printf("Enter a number of entries in table: ");
    scanf("%d", &n);

    for (i = 1, mark = 1; i <= n; ++i, ++mark) {
        if (mark > 24) {
            printf("Press Enter to continue...");
            while (getchar() != '\n')
                ;

            mark = 1;
        }

        printf("%10d%10d\n", i, i * i);
    }

    return 0;
}

The problem is that when the program enters the if statement for the first time, it completely misses everything except the printf statement, continuing the loop. Not even asking for input. Whenever it enters the if statement from then on, it also executes the rest of the statements (i.e. it pauses on every 24th iteration and asks for input).

Here's the output I'm getting:

This program prints a table of squares.
Enter a number of entries in table: 100
         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
        24       576
Press Enter to continue...        25       625
        26       676
        27       729
        28       784
        29       841
        30       900
        31       961
        32      1024
        33      1089
        34      1156
        35      1225
        36      1296
        37      1369
        38      1444
        39      1521
        40      1600
        41      1681
        42      1764
        43      1849
        44      1936
        45      2025
        46      2116
        47      2209
        48      2304
Press Enter to continue...aw
        49      2401
        50      2500
        51      2601
        52      2704
        53      2809
        54      2916
        55      3025
        56      3136
        57      3249
        58      3364
        59      3481
        60      3600
        61      3721
        62      3844
        63      3969
        64      4096
        65      4225
        66      4356
        67      4489
        68      4624
        69      4761
        70      4900
        71      5041
        72      5184
Press Enter to continue...dwa
        73      5329
        74      5476
        75      5625
        76      5776
        77      5929
        78      6084
        79      6241
        80      6400
        81      6561
        82      6724
        83      6889
        84      7056
        85      7225
        86      7396
        87      7569
        88      7744
        89      7921
        90      8100
        91      8281
        92      8464
        93      8649
        94      8836
        95      9025
        96      9216
Press Enter to continue...f
        97      9409
        98      9604
        99      9801
       100     10000

I feel like I'm doing something obviously wrong.

Upvotes: 0

Views: 47

Answers (1)

user19851608
user19851608

Reputation:

The scanf being used is leaving behind a \n in the standard input. So when the getchar() statement is executed, it fetches the \n character left behind by scanf, resulting in the while loop condition to be false and continuing the execution of the program without pausing. A workaround would be to use a getchar() to catch the rogue \n, or as @Cheatah suggested, to use fgets to take a line of input and parsing it afterwards.

Upvotes: 1

Related Questions