2014-01-13 77 views
-5

我在CS50和我的继承人greedy.c为什么我会收到这些语法错误消息?

#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硬币有没有人知道一个修复?

+4

您定义'cents'和'used'为数组,但把它们作为单一值。哪一个? – Joe

+4

你真的不需要'cents'和'used'是1000个元素的数组,是吗?如果您的印象是“int cents [1000];'将最大值为1000的整数声明为'cents',请重新阅读您的教科书。 –

+0

好吧,我摆脱了1000,它仍然无法工作@凯瑟汤普森 – mrnatbus12

回答

1

我看到的三个问题!

1 - >您已经为1000元素的数组使用单整数

2定义cents - >您的scanf函数是不正确! (&缺失)

scanf("%d", &cents); 

3 - >您centsintarray和你想如果float/double.

编辑使用:

了解一些基础知识在这里:http://www.cprogramming.com/tutorial/c-tutorial.html

+0

好吧我摆脱了100,并添加了符号,但现在最后一行不工作错误:greedy.c:31:6:警告:格式'%d'需要类型'int'的参数,但参数2类型为' int *'[-Wformat =] printf(“我们使用了%d个硬币”,&used); ##编辑##:我修正了它从来不知道 – mrnatbus12

+1

在printfs中你不应该使用& –

+0

是啊我只是改变了它@digital_Realilty – mrnatbus12

0

使这两个宣布美分和使用只是双变量不是一个数组

 double cents; 
     double used; 

scanf函数必须通过一个地址,以便使其&变量

 scanf("%lf",&cents); 
相关问题