C - 选择没有被选中

时间:2015-12-15 23:58:16

标签: c

        write(sd, "\nWelcome!\n", 51);
        write(sd, "Please enter one of the following options:\n\n", 45);
        write(sd, "1 - Do this\n", 28);
        write(sd, "2 - Do that\n", 26);
        write(sd, "3 - Do the other\n", 27);
        fflush(stdout);

        recv(sd, &choice, 1, 0);

        switch (choice) {
        case 49:
        fp = popen("/bin/df", "r");
        if (fp == NULL) {
            printf("Failed to run command\n");
            exit(1);
        }
        while (fgets(path, sizeof(path), fp) != NULL) {
            sgnet_writes(sd, path);

        }
        break;
        default:
        write(sd, "Invalid choice!\n", 17);
        break;

所以当我输入49时,我收到无效的选择信息。知道如何或至少如何到达DO SOME STUFF部分?

如果我输入以1开头的任何数字,它会选择第一个选项,任何以2开头的数字,第二个,任何以3开头的数字,它是第3个选项。任何以4或以上开头的数字都会失败

由于

3 个答案:

答案 0 :(得分:2)

始终记得case switchcase 49: { DO SOME STUFF; } break; 声明:

$(document).ready(function(){
   $(window).on('resize' , function(){
            resize('#div1' , '#div2');
   });

});

function resize(el1 , el2){
    var $window = $(window);
    if($window.width() <= 400){
         $(el2).insertBefore(el1);
    }else{
         $(el2).insertAfter(el1);
    }
}

答案 1 :(得分:0)

就像artm所说,只需添加休息时间:

    write(sd, "\nWelcome!\n", 51);
    write(sd, "Please enter one of the following options:\n\n", 45);
    write(sd, "1 - Do this\n", 28);
    write(sd, "2 - Do that\n", 26);
    write(sd, "3 - Do the other\n", 27);
    fflush(stdout);

    recv(sd, &choice, 1, 0);

    switch (choice) {
    case 49:
        DO SOME STUFF
        break;
    default:
    write(sd, "Invalid choice!\n", 17);
    break;

答案 2 :(得分:0)

choiceint类型的变量。最常见的平台上int为4或8个字节。 read将1个字节int放入read会产生不可预测的结果,因此您不应该这样做。

此外,角色&#39; 1&#39;与数字1不同。事实上,角色&#39; 1&#39;值为49(同样,在最常见的平台上)。并且1读取字符而不是数字 - 如果键入position: absolute,它读取的第一个字节将具有值49。