【发布时间】:2015-09-16 17:54:32
【问题描述】:
问题:当输入的是*和#时,用户需要重新输入名字。最大输入限制为 25 个字符,并以 enter 键结束输入。
#include <stdio.h>
#include <conio.h>
#define MAX 25
char welcomeMsg[]= "Please enter your name without '*' or # " ;
char errorMsg[]= "\nError, please re-type your name. ";
void main(void)
{
int j;
char input;
char name[MAX];
j=0;
puts(welcomeMsg);
do
{
input = getche();
if(input == '*' || input =='#' )
{
puts(errorMsg);
j = 0;
continue;
}
scanf("%s", name[j]);
j++;
} while ( j < MAX && input != '\r' );
name[j] = NULL;
puts("\nYour name is");
puts(name);
}
【问题讨论】:
-
有什么问题?
-
您不必在字符串文字中显式添加
\0。 -
main声明错误!应该是int main(int argc, char**argv)。并且getche没有定义。您应该测试scanf(扫描项目数)的结果 -
当我在程序中输入名称时程序会崩溃。
-
所以你有一个错误。编译所有警告和调试信息 (
gcc -Wall -Wextra -g) 并使用调试器 (gdb);然后思考找出错误
标签: c char user-input scanf