-1

I am writing a flexible command-line (but not for long!) diamond-square generator in C++. I have just finished writing the user input half. However, on the very last command, input "slips" and a newline is automatically inputted to getchar(). I have taken precautions to ensure that it's not any sort of overflow, namely, fflushing both stdin, and, for good measure, stdout. The problem persists. Here is my code:

#include <stdio.h>
#include <stdlib.h>

int main () {
    unsigned  long seed = 0, x = 0, y = 0, initial = 0, range = 0;
    int smooth = 0, fail = 1;
    char flagchar1 = 'n';
    printf("Welcome to my diamond-square generator! This isn't full-feature yet, so I'm just gonna have you input the variables one by one. ");
    do {
        printf("Please input the seed (this is a positive integer):\n");
        fail = scanf("%lu", &seed);
        while (fail == 0) {
            printf("Try again, smartass.\n");
            fail = scanf("%lu", &seed);
        }
        fail = 1;
        printf("Now input the x, or horizontal, size of your grid:\n");
        fail = scanf("%lu", &x);
        while (fail == 0) {
            printf("An integer. Not a string. An integer. You can do that, can't you?\n");
            fail = scanf("%lu", &x);
        }
        fail = 1;
        printf("Now input the y, or vertical, size of your grid:\n");
        fail = scanf("%lu", &y);
        while (fail == 0) {
            printf("What was that supposed to be? An integer, please.\n");
            fail = scanf("%lu", &y);
        }
        fail = 1;
        printf("Now input about how high you'd like the grid to be (this goes from a scale of 1 to 256):\n");
        fail = scanf("%lu", &initial);
        while (initial == 0 || initial > 256 || fail == 0) {
            printf("ahahahahaha how HIGH do you have to be just to HAVE that hieght........\n");
            fail = scanf("%lu", &initial);
        }
        fail = 1;
        printf("Now input the range of the heights on your grid (this must be equal to or less than 256):\n");
        scanf("%lu", &range);
        while (range >= 256 || fail == 0) {
            printf("What did I say about being equal to or less than 256? Give me something reasonable to work with here.\n");
            fail = scanf("%lu", &range);
        }
        fail = 1;
        printf("Just one more variable to go! Now, I need you to input the smoothness of your grid. Smaller numbers make spikier grids. You can make this negative, but beware!\n");
        fail = scanf("%d", &smooth);
        while (fail == 0) {
            printf("That... was not a number.\n");
            fail = scanf("%d", &smooth);
        }
        fail = 1;
        printf("\nOkay. Are these the values you want?\n   Seed:       %lu\n   Width:      %lu\n   Length:     %lu\n   Height:     %lu\n   Range:      %lu\n   Smoothness: %d\nDo you want to keep these? Type Y/n.\n", seed, x, y, initial, range, smooth);
        fflush(stdin);
        fflush(stdout);
        flagchar1 = getchar();
    } while (flagchar1 != 'y' && flagchar1 != 'Y' && flagchar1 != '\n');
}

Here is my output, the program having ended (the program just repeats the entire do-while loop if I remove the && flagchar1 != '\n' from while()):

    Welcome to my diamond-square generator! This isn't full-feature yet, so I'm just gonna have you input the variables one by one. Please input the seed (this is a positive integer):
    12345678
    Now input the x, or horizontal, size of your grid:
    40
    Now input the y, or vertical, size of your grid:
    30
    Now input about how high you'd like the grid to be (this goes from a scale of 1 to 256):
    1288
    ahahahahaha how HIGH do you have to be just to HAVE that hieght........
    128
    Now input the range of the heights on your grid (this must be equal to or less than 256):
    30
    Just one more variable to go! Now, I need you to input the smoothness of your grid. Smaller numbers make spikier grids. You can make this negative, but beware!
    10

Okay. Are these the values you want?
  Seed:       12345678
  Width:      40
  Length:     30
  Height:     128
  Range:      30
  Smoothness: 10
Do you want to keep these? Type Y/n.

What's happening, and how do I fix it?

P.S. I know my input validation is essentially useless. Help with this is also greatly appreciated.

Robᵩ
  • 143,876
  • 16
  • 205
  • 276
