开发者

Need help defining functions in my C programming

I am learning C functions and trying to write a simple script that outputs a lettergrade depending on the number the user inputs. Yet I am getting the following error and I can't figure out where I'm lacking...

102809.c: In function ‘function_points’:
102809.c:44: error: ‘lettergrade’ redeclared as different kind of symbol
102809.c:41: error: previous definition of ‘lettergrade’ was here
102809.c:47: error: ‘A’ undeclared (first use in this function)
102809.c:47: error: (Each undeclared identifier is reported only once
102809.c:47: error: for each function it appears in.)
102809.c:49: error: syntax error before ‘lettergrade’ 

If you could please provide guidance, I would appreciate it. I am comparing this script to a simple C function script and they look similar :(

#include<stdio.h>
#include<stdlib.h>

//prototype
int function_points (char);

int main (void)
{
    //use a do or while loop to continue asking for user input, asking user to input test score. 
    //0 = quit

    int points; //this is student's points, we call th开发者_JAVA百科is via scanf
    int counter; //our counter variable to increase loop
    char lettergrade;
    counter = 0;

    do {
        printf("Enter the student's points:  (0 to quit): ");
        scanf("%d", &points);
        printf("%d points = %c grade \n", points, function_points(lettergrade)); //declare function
        counter++;
    } while (points != 0); 
    //counter --;


    printf("Press any key to continue...");

    getchar();
    getchar();
    return 0;
}

//function definition
int function_points (char lettergrade)
{
    int points;
    char lettergrade;

    if (points < 90 && points > 100) 
        lettergrade = A;
    else if (points < 80 && points > 89 
             lettergrade = B;
    else if (points < 70 && points > 79 
             lettergrade = C;
    else if (point < 60 && point > 69) 
             lettergrade = D;
    else 
    {
             lettergrade = F;
    }
    return lettergrade;
}


This:

int function_points (char lettergrade)
{
    int points;
    char lettergrade;

You're redeclaring your function arguments as local variables. You do not need to do that (nor can you do that). Just remove the last line from the above snippet.

Are you by chance using some C book that is pre-ANSI C (e.g. K&R 1st ed.)? The mistake looks like a mixed ANSI and old K&R declaration. If that's the case, find an updated book.

Furthermore, this:

if (points < 90 && points > 100) 
    lettergrade = A;
else if (points < 80 && points > 89 
             lettergrade = B;
else if (points < 70 && points > 79 
             lettergrade = C;
else if (point < 60 && point > 69) 
             lettergrade = D;
else 
{
             lettergrade = F;
}

This code, as written, is trying to reference variables A, B, C etc. What you probably want is characters - those would be written as 'A', 'B', 'C' etc - in single quotes.


  1. lettergrade is an argument, so you shouldn't have char lettergrade; in the function body.
  2. lettergrade = 'A'; not lettergrade = A;
  3. You're missing parentheses in your if statements
  4. Your logic is incorrect because you've switched your less than and greater than signs: if (points < 90 && points > 100) should be if (points > 89 && points <= 100) (assuming you want 90-100 to be an A)
  5. You should handle the boundary conditions (use <= on the upper bound)
  6. "point" is undefined


//function definition
int function_points (char lettergrade)
{
    int points;
    char lettergrade;  -- You can not redeclare this lettergrade here.

Throw this out and try again.


In addition to the other posts:

lettergrade = F;

is not correct syntax. That shouldn't compile. Is that where you are reciving the error -- compilation?

The correct syntax is:

lettergrade = 'F';


a) If you got a char as passed parameter, you don't have to redeclare it again. Remove the lettergrade declaration in function_points line 2.

b) There is no such literal like A. Use 'A' instead.


Just a minor point, but the 'points' your accessing in your function is local to that function, not the version in Main. You need to pass that into the points_function in order for it to make use of it.


Over and above the other answers, it looks to me as though the argument to function_points() should be 'int points' rather than 'char lettergrade', so that you are not using an uninitialized variable in the function:

int function_points(int points)
{
    char lettergrade;

    if (points < 90 && points > 100)
        lettergrade ='A';
    else if (points < 80 && points > 89)
        lettergrade = 'B';
    else if (points < 70 && points > 79)
        lettergrade = 'C';
    else if (point < 60 && point > 69) 
        lettergrade = 'D';
    else 
    {
        lettergrade = 'F';
    }
    return lettergrade;
}

You would have to alter the declaration before the main function, of course. There were also some missing close parentheses after some of the conditions.

Also, in main(), you tag the use (invocation) of function_points() as the declaration; it is not a declaration there.


Your function should return a char and take in an int. Or it can take in a char as a number, not ascii, i.e.:

char pointsToLetterGrade(unsigned int points)

{

char returnValue = 'A';

// (or you can use a bunch of if branches like in your code snippet)
while (points < 90 && returnValue < 'F')
{
    ++returnValue;
    points += 10;
}

return returnValue;

}

In the function header, char is the return type, pointsToLetterGrade is the function name, and unsigned int points is the data that the function takes in. Another thing to note, regarding "lettergrade = A;"... The computer treats a char as a BYTE, which has a numerical representation of an ascii value in memory. To treat a character as its ascii value, you surround it with single quotes. i.e. 'L' or '4' or 'A'. If you try and assign "char letter" to A, the compiler will think that A is a variable.

Hope this is helpful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