gpt4 book ai didi

c - scanf 被跳过

转载 作者:太空狗 更新时间:2023-10-29 16:58:55 24 4
gpt4 key购买 nike

<分区>

我正在尝试为一个类制作一个简单的 C 程序,其中一个要求是我需要对所有输入和输出使用 scanf/printf .我的问题是为什么我的 scanf 在 main 中的 for 循环之后被跳过并且程序刚刚终止。

这是我的代码

#include <stdio.h>

void main() {
int userValue;
int x;
char c;

printf("Enter a number : ");
scanf("%d", &userValue);
printf("The odd prime values are:\n");
for (x = 3; x <= userValue; x = x + 2) {
int a;
a = isPrime(x);
if (a = 1) {
printf("%d is an odd prime\n", x);
}
}
printf("hit anything to terminate...");
scanf("%c", &c);
}

int isPrime(int number) {
int i;
for (i = 2; i < number; i++) {
if (number % i == 0 && i != number)
return 0;
}
return 1;
}

我可以通过在第一个之后添加另一个相同的 scanf 来“修复”它,但我更愿意只使用那个。

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