创建提示用户输入单个字符的功能。函数的返回值为char
,并将返回用户输入的字符值。他的返回值将存储在main()
中的局部变量C中。此字符的初始默认值为''。问题是如果局部变量C不是全局变量,我将如何将其存储在main中,并且我们不能将其设置为全局变量。我必须使用该C变量来完成我的其他功能。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
char enterSingleChar();
int main()
{
char userChoice;
int N = 0;
char C = ' ';
printf("Please choose one of the following choices below \n");
printf("Enter/Change Character (C/c)\n");
printf("Quit Program (Q/q) \n");
scanf("%c", &userChoice);
switch(userChoice)
{
case 'C':
case 'c':
enterSingleChar();
break;
case 'Q':
case 'q':
printf("The program will now quit\n");
exit(1);
default:
break;
}
}
char enterSingleChar()
{
char singleChar = ' ';
printf("Please enter a single character \n");
scanf(" %c", &singleChar);
return singleChar;
}
答案 0 :(得分:1)
switch(userChoice)
{
case 'C':
case 'c':
C=enterSingleChar();
break;
case 'Q':
case 'q':
printf("The program will now quit\n");
exit(1);
default:
break;
}