【问题标题】:[Error]:expected '(' before 'hours'[错误]:预期 '(' 在 'hours' 之前
【发布时间】:2017-03-08 10:10:07
【问题描述】:

我正在努力学习编程。这是一个简单的问题,但我无法解决。 为什么我会收到错误消息?怎么了? 我怎么解决这个问题?请帮帮我。

这是问题定义和我的代码:

#include<stdio.h>

void calculateCharged(int c) {
    int a;
    int hours = scanf("%d", &a);
    int totalFee = 25;
}

int main(void) {

    int i;
    int b;
    printf("Please Enter Number of Cars ");
    scanf("%d", &i);
    for (b = 0; b < i; b++) {
        calculateCharged(hours)
        if (hours)<8
        totalFee += hours * 0, 5
        else if hours < 24
        int additionalHours = hours - 7
        totalFee += additionalHours * 5
        totalFee += hours * 0, 5
        else
        int days = hours / 24
        int extraHours = hours % 24
        totalFee += days * 50
        totalFee += days * 24 * 0, 5
        totalFee += extraHours * 5
        totalFee += extraHours * 0, 5
    }
}

【问题讨论】:

  • 请不要使用图像和 C/C++ 标记。请阅读how to ask
  • 这里有几百万个错误,你应该拿起一本关于 C 的书,猜测不是办法。
  • 您会出错,因为您还没有学习该语言。 Please find a good beginners book 阅读,然后从更简单的内容重新开始。
  • Mert, aradığın cevabı bulduysan cevaplardan birini kabul etmelisin。 Sorunun puanının altındaki yeşil tik işaretine tıklayarak yapabilirsin bunu。

标签: c if-statement compiler-errors runtime-error


【解决方案1】:

解决方案实际上在错误消息中。

在 C 中,if 的语法需要在条件两边加上括号。

应该是这样的

else if(hours < 24)

这是一个很好的提示,您的第一个 if 已被接受。

此外,您的范围需要大括号,您的代码看起来像未缩进的 Python 或其他东西。在尝试使用新的编程语言之前,请阅读一些基本参考资料。

【讨论】:

    【解决方案2】:

    我没有阅读您的问题定义,但您的代码至少应该像这样在语法和逻辑上是正确的:

    #include<stdio.h>
    
    float totalFee = 0;
    int hours = 0;
    
    int main(void) {
    
        int i;
        int b;
        printf("Please Enter Number of Cars ");
        scanf("%d", &i);
    
        for (b = 0; b < i; b++) {
            scanf("%d", &hours);
            if (hours < 8) {
                totalFee += hours * 0.5;
            } else if (hours < 24) {
                float additionalHours = hours - 7;
                totalFee += additionalHours * 5;
                totalFee += hours * 0.5;
            } else {
                float days = hours / 24;
                float extraHours = hours % 24;
                totalFee += days * 50;
                totalFee += days * 24 * 0.5;
                totalFee += extraHours * 5;
                totalFee += extraHours * 0.5;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 2023-04-07
      • 1970-01-01
      相关资源
      最近更新 更多