我在C中创建一个菜单驱动的程序,我必须要求用户输入,然后在我的程序中只使用输入的第一个字符。除了下面的代码之外,我还尝试了#define MAX_CHAR 1
并在EOF
循环中使用它而不是while
。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void print_menu();
void uppercase(char *string);
int main()
{
char i = 0, c = 0;
char selection[0];
print_menu();
while((c = getchar()) != EOF)
{
selection[i++] = c;
selection[i] = '\0'; //insert NULL at end of string
uppercase(selection);
switch(selection[i])
{
case 'c':
uppercase(selection);
printf("%s\n", selection );
print_menu();
break;
case 'X':
printf("The program is exiting, Schuss!\n" );
exit(0);
default:
printf("\nYou entered: %s\n", selection);
print_menu();
break;
}
}
return 0;
}
void print_menu() //gives code for menu
{
printf("Select a menu item and hit enter: \n" );
printf("C) Convert the string to uppercase\n");
printf("M) Display this menu\n");
printf("X) Exit the program\n");
}
void uppercase(char *string)
{
int c = 0;
while (string[c] != '\0')
{
if (string[c] >= 'a' && string[c] <= 'z')
{
string[c] = string[c] - 32;
}
c++;
}
}
如果我在运行程序时键入Yes!
,我希望输出为
You entered: Y
然后打印菜单。
目前输出是
You entered: Y
Select a menu item and hit enter:
C) Convert the string to uppercase
M) Display this menu
X) Exit the program
You entered: YE
Select a menu item and hit enter:
C) Convert the string to uppercase
M) Display this menu
X) Exit the program
You entered: S
Select a menu item and hit enter:
C) Convert the string to uppercase
M) Display this menu
X) Exit the program
You entered: S!
Select a menu item and hit enter:
C) Convert the string to uppercase
M) Display this menu
X) Exit the program
You entered:
Select a menu item and hit enter:
C) Convert the string to uppercase
M) Display this menu
X) Exit the program
^C
我很确定这是while
循环的问题,但还没弄清楚如何修复它。最终我会有更多的菜单项和案例。例如,用户将输入A
并打印a
然后再次打印菜单以等待下一个菜单选择,直到用户输入“X”退出。另外,我知道uppercase
有两个函数调用,这是故意的。
编辑:我希望getchar()
能够按照设计的目的阅读一个字符。然后做任何符合该角色的案例。然后再次打印菜单(这部分包含在每个case语句中)。然后从步骤1重复(这是“读取一个字符”)。
我已将我的代码更改为将getchar()
置于while循环之外,并将循环设置为while(1)
,而不会导致只读取一个字符,但也会创建无限循环打印{{1}和菜单。那是进步,有点。
答案 0 :(得分:1)
如何在C中阅读
getchar
的单个字符?
您只能使用getchar
阅读单个字符。
我必须要求用户输入,然后只使用我程序中输入的第一个字符。
您可以通过阅读整行并仅使用该行的第一个字符来完成此操作。
char line[100]; // Make it large enough.
while( fgets(line, 100, stdin) != NULL )
{
uppercase(line);
char selection = line[0];
switch (selection)
{
...
}
}
答案 1 :(得分:0)
如何在C中用getchar读取单个字符?
就这样:
getchar();
顺便说一下,你在这里覆盖了一些内存:
char i = 0, c = 0;
char selection[0]; // What?! An array of 0 elements?!
...
selection[i++] = c; // Where are you storing the value?
selection[i] = '\0'; // Same here
请注意,经过一段时间的循环,您的i
变量的值为12? 34? 1234?,因为每次执行selection[i++] = c
之前都没有将它初始化为0。
答案 2 :(得分:0)
即使以下代码没有未定义的行为并且允许用户输入最多128个字符,OPs代码中也没有任何内容,此代码也没有使用第一个(最多)4个字符
#include <stdio.h> // getchar(), prinf()
//#include <string.h>
//#include <stdlib.h> // exit(), EXIT_SUCCESS
#include <ctype.h> // toupper()
void print_menu( void );
//void uppercase( char *string );
#define MAX_CHARS (128)
int main( void )
{
size_t i = 0;
int c; // getchar() returns an int, not a char
//char selection[0];
char selection[ MAX_CHARS+1 ]; // +1 to allow for NUL terminator byte
print_menu();
while( i < MAX_CHARS && (c = getchar()) != EOF && 'X' != toupper(c) )
{
if( '\n' == c ) // ignore any newlines
continue;
selection[i++] = (char)toupper(c);
selection[i] = '\0'; //insert NUL byte at end of string
//uppercase(selection);
switch(selection[i-1]) // -1 because 'i' is the termination byte
{
//case 'c':
// //uppercase(selection);
// printf("%s\n", selection );
// print_menu();
// break;
//case 'X':
// printf("The program is exiting, Schuss!\n" );
// //exit(0);
// exit( EXIT_SUCCESS );
// break;
default:
printf("\nYou entered: %s\n", selection);
print_menu();
break;
}
}
printf("The program is exiting, Schuss!\n" );
//return 0; // not needed for 'main()' in modern C
} // end function: main
void print_menu() //gives code for menu
{
// note: C concatenates successive literal strings
printf("Select a menu item and hit enter: \n"
"X) Exit the program\n"
"any other key appends to input, except newline\n");
} // end function: print_menu
//void uppercase(char *string)
//{
// int c = 0;
// while (string[c] != '\0')
// {
// if (string[c] >= 'a' && string[c] <= 'z')
// {
// string[c] = string[c] - 32;
// }
// c++;
// }
//}