名称无法显示
Name cannot be display
问题:当输入*和#时,用户需要重新输入姓名。最大输入限制为 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);
}
scanf("%s", name[j]);
写错了,写
name[j] = input;
相反。给定格式字符串,scanf 将读取整个字符串,
但是您的程序显然需要按字符读取。
你还需要
#include <stdio.h>
开头。
哦,还有
name[j] < 25
应该是
j < MAX
更多错误修复:
char name[MAX+1]; /* one more for the terminating 0 */
name[j]=0; /* instead of NULL */
我已经修改了你的代码。请看下面的代码片段
#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;
}
name[j] = input;
j++;
} while ( j < MAX && input != '\r' );
name[j] = 0;
puts("\nYour name is");
puts(name);
}
你的程序由于各种原因出错,比如
scanf("%s", name[j]);
、%s
需要指向 char
数组的指针。
name[j] = NULL;
,NULL是指针类型
等等
相反,我建议您使用更简洁的算法。看看你喜不喜欢
- 使用
fgets()
读取用户输入。
- 使用
strpbrk()
. 搜索是否存在 *
或 #
或任何其他(如您所愿)
- 如果
strpbrk()
返回NULL,这意味着您可以进一步使用输入。 restricted chars
不存在。
- 否则,请重新输入。
也就是说,void main(void)
不被视为 main()
的推荐签名,您应该改用 int main(void)
。
问题:当输入*和#时,用户需要重新输入姓名。最大输入限制为 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);
}
scanf("%s", name[j]);
写错了,写
name[j] = input;
相反。给定格式字符串,scanf 将读取整个字符串, 但是您的程序显然需要按字符读取。
你还需要
#include <stdio.h>
开头。
哦,还有
name[j] < 25
应该是
j < MAX
更多错误修复:
char name[MAX+1]; /* one more for the terminating 0 */
name[j]=0; /* instead of NULL */
我已经修改了你的代码。请看下面的代码片段
#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;
}
name[j] = input;
j++;
} while ( j < MAX && input != '\r' );
name[j] = 0;
puts("\nYour name is");
puts(name);
}
你的程序由于各种原因出错,比如
scanf("%s", name[j]);
、%s
需要指向char
数组的指针。name[j] = NULL;
,NULL是指针类型
等等
相反,我建议您使用更简洁的算法。看看你喜不喜欢
- 使用
fgets()
读取用户输入。 - 使用
strpbrk()
. 搜索是否存在 - 如果
strpbrk()
返回NULL,这意味着您可以进一步使用输入。 restrictedchars
不存在。 - 否则,请重新输入。
*
或 #
或任何其他(如您所愿)
也就是说,void main(void)
不被视为 main()
的推荐签名,您应该改用 int main(void)
。