任何人都可以帮助我编写代码
void gotoxy(int, int);
void clrscr();
void dispMenu();
int main(void){
int choice;
choice = 0;
menu:
dispMenu();
scanf("%d", &choice);
if(choice==1){
clrscr();
char name[100];
printf("Please Input your Complete name: ");
scanf("%[^\n]s", &name);
printf("Your name is: %s \n", name);
}
getch();
goto menu;
}
void dispMenu(){
gotoxy(23,9);
printf("List of C-Lang Activities\n");
gotoxy(23,11);
printf("1. Input Name");
gotoxy(23,12);
printf("2. (blank) \n");
gotoxy(23,13);
printf("3. (blank) \n");
gotoxy(23,14);
printf("4. (blank)\n");
gotoxy(23,15);
printf("5. (blank)\n");
gotoxy(23,17);
printf("Please Enter the Number of your choice: ");
}
void gotoxy(int x, int y){
HANDLE hConsoleOutput;
COORD dxCursorPosition;
dxCursorPosition.X = x;
dxCursorPosition.Y = y;
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput, dxCursorPosition);
}
void clrscr(){
system("cls");
}
当我尝试将程序放入表格菜单时遇到问题,如下所示:
输出将是这样的
Please Input your Complete Name: John Kenneth
Your Name is: John Kenneth
最佳答案
使用标准函数fgets
。例如
fgets( name, sizeof( name ), stdin );
name[strcspn( name, "\n" )] = '\0';
请考虑到使用 goto 语句不是一个好主意。您应该忘记 C 中有 goto 语句。请使用 while
或 do-while
循环。
关于c - 输入名称(空格),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35152879/