问题:当输入*和#时,用户需要重新输入名称。最大输入限制为 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);
}
最佳答案
我修改了你的代码。请参阅下面的片段代码
#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);
}
关于c - 名称无法显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31129738/