【问题标题】:C error cannot convert argument 1 from int * to intC 错误无法将参数 1 从 int * 转换为 int
【发布时间】:2014-04-02 07:22:53
【问题描述】:

刚接触 C 和学习。我不断收到错误(C2664: 'int readScores(int,int,int)' : cannot convert argument 1 from 'int *' to 'int')。我不知道如何解决它。我试着查了一下,但不明白错误代码...... 我该如何解决? 此外,任何关于代码的指针和或/提示将不胜感激。 谢谢

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

// Functions 
int readScores(int test1, int test2, int test3);
int determineGrade(int test1, int test2, int test3);
void print(int test1, int test2, int test3);


int main(void)
{
    int test1;
    int test2;
    int test3;
    readScores(&test1, &test2, &test3);
    determineGrade(test1, test2, test3);
    print(test1, test2, test3);
    return 0;
}

void readScores(int *test1, int *test2, int *test3)
{
    // Promts
    printf("Hello, this program will determine");
    printf("the grades of average test scores");
    printf("to see if you passed or not this year.");
    printf("Please enter in the three test...");
    printf("Note: only enter scores that are (0-100)");

    printf("Enter in test1\n");
    scanf("%d", test1);
    printf("Enter in test2\n");
    scanf("%d", test2);
    printf("Enter in test 3\n");
    scanf("%d", test3);
    return;
}
int determineGrade(int test1, int test2, int test3)
{
    // Local declrations
    int average;

    // Math
    average = 3 / (test1 + test2 + test3);
    return average;
}
void print(int test1, int test2, int test3)
{
    int Grade;
    Grade = determineGrade(test1, test2, test3);
    if (Grade > 90)
    {
        printf("Great job you have an A %d int the class\n", Grade);
        return;
    }
    else if (70 < Grade > 90, test3)
    {
        if (test3 < 90)
        {
            printf("Good job you got a A %d\n", Grade);
            return;
        }
        else
        {
            printf("Easy Beezy you got a B %d for the class\n", Grade);
            return;
        }
        return;
    }
    else if (50 < Grade > 70, test2, test3)
    {
        Grade = 2 / (test2 + test3);
        if (Grade > 70)
        {
            printf("You passed congrats you have a C %d for the class\n", Grade);
            return;
        }
        else
        {
            printf("You have a D for the class %d\n", Grade);
            return;
        }
    }
    else if (Grade < 50)
    {
        printf("Yeah you might want to take this class again you have a F %d\n", Grade);
        return;
    }
    return;
}

【问题讨论】:

  • 您对readScores 的声明与您对readScores 的定义不匹配。参数类型不同。甚至返回类型也不同。这是没有意义的。你试图通过做出不匹配的定义来做什么?你是代码的作者吗?

标签: c if-statement for-loop int


【解决方案1】:

int readScores(int test1, int test2, int test3); 方法声明为 int 类型,但您使用指针定义(并调用)它。

将声明更改为int readScores(int* test1, int* test2, int* test3);

【讨论】:

  • 更改 scanf 调用是错误的——它需要一个地址,而不是一个 int。
【解决方案2】:

你必须制作函数原型,例如

int readScores(int test1, int test2, int test3);

匹配实际功能实现:

void readScores(int *test1, int *test2, int *test3)

稍微详细一点。您的程序中的数据流很好,您在 main 中声明了三个变量,将指向这些变量的指针作为参数传递给 readScores,然后它将修改它们。然后将变量用于打印和计算。所以你唯一的问题是你首先错误地告诉编译器“readScores 有三个 int 参数”,而你用三个 int 指针实现它。

【讨论】:

    【解决方案3】:

    main,无需调用

    determineGrade(test1, test2, test3);
    

    正如您所说并在您的打印功能中真正使用它。此外,determineGrade 函数似乎有点偏离。对于平均值,您希望分数的总和在分子中,而 3 在分母中:

    average = (test1 + test2 + test3) / 3;
    

    虽然取0-100分的平均值时没有必要,但以后取平均值时,最好把分子的每一部分分别除以分母,然后相加:

    average = (test1 / 3) + (test2 / 3) + (test3 / 3);
    

    如果您的分母项总和大于MAX_INT,这可以避免溢出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      • 2018-11-25
      • 2015-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多