Out of all the possible problems I though I may have in my program, this was not one of them.

My menu code is looping twice before stopping for the fgets to take input.. so my menu options are getting printed twice? I have tried flushing the crap out of the stdin, and so many other things.. but it still steps through the same.

So how do I get it to stop this double looping?

Here is a snippet..

void MainMenu()
{
    char selection = '\0';
    do
   {
        printf("Bla Bla.. ");
        printf("Please make your selection (1, 2, 3 or 4): ");
        selection = GetAChar();
        switch(selection)
        {
             ...some menu code...
        }
    }while(selection != '4');
    return;
}

char GetAChar()
{
    char buffer[CHAR_INPUT]; //CHAR_INPUT is 2
    char firstChar = '\0';

    (fflush(stdin)); //flush any crap out of stdin
    fgets(buffer, CHAR_INPUT, stdin); //readline into string
    sscanf(buffer, "%c", &firstChar); // get the first char
    return(firstChar);
}

Thanks in advance guys...

Dani AI

Generated

Short diagnosis
The “double menu” happens when leftover characters (typically the newline or other trailing input left by a prior scanf) are still in stdin when the menu reads input. already tracked the cause to a previous scanf and correctly suggested consuming the remainder of the line. The most robust approach is to treat input as lines: read a whole line, trim/inspect it, and only then parse the first meaningful character. Avoid fflush(stdin) and avoid mixing line-oriented reads and formatted reads without handling the leftover characters.

A compact, robust pattern (reads one line, discards any overflow, returns first non-space char):

#include <stdio.h>
#include <string.h>
#include <ctype.h>

char get_menu_choice(void) {
    char buf[64];
    if (fgets(buf, sizeof buf, stdin) == NULL) return '\0'; /* EOF */
    if (strchr(buf, '\n') == NULL) { /* input longer than buffer, discard rest */
        int c;
        while ((c = fgetc(stdin)) != '\n' && c != EOF) {}
    }
    for (size_t i = 0; buf[i] != '\0'; ++i) {
        if (!isspace((unsigned char)buf[i])) return buf[i];
    }
    return '\0';
}

Quick troubleshooting checklist

  • If scanf was used earlier, check its return value and be aware it may leave the newline.
  • Use scanf(" %c", &ch) (leading space) if a single non-whitespace char is required and no mixing with fgets occurs.
  • Prefer a single input method: either always use fgets/getline + parsing (recommended) or carefully use scanf formats that consume separators.
  • To debug leftovers, print the numeric values of stdin bytes (e.g., log each char with its int value) to see what remains.

Final note
’s consume-the-rest idea is the right direction; standardizing on line-based reads (the snippet above) will make menus predictable and portable across platforms.

Recommended Answers

All 3 Replies

ok my bad I found out there was trailing crap from a previous scanf that was bunging things up.. and that flush is useless.... but I may have more questions.. so I want mark te thread as solved yet.

ok my bad I found out there was trailing crap from a previous scanf that was bunging things up.. and that flush is useless.... but I may have more questions.. so I want mark te thread as solved yet.

You're making this more difficult than it needs to be and fflush(stdin) is undefined behaviour according to the standard (although some compilers probably support it).

Take a look at the code snippet below (minus the GetAChar() function):

#include <stdio.h>

void MainMenu() {
    char selection = '\0';
    do {
        printf("Bla Bla.. ");
        printf("Please make your selection (1, 2, 3 or 4): ");
        //selection = GetAChar();
        selection = getchar();
        switch (selection) {
            case '1':
            case '2':
            case '3':
                printf("You selected %c\n", selection);
                break;
            default:
                printf("Whatever!\n");
                break;
        }
        // consume any additional chars entered up to and including
        // the newline character
        while (getchar() != '\n');

    } while (selection != '4');

    return;
}

int main(void) {

    MainMenu();
    return 0;
}

Try the code out and see if you can incorporate aspects of it into your project.

You're making this more difficult than it needs to be and fflush(stdin) is undefined behaviour according to the standard (although some compilers probably support it).

Take a look at the code snippet below (minus the GetAChar() function):

#include <stdio.h>

void MainMenu() {
    char selection = '\0';
    do {
        printf("Bla Bla.. ");
        printf("Please make your selection (1, 2, 3 or 4): ");
        //selection = GetAChar();
        selection = getchar();
        switch (selection) {
            case '1':
            case '2':
            case '3':
                printf("You selected %c\n", selection);
                break;
            default:
                printf("Whatever!\n");
                break;
        }
        // consume any additional chars entered up to and including
        // the newline character
        while (getchar() != '\n');

    } while (selection != '4');

    return;
}

int main(void) {

    MainMenu();
    return 0;
}

Try the code out and see if you can incorporate aspects of it into your project.

while (getchar() != '\n');

That is the line of code I could have done with this afternoon.. as usual snow you come through.... thanks for the input.

Nate

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.