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...

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.