我在CS50,继承人我的贪婪。
#include <cs50.h>
#include <stdio.h>
int main (void)
{
int cents[1000];
int used[1000];
used = 0;
printf("How much change is due? (Don't use symbols ex: 0.45)\n");
scanf("%d", cents);
if (cents < 0) {
printf("Please use positve numbers only \n");
scanf("%d", cents);
};
while (cents >= 0.25) {
cents -= 0.25;
used+1;
};
while (cents >= 0.10) {
cents -= 0.10;
used+1;
};
while (cents >= 0.05) {
cents -= 0.05;
used+1;
};
while (cents >= 0.01) {
cents -= 0.01;
used+1;
};
printf("%d", used);
}
有人可以解释为什么它不起作用?我不断收到此错误消息:
greedy.c:8:7: error: incompatible types when assigning to type ‘int[1000]’ from type ‘int’
used = 0;
^
greedy.c:15:15: error: invalid operands to binary >= (have ‘int *’ and ‘double’)
while (cents >= 0.25) {
^
greedy.c:16:9: error: invalid operands to binary - (have ‘int[1000]’ and ‘double’)
cents -= 0.25;
^
greedy.c:19:15: error: invalid operands to binary >= (have ‘int *’ and ‘double’)
while (cents >= 0.10) {
^
greedy.c:20:9: error: invalid operands to binary - (have ‘int[1000]’ and ‘double’)
cents -= 0.10;
^
greedy.c:23:15: error: invalid operands to binary >= (have ‘int *’ and ‘double’)
while (cents >= 0.05) {
^
greedy.c:24:9: error: invalid operands to binary - (have ‘int[1000]’ and ‘double’)
cents -= 0.05;
^
greedy.c:27:15: error: invalid operands to binary >= (have ‘int *’ and ‘double’)
while (cents >= 0.01) {
^
greedy.c:28:9: error: invalid operands to binary - (have ‘int[1000]’ and ‘double’)
cents -= 0.01;
^
greedy.c:31:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
printf("%d", used);
^
make: *** [greedy] Error 1
编辑:好的,所以感谢@Digital_Reality我得到它编译,但现在如果我通过1.25我得到我们使用1硬币,如果我通过1000.00它说我们使用1000硬币有没有人知道修复?
答案 0 :(得分:1)
我看到三个问题!
1 - &GT;您已定义cents
,因为1000
元素的数组正在使用单个整数
2 - &GT;你的scanf不正确! (&amp; missing)
scanf("%d", ¢s);
3 - &GT;您的cents
是int
array
,而您正尝试使用if float / double.
修改强>
在此处阅读一些基础知识:http://www.cprogramming.com/tutorial/c-tutorial.html
答案 1 :(得分:0)
使两个声明的美分并用作双变量而不是数组
double cents;
double used;
scanf必须传递一个地址,因此将其变为&amp; variable
scanf("%lf",¢s);