Scanf - 用户输入必须输入的两倍或更多倍

时间:2015-07-07 09:05:02

标签: c scanf

我写了一个C应用程序包含switch语句。当我尝试运行应用程序时,它会提示选项,有时当我试图输入一个选项时系统没有响应,只有在我被迫2次或更多次之后用enter输入相同的选项。 我的代码有问题吗? 我正在通过终端在我的开发板上执行应用程序。 请参阅下面的我的代码段

#include "TestApp.h"

int main(void) {
BTON = "ON";    // To hold Bluetooth status as ON
BTOFF = "OFF";  // To hold Bluetooth status as OFF
int mainChoice, btChoice, zigChoice; // To select choices
char key, keyMain, c; // To handle continuity of the program
int flag = 0; // To handle continuity of the program
btstatus = "OFF";
puts("!!! Test App!!!\n");
do {

    printf("\nMain  Menu: Enter your choice\n"
            "1.Bluetooth Test\n"
            "2.Zigbee Test\n"
            "3.Brightness Test\n");
    scanf("%d", &mainChoice);
    switch (mainChoice) {

    case 1:
        flag = 1;

        do {

            printf("Enter your choice for Bluetooth :\n"
                    "1. Turn on bluetooth\n"
                    "2. Turn on visibility\n"
                    "3. Set passkey\n"
                    "4. Scan bluetooth device\n"
                    "5. Display bluetooth configuration\n"
                    "6. Reset Passkey\n"
                    "7. Turn off visibility\n"
                    "8. Turn off bluetooth\n");
            scanf("%d", &btChoice);
            bluetoothTest(btChoice);

            do {
                printf(
                    "Do u want to continue with bluetooth test? (y/n) \n");               
                while (((c = getchar()) != '\n') && (c != EOF))
                    ;
                key = getchar();

                if (key != 'y' && key != 'n') {
                    printf("Invalid choice\n");
                }
            } while (key != 'y' && key != 'n');
        } while (key == 'y');
        break;

    case 2:
        flag = 1;

        do {

            printf("Enter your choice for Zigbee :\n"
                                    "1. To Enable Zigbee Module\n"
                                    "2. Display Firmware version\n"
                                    "3. Display network information\n"
                                    "4. Disassociate from PAN\n"
                                    "5. Establish PAN\n"
                                    "6. Scan for PAN\n"
                                    "7. Join network\n"
                                    "8. Scan Network \n"
                                    "9. Broadcast message\n");
            scanf("%d", &zigChoice);
            zigbeeTest(zigChoice);

            do {
                printf("Do u want to continue with zigbee test? (y/n) \n");
                while (((c = getchar()) != '\n') && (c != EOF))
                    ;
                key = getchar();

                if (key != 'y' && key != 'n') {
                    printf("Invalid choice\n");
                }
            } while (key != 'y' && key != 'n');
        } while (key == 'y');
        break;
    case 3:
        flag = 1;
        do {
            brightnessTest();

            do {
                printf(
                        "Do u want to continue with brightness test? (y/n) \n");
                while (((c = getchar()) != '\n') && (c != EOF))
                    ;
                key = getchar();
                if (key != 'y' && key != 'n') {
                    printf("Invalid choice\n");
                }
            } while (key != 'y' && key != 'n');
        } while (key == 'y');
        break;

    default:
        printf("\n Invalid choice\n");
        break;
    }
    do {
        printf("Do u want to continue  Test App? (y/n) \n");
        if (flag == 1) {
            while (((c = getchar()) != '\n') && (c != EOF))
                ;
        }

        keyMain = getchar();
        if (keyMain != 'y' && keyMain != 'n') {
            printf("Invalid choice\n");
        }

    } while (keyMain != 'y' && keyMain != 'n');

    if (keyMain == 'n') {
        break;
    }
} while (keyMain == 'y');
return EXIT_SUCCESS;
 }

void bluetoothTest(int option) {

switch (option) {
case 1:
    turnBluetooth(BTON);

    break;
case 2:
    setVisibility(BTON);

    break;
case 3:
    setPasskey();
    break;
case 4:
    scanBTDevice();
    break;
case 5:
    displayBTConfiguration();
    break;
case 6:
    resetPasskey();
    break;
case 7:
    setVisibility(BTOFF);
    break;
case 8:
    turnBluetooth(BTOFF);
    break;
default:
    printf("\n Invalid choice\n");
    break;

}

 }
 void zigbeeTest(int option) {
  switch (option) {
    case 1:
        printf("\n Zigbee Module Enabled!!!! \n");
        zigbeeEnable();

        break;
    case 2:
        displayFirmwareVersion();

        break;
    case 3:
        displayNetworkInfo();

        break;
    case 4:
        disAssociateFromPan();

        break;
    case 5:
        establishPan();

        break;
    case 6:
        scanForPan();

        break;
    case 7:
        joinNetwork();

        break;
    case 8:
        scanNetwork();

        break;
    case 9:
        broadcastMessage();

        break;

    default:
        printf("\n Invalid choice\n");
        break;

}

}

 void brightnessTest() {
float brightval;
printf("Enter brightness value from 0 to 10  :\n  ");
scanf("%f", &brightval);
setBrightness(brightval);
}

0 个答案:

没有答案