c - 输入名称(空格)

标签 c removing-whitespace

任何人都可以帮助我编写代码

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");
}

当我尝试将程序放入表格菜单时遇到问题,如下所示:

enter image description here

输出将是这样的

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 语句。请使用 whiledo-while 循环。

关于c - 输入名称(空格),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35152879/

相关文章:

c++ - C++中的 Mongoose 服务器

css - 垂直滚动的白色 100% 框 CSS 问题

javascript - ASP.NET 和 C# : remove spaces from asp. 键入和粘贴时的文本框

html - 导致外部滚动条的空间来自哪里?

R 条纹和白色条纹

python - 文件指针上是否存在与 Python next() 等效的 C 语言?

计算给定约束下可能的密码数量

c - 分配连续物理内存的方法

html - 如何删除不必要的空格

c - Sqlite3 C API 行数