简体   繁体   English

c - fflush 或缓冲区清理

[英]c - fflush or buffer clean

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.我正在使用以下代码,当我选择 's' 为例时,内部函数运行,并且我在内部函数中使用 scanf("%d") 和空格键或最后一个之后的输入输入被保存在 main 中的下一个 ch。 I've tried to use fflush after every case but that didnt helped me.我尝试在每个案例之后使用 fflush 但这并没有帮助我。 do you guys have any suggestions ?你们有什么建议吗? sec problem is that the function stops when im inserting e, but doesnt print the BYE BYE message秒问题是当我插入 e 时函数停止,但不打印 BYE BYE 消息

       #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);

}

I've fixed the problem in the printMainOption function the do while fixed it all.我已经修复了 printMainOption 函数中的问题,同时修复了所有问题。

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==' ');

}

tnx for helping everybody! tnx 帮助大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM