相关疑难解决方法(0)

使用C从用户输入中获取文本

我只是在学习C并制作一个基本的"hello,NAME"程序.我已经让它工作来读取用户的输入,但它输出为数字而不是它们输入的内容?

我究竟做错了什么?

#include <stdio.h>

int main()
{
    char name[20];

    printf("Hello. What's your name?\n");
    scanf("%d", &name);
    printf("Hi there, %d", name);

    getchar();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c

13
推荐指数
2
解决办法
6万
查看次数

刷新输入流C的问题

我无法在这里刷新stdin,有没有办法刷新stdin?如果没有,那么如何让getchar()从用户输入一个字符作为输入,而不是输入缓冲区中scanf留下的"\n"? ?

#include "stdio.h"
#include "stdlib.h"

int main(int argc,char*argv[]) {
    FILE *fp;
    char another='y';
    struct emp {
        char name[40];
        int age;
        float bs;
    };
    struct emp e;
    if(argc!=2) {
        printf("please write 1 target file name\n");
    }
    fp=fopen(argv[1],"wb");
    if(fp==NULL) {
        puts("cannot open file");
        exit(1);
    }
    while(another=='y') {
        printf("\nEnter name,age and basic salary");
        scanf("%s %d %f",e.name,&e.age,&e.bs);
        fwrite(&e,sizeof(e),1,fp);

        printf("Add another record (Y/N)");
        fflush(stdin);
        another=getchar();
    }
    fclose(fp);
    return 0;
}

编辑: - 更新的代码,仍然无法正常工作

#include "stdio.h"
#include "stdlib.h"

int main(int argc,char*argv[]) {
    FILE *fp;
    char …

c stream

5
推荐指数
2
解决办法
7224
查看次数

在C中程序完成时暂停屏幕

我希望能够Press any key to exit在程序完成时做一些事情,但却无法弄清楚如何.

当我运行程序时,终端退出后才能看到结果.

//by Nyxm
#include <stdio.h>

main() {

    int temp, x, flag, num, size;

    printf("\nEnter how many numbers you wish to enter: ");
    scanf("%d", &size);
    int array[size];

    for (x = 0; x < size; x++) {
        printf("Enter an integer: ");
        scanf("%d", &num);
        array[x] = num;
    }

    printf("Please enter either 1 or 2\n1:\tAscending\n2:\tDescending\n\n...");
    scanf("%d", &num);

    if (num == 1) {
        flag = 0;
        while (flag == 0) {
            flag = 1;
            for (x = 1; …
Run Code Online (Sandbox Code Playgroud)

c

2
推荐指数
2
解决办法
8万
查看次数

标签 统计

c ×3

stream ×1