gpt4 book ai didi

c - 名称无法显示

转载 作者:行者123 更新时间:2023-11-30 21:39:36 25 4
gpt4 key购买 nike

问题:当输入*#时,用户需要重新输入名称。最大输入限制为 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);
}

enter image description here

关于c - 名称无法显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31129738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com