【问题标题】:Why do I get these syntax error messages?为什么我会收到这些语法错误消息?
【发布时间】:2014-01-13 04:58:36
【问题描述】:

我在 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 个硬币,有人知道解决方法吗?

【问题讨论】:

  • 您将centsused 定义为数组,但将它们视为单个值。是哪一个?
  • 你真的不需要 centsused 成为 1000 个元素的数组,对吗?如果您认为int cents[1000];cents 声明为最大值为1000 的整数,请重新阅读您的教科书。
  • 好的,所以我摆脱了 1000,但它仍然无法工作@KeithThompson
  • @mrnatbus12:你“摆脱了 1000”;这是否意味着你有int cents[];?它“仍然不起作用”;这到底是什么意思?你的程序是什么样的,你现在得到什么错误信息? (还有一个小问题:我猜 CS50 是一门计算机科学课,但你希望每个人都知道吗?)
  • @KeithThompson 它看起来像 int 美分;

标签: c cs50


【解决方案1】:

我看到了三个问题!

1--> 您已将 cents 定义为 1000 元素的数组,用作单个整数

2--> 你的 scanf 不正确! (&失踪)

 scanf("%d", &cents);

3--> 你的 centsint array 并且你正在尝试使用 if for float / double.

编辑:

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

【讨论】:

  • 好的,我去掉了 100 并添加了符号,但现在最后一行不起作用错误:greedy.c:31:6: warning: format '%d' expects argument of type 'int' , 但参数 2 的类型为 'int *' [-Wformat=] printf("我们使用了 %d 个硬币", &used); ## 编辑##:我修好了没关系
  • 在 printfs 中你不应该使用 &。
  • 是的,我刚刚改了@digital_Realilty
  • 好的,我编译了,但不一定能工作,请检查编辑
【解决方案2】:

使声明的美分都用作双变量而不是数组

      double cents;
      double used;

scanf 必须传递一个地址,所以将其设为 &variable

     scanf("%lf",&cents);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-24
    • 2011-11-29
    • 1970-01-01
    相关资源
    最近更新 更多