我在NetBeans上有一个脚本,要求用户输入。因为“控制台类型”内部终端给我一个错误,所以将控制台更改为标准输出。
它可以使用常见的简单代码运行,但是当我尝试在实际代码上运行它时,在输入之前出现消息后立即返回错误“ RUN FAILED(退出值-1.073.741.819,总时间:79ms)”。
这是代码:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
int product, price, n_product, sum, num, canti, total;
printf("What's the number of products? ");
scanf( "%d", &n_product );
num=1;
sum=0;
while(num<=n_product) {
printf("What was the product? ");
scanf( "%s", product );
printf("What was the unit price? ");
scanf( "%d", &price );
printf("How much did you buy? ");
scanf( "%d", &canti );
total=price*canti;
sum=sum+total;
num++;
}
printf("Total is %n", sum);
return (EXIT_SUCCESS);
}
我已经搜索了错误,但未显示任何结果,所以我真的不知道问题出在哪里。
没有声明的代码上没有一个变量。由于它在第一个之后立即停止,因此printf
我认为该错误可能在上,scanf
但我不知道。
编辑:
我将代码更改如下:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
int price, n_product, sum, num, canti, total;
char product[20];
printf("What's the number of products? ");
scanf( "%d", &n_product );
num=1;
sum=0;
while(num<=n_product) {
printf("What was the product? ");
scanf( "%19s", product );
printf("What was the unit price? ");
scanf( "%d", &price );
printf("How much did you buy? ");
scanf( "%d", &canti );
total=price*canti;
sum=sum+total;
num++;
}
printf("Total is %d", sum);
return (EXIT_SUCCESS);
}
现在外部端子没有错误,但是我根本无法输入,只能让我关闭它
因此,看来NetBeans完全没有理由被破坏了,我要做的就是在另一个文件上尝试以下代码:
#include <stdio.h>
int main(void) {
int product,num,sum, price, canti, total;
char name[20];
puts("What's the number of products? ");
scanf( "%d", &product );
num=1;
sum=0;
while(num<=product) {
puts("What's the product name? ");
scanf("%19s", name);
puts("What's the unit price? ");
scanf( "%d", &price );
puts("How much did you buy? ");
scanf( "%d", &canti );
total=price*canti;
sum=sum + total;
num++;
}
printf("We've bought %d", sum);
}
仍然不明白为什么,但这就是我修复它的方式。不,它不能在我尝试编辑的同一文件上工作,我必须制作一个新文件才能正常工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句