首页 > 解决方案 > 输出前双输入

问题描述

我遇到了一些代码的问题,我的输入提示都在输出之前运行,而结构在这里 -

scanf("%d\n\n", &d);

printf("\nYour result is: %d. \nWhat is the random number?:", r * d);

scanf("%d\n\n", &s);

所以发生的是我得到第一个输入提示,然后是第二个,然后 printf 运行。我将如何修复它以便 scanf 运行,然后 printf,然后再次 scanf?

完整的帮助代码 -

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int d;
int s;

int main() {

    //r for rand, d for dividing number, s for sum

    int r = rand() % 1 + 10;
    time_t SFE = time(NULL);
    srand(SFE);

    r = rand() % r;

    printf("Please choose a number: ");
    scanf("%d\n\n", &d);

    printf("\nYour result is: %d. \nWhat is the random number?:", r * d);

    scanf("%d", &s);

    if (s == r) {
        printf("\nYou have the correct number!");
        }
    else {
        printf("\nSorry, that's wrong. The correct answer is %d! Try again next time!", r);
        }


    return 0;
}

标签: c

解决方案


谢谢用户3121023

答案只是删除换行符。


推荐阅读