简体   繁体   English

接受C语言单个字符的菜单

[英]Menu that accepts single character in C language

I want to create a simple menu in C program that accepts single character. 我想在C程序中创建一个接受单个字符的简单菜单。 The menu will be like this: 菜单将如下所示:

  1. [S]how [节目
  2. [E]xit [出口

If the user enter '1','s' or 'S' the program will print "Hello" and prompt again for the input else if the user enters '2',E or 'E' the program ends. 如果用户输入“ 1”,“ s”或“ S”,则程序将打印“ Hello”并再次提示输入,否则,如果用户输入“ 2”,E或“ E”,则程序结束。 else it should print "invalid input" and prompt again. 否则它将打印“无效输入”并再次提示。

I am able to create the program but the problem is that when user enters 12, 13, 14, 15, 16.....so on starting with 1, it shows Hello and same for other options. 我能够创建该程序,但问题是当用户输入12,13,14,15,16 ...时...以1开头时,它显示Hello且其他选项相同。

My code is: 我的代码是:

#include <stdio.h>

void clearBuffer();


int main() {

    int i = 0;
    char selection;

    do
    {
        printf("\t1. [S]how\n");
        printf("\t2. [E]xit\n");
        printf("Enter your selection from the number or character noted above: ");
        scanf("%s", &selection);
        clearBuffer();

        if (selection == '1' || selection == 's' || selection == 'S')
            printf("Hello");
        else if (selection == '2' || selection == 'E' || selection == 'x')
            i = 0;

 } while(i != 0);



}

void clearBuffer()
{   

    while(getchar() != '\n');

}

If you are going to receive only one character consider replacing the scanf() function for getchar() function: 如果只接收一个字符,请考虑将scanf()函数替换为getchar()函数:

printf("Enter your selection from the number or character noted above: "); 
selection = getchar();

You could use strlen , which is part of the standard C library, to check the length of the string returned by scanf and reject entries longer than one character: 您可以使用strlen (这是标准C库的一部分)来检查scanf返回的字符串的长度,并拒绝超过一个字符的条目:

if (strlen(selection) > 1)
{
    printf("Invalid selection.");
}

Alternatively, I think you could use getchar() to accept just a single character from the user, which means they wouldn't have to press enter. 另外,我认为您可以使用getchar()来接受用户的单个字符,这意味着他们不必按Enter键。

As already mentioned, you should use getchar() if you only want one character. 如前所述,如果只需要一个字符,则应使用getchar() If you still want to use scanf() for whatever reason you may have, the correct format is "%c" , not "%s" . 如果出于任何原因仍要使用scanf() ,则正确的格式为"%c" ,而不是"%s"

I would also suggest that if you are looking for a single character, the if block looks a little "busy" (read, awkward) ... a switch would be a cleaner, more elegant way to do it (IMHO). 我还建议,如果您要查找单个字符,则if块看起来有点“忙”(读,笨拙)...开关将是一种更简洁,更优雅的方式(恕我直言)。

/* something like this ... */
switch ( selection ) {
  case '1':
  case 's':
  case 'S':
    printf ( "Hello\n" );
    break;

  case '2':
  case 'e':
  case 'E':
    i = 0;
    break;
}

Other couple of things ... if you don't care about the case of the character being read (that is, 's' and 'S' will do the same thing), you can convert selection to uppercase before your if -block or switch -block using toupper() . 其他几件事...如果您不关心读取字符的大小写(即, 's''S'将执行相同的操作),则可以在if -block之前将selection转换为大写或使用toupper() switch块。 Also, and this is just a style suggestion, don't use i for your exit flag. 另外,这只是样式建议,请勿将i用作退出标志。 General practice is to use things like i and j for counters or indexes - you could use something like quit_now or user_done which would convey more precisely what the variable means. 通常的做法是将ij用作计数器或索引-您可以使用quit_nowuser_done内容来更准确地传达变量的含义。

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

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