如何在 C 中使用 getchar 读取单个字符?
How to read a single character with getchar in C?
我正在用 C 语言创建一个菜单驱动程序,我必须在其中请求用户输入,然后在我的程序中仅使用输入的第一个字符。除了下面的代码之外,我还尝试了 #define MAX_CHAR 1
并在 while
循环中使用它代替 EOF
,等等。
#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] = '[=10=]'; //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] != '[=10=]')
{
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 步开始重复(即 "read one character")。
我更改了我的代码,将 getchar()
放在 while 循环之外,并将循环设置为 while(1)
,这确实导致只读取一个字符,但也会创建一个无限循环打印 You entered: B
和菜单。这就是进步,有点。
How to read a single character with getchar
in C?
使用 getchar
.
只能读取一个字符
I have to ask for user input, then use only the first character of the input in my program.
您可以通过阅读整行并仅使用该行的第一个字符来做到这一点。
char line[100]; // Make it large enough.
while( fgets(line, 100, stdin) != NULL )
{
uppercase(line);
char selection = line[0];
switch (selection)
{
...
}
}
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] = '[=11=]'; // Same here
请注意,经过一些 while 循环后,您的 i
变量的值为 12? 34? 1234?,因为你在做之前没有将它初始化为 0 selection[i++] = c
,每次你这样做。
尽管以下代码没有未定义的行为并且允许用户输入最多 128 个字符,但 OP 代码中没有任何内容,此代码也没有使用前(最多)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] = '[=10=]'; //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] != '[=10=]')
// {
// if (string[c] >= 'a' && string[c] <= 'z')
// {
// string[c] = string[c] - 32;
// }
// c++;
// }
//}
我正在用 C 语言创建一个菜单驱动程序,我必须在其中请求用户输入,然后在我的程序中仅使用输入的第一个字符。除了下面的代码之外,我还尝试了 #define MAX_CHAR 1
并在 while
循环中使用它代替 EOF
,等等。
#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] = '[=10=]'; //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] != '[=10=]')
{
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 步开始重复(即 "read one character")。
我更改了我的代码,将 getchar()
放在 while 循环之外,并将循环设置为 while(1)
,这确实导致只读取一个字符,但也会创建一个无限循环打印 You entered: B
和菜单。这就是进步,有点。
How to read a single character with
getchar
in C?
使用 getchar
.
I have to ask for user input, then use only the first character of the input in my program.
您可以通过阅读整行并仅使用该行的第一个字符来做到这一点。
char line[100]; // Make it large enough.
while( fgets(line, 100, stdin) != NULL )
{
uppercase(line);
char selection = line[0];
switch (selection)
{
...
}
}
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] = '[=11=]'; // Same here
请注意,经过一些 while 循环后,您的 i
变量的值为 12? 34? 1234?,因为你在做之前没有将它初始化为 0 selection[i++] = c
,每次你这样做。
尽管以下代码没有未定义的行为并且允许用户输入最多 128 个字符,但 OP 代码中没有任何内容,此代码也没有使用前(最多)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] = '[=10=]'; //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] != '[=10=]')
// {
// if (string[c] >= 'a' && string[c] <= 'z')
// {
// string[c] = string[c] - 32;
// }
// c++;
// }
//}