mszegedy
  • 175
  • 1
  • 8
  • I'm not sure I'm clear on your question. What is the behavior you expect? – Carey Gregory Mar 05 '12 at 19:29
  • If I type `y`, `Y`, or a newline, it exits the loop. If I type anything else, it restarts it. But currently it types a newline for me regardless of what I WANT to input, which exits the loop automatically. – mszegedy Mar 05 '12 at 19:31
  • Your call to `fflush(stdin)` invokes behavior not defined by the standard. It may be defined by your compiler vendor. What compiler and operating system are you running? – Robᵩ Mar 05 '12 at 20:02
  • Not enough debugging or problem decomposition – Lightness Races in Orbit Mar 05 '12 at 20:40

5 Answers5

2

Make the end of your loop look like this:

    // Ignore remaining characters on current line.
    int ch;
    while( (ch = getchar()) != EOF && ch != '\n')
      ;
    // fetch first character on next line
    flagchar1 = getchar();
} while (flagchar1 != 'y' && flagchar1 != 'Y' && flagchar1 != '\n');

You are leaving the '\n' in stdin after your last call to scanf.

You must not rely upon fflush(stdin) having any specific behavior. The result of invoking fflush on a input stream is undefined. See Using fflush(stdin)

Community
  • 1
  • 1
Robᵩ
  • 143,876
  • 16
  • 205
  • 276
  • No, he's not. He flushes stdin before calling getchar(). – Carey Gregory Mar 05 '12 at 19:55
  • At least on Ubuntu 10.4, the `fflush` is ineffective. See http://stackoverflow.com/questions/2979209/using-fflushstdin – Robᵩ Mar 05 '12 at 19:56
  • Perhaps, and I agree that's not the best way to handle it, but that's not the issue. See the comments to my answer. – Carey Gregory Mar 05 '12 at 20:04
  • 1
    Maybe the fflush(stdin) is, in fact, the problem. With his clarifications, it seems getchar() is behaving strangely, and flusing stdin might be the cause of that. I've edited my answer to reflect this and upvoted your answer. – Carey Gregory Mar 05 '12 at 20:13
0

The code is behaving exactly as you tell it to. If the user enters 'y', 'Y' or enter, one of those conditions in the while loop will be false, which causes it to exit.

What you want is:

while (flagchar1 == 'y' || flagchar1 == 'Y' || flagchar1 == '\n');

Edit: I would also delete the fflush(stdin) and replace getchar() with fgets(). That will guarantee the entire line is read without having to use fflush, which may be the issue.

Carey Gregory
  • 6,685
  • 2
  • 23
  • 45
  • No, I mean, I don't even input a newline. It doesn't give me a chance to input. It's supposed to be, it exits the loop if I pass it a `y`, `Y`, or newline. But I don't type ANYTHING, and it exits the loop all the same. (Sorry, I wasn't clear enough in my other comment. I meant that I WANTED it to do that.) – mszegedy Mar 05 '12 at 19:56
  • Well, that's interesting because your code works for me if I make the change I described above. How are you compiling and linking this? And with what tools? – Carey Gregory Mar 05 '12 at 20:03
  • -1: No, he has the sense of the loop correct. The question presented to the user is: "Are these values okay?" A response of 'y' is intended to exit the loop. Any other response is intended to repeat the loop. – Robᵩ Mar 05 '12 at 20:05
  • Rewording his prompt would seem to be problem #2. From his description above, he's not getting a chance to enter anything. – Carey Gregory Mar 05 '12 at 20:10
0

I'm guessing you are on Linux? This works fine on VS in Windows. It prompts, reads from keyboard, and if examined contains the correct 'y' or 'Y' in question.

I might suggest you try changing the last scanf to:

fail = scanf("%d ", &smooth);

You could also try calling fpurge() instead of fflush(), but that's non standard, and I think the space at the end of the format string will get you what you want.

The trailing space will ask scanf to consume any extra whitespace (including newlines) in the input data. fflush() probably won't do what you want for input.

I suspect whatever system you are on is indeed leaving the carriage return in the stream and if you print flagchar1 as an int you'll get 10?

Joe
  • 2,859
  • 14
  • 15
0

Suggestions:

  1. Use C++ streams.
  2. Use tolower or toupper before comparing characters.
  3. Use std::string.

The C language, which uses fgets, gets, fflush, strcmp, has many issues in this area. The C++ language has resolved many of these issues in the std::stream classes.

Since you are not using C++ features, you should change the C++ tag to C.

Thomas Matthews
  • 52,985
  • 12
  • 85
  • 144
-1

Try

} while (flagchar1 != 'y' || flagchar1 != 'Y' || flagchar1 != '\n');

instead of

} while (flagchar1 != 'y' && flagchar1 != 'Y' && flagchar1 != '\n');

TimKouters
  • 26
  • 2