tags:

views:

133

answers:

8

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 this 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;
}
+10  A: 

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.

Pavel Minaev
i thought i had to declare those variables since the function is outside int main?
HollerTrain
`lettergrade` is a function parameter. You already declared it when you wrote `int function_points(char lettergrade)` - you gave it both a name and a type.
Pavel Minaev
When I remove the two variables declarations I get an error saying I haven't declared my variables.102809.c: In function ‘function_points’:102809.c:46: error: ‘points’ undeclared (first use in this function)102809.c:46: error: (Each undeclared identifier is reported only once102809.c:46: error: for each function it appears in.)102809.c:47: error: ‘A’ undeclared (first use in this function)102809.c:49: error: syntax error before ‘lettergrade’
HollerTrain
Parameters (e.g. (char lettergrade)) are automatically "declared" for use inside the scope of that function (in this case, function_points).
Andrew Coleson
hmm. why do i have redeclare the int points tho? i am confuse why one i have to redeclare yet the other i don't.
HollerTrain
You don't redeclare it. You have one _local_ variable `points` inside function `main`, and another _local_ variable `points` inside function `function_points`. By definition, a local variable is local to a function. In fact, because of this, you're reading a value of uninitialized variable (which is going to be indeterminate) in `function_points`.
Pavel Minaev
A: 
//function definition
int function_points (char lettergrade)
{
    int points;
    char lettergrade;  -- You can not redeclare this lettergrade here.

Throw this out and try again.

bua
A: 

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';
Frank V
A: 

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.

erenon
oh ok. so since i declared char lettergrade in int main does this mean i don't have to redeclare in the function?
HollerTrain
You have it "declared" as a function parameter.Function is a closed entity, it sees only parameters from outside, or global variables (those declared outside the functions including main).
bua
+3  A: 
  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
John at CashCommons
ah yes great point!
HollerTrain
A: 

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.

Foxeris
A: 

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.

Jonathan Leffler
A: 

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.

Rantaak