gpt4 book ai didi

c - 程序打印延迟 (c)

转载 作者:太空宇宙 更新时间:2023-11-04 04:33:24 24 4
gpt4 key购买 nike

<分区>

我写了下面的代码(它是一个计算器,它得到 3 个运算符中的 1 个(+/-/$)和 2 个自然数(a,b)和计算(a op b)(a$b 被定义为a+(a+1)+...+(b-1)+b for a<=b and for a>b it is not defined):

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

int main() {

printf("Please choose an operation (+/-/$): ");
char op;
scanf("%c", &op);
while (op != '+' && op != '-' && op != '$') {
printf("\nInvalid operation. Please choose again: ");
scanf("%c", &op);
}
int a;
int b;
char term;
printf("\nPlease enter the first operand: ");
int scanCheck = scanf("%d%c", &a, &term);
while (scanCheck != 2 || term != '\n' || a < 0) {
printf("\nInvalid number\n. Please enter the first operand: ");
scanCheck = scanf("%d%c", &a, &term);
}
printf("\nPlease enter the second operand: ");
scanCheck = scanf("%d%c", &b, &term);
while (scanCheck != 2 || term != '\n' || b < 0) {
printf("\nInvalid number\n. Please enter the first operand: ");
scanCheck = scanf("%d%c", &b, &term);
}

if (op == '$' && a > b)
printf("\nThe result is: Not Valid");

int result;
switch (op) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '$':
result = 0;
while (a <= b) {
result += a;
a++;
}
break;
}
printf("\nThe result is: %d", result);
return 0;
}

我的问题是,当我运行该程序时,它没有打印任何内容。然而,在给程序一个输入(例如 +、3、4)之后,它会打印它应该早先打印的行(具有正确的结果)。为什么会这样?我怎样才能解决这个问题?仅供引用,我正在使用带有 minGW 编译器的 eclipse Juno。

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