0

I'm using the following code, and when I'm picking 's' for an example, the inner function runs and Im using scanf("%d") in the inner function and the spacebar or the enter that comes after the last input is saved for the next ch in the main. I've tried to use fflush after every case but that didnt helped me. do you guys have any suggestions ? sec problem is that the function stops when im inserting e, but doesnt print the BYE BYE message

       #include "exe.h"
        #include <time.h>
        #include <stdlib.h>
        #include <stdio.h>
        
        int  main()
        {
            srand(time(NULL));
        //TODO: change the options
            char ch;
            do 
            {
                printMainOption(&ch);
                
                switch (ch)
                {
                    case 's':
                        q_subMat();
                        fflush(stdin);
                    break;
                         
                    case 'c':
            //funcC
                    break;
            
                    case 'b':
            //funcB
                    break;
            
                    case 'e':
  scanf("Bye Bye");
            fflush(stdin);
                    break;
            
                    default:
                        printf("The operator doesn't match any constant\n");
                
            }
        }while(ch!='e' || ch!='E');




void printMainOption(char *ch)
{
    printf("\n\nPlease choose one of the following options\n");
    printf("S/s - Biggest Matrix Sum\n");
    printf("C/c - Color Game\n");
    printf("B/b - Black And White Game\n");
    printf("E/e - Quit\n");
    scanf(" %c",ch);
    *ch=tolower(*ch);

}
  • What's in `printMainOption`? I'm going to guess `scanf("%c", ch);`. Put a space before the `%c`, so `scanf(" %c", ch);` so it will skip leading whitespace. – Retired Ninja Nov 05 '21 at 08:49
  • Does this answer your question? [scanf() leaves the new line char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer) – Retired Ninja Nov 05 '21 at 08:49
  • Could u share the function `printMainOption`?? Another comment, we can see that the variable `ch` is a `char` but you use `%d` to get the variable? Try to use `%c`. One more option after change those things, include a space on the `scanf` function like this `scanf(" %c", &ch);` – Londo Nov 05 '21 at 08:50
  • included the printOption func in the question. – יונתן אליהו Nov 05 '21 at 09:06
  • You shouldn't use `fflush` on read-only `FILE`, explanation [here](https://stackoverflow.com/questions/2979209/using-fflushstdin). – WENDYN Nov 05 '21 at 09:28

1 Answers1

1

I've fixed the problem in the printMainOption function.

The do ... while fixed it all:

void printMainOption(char *ch) {
    printf("\n\nPlease choose one of the following options\n");
    printf("S/s - Biggest Matrix Sum\n");
    printf("C/c - Color Game\n");
    printf("B/b - Black And White Game\n");
    printf("E/e - Quit\n");
    do {
        scanf(" %c",ch);
        *ch=tolower(*ch);
    } while(*ch=='\n' || *ch==' ');
}
Antoine
  • 1,393
  • 4
  • 20
  • 26