Calcluating Grades

G

Gregc.

Hi

I am trying to calculate a persons grade, here is my code:

// Purpose: To calculate the grade code awarded for a given mark
// Arguments: mark is used to calculate grade code
// Ret.Val: The grade as a character
// Pre: The mark has been attained
// Post: The grade has been calculated according to relevant
constants
// and returned.

char getGrade(double mark) {

if (mark >=85) printf("High Distinction");
else if (mark >=75) printf("Distinction");
else if (mark >=65) printf("Credit");
else if (mark >=50) prinf ("Pass");
else printf("Fail Grade");
printf("\n");
return 0.0;
}

When I compile the code, I get error message the it forbids comparison
between pointer and integer. I know that you can do via switch
statmen, but am unsure on how to go about it. Could someone direct me
the correct direction.

Thanks in advance.

Greg
 
R

Robert Gamble

Gregc. said:
Hi

I am trying to calculate a persons grade, here is my code:

// Purpose: To calculate the grade code awarded for a given mark
// Arguments: mark is used to calculate grade code
// Ret.Val: The grade as a character
// Pre: The mark has been attained
// Post: The grade has been calculated according to relevant
constants
// and returned.

char getGrade(double mark) {

if (mark >=85) printf("High Distinction");
else if (mark >=75) printf("Distinction");
else if (mark >=65) printf("Credit");
else if (mark >=50) prinf ("Pass");
else printf("Fail Grade");
printf("\n");
return 0.0;
}

When I compile the code, I get error message the it forbids comparison
between pointer and integer. I know that you can do via switch
statmen, but am unsure on how to go about it. Could someone direct me
the correct direction.

Please copy and paste the actual program and error message. My guess
would be that you have defined getGrade as taking a pointer to double
instead of a double but why should we have to guess? Show us the real
thing.

Robert Gamble
 
W

websnarf

Gregc. said:
I am trying to calculate a persons grade, here is my code:

// Purpose: To calculate the grade code awarded for a given mark
// Arguments: mark is used to calculate grade code
// Ret.Val: The grade as a character
// Pre: The mark has been attained
// Post: The grade has been calculated according to relevant
constants
// and returned.

These are C++ comments. You may experience portability problems (if
you care about that) if you use them instead of the more standard /* */
C comments.
char getGrade(double mark) {

if (mark >=85) printf("High Distinction");
else if (mark >=75) printf("Distinction");
else if (mark >=65) printf("Credit");
else if (mark >=50) prinf ("Pass");
else printf("Fail Grade");
printf("\n");
return 0.0;
}

When I compile the code, I get error message the it forbids comparison
between pointer and integer.

The problem is with the return type. You have declared the function to
return a "char", but have returned a float. Given what this function
does, I would change the return type to void and return nothing (i.e.:
return;)
[...] I know that you can do via switch statment, but am unsure on how to go about it.

A switch statement would not help you here. Switch is only useful for
selecting between a finite number of possible inputs (which double
*ranges* are not.)
 
C

Chris McDonald

Please copy and paste the actual program and error message. My guess
would be that you have defined getGrade as taking a pointer to double
instead of a double but why should we have to guess? Show us the real
thing.


Or that the function claims to return a char, but returns a float
(if one's not fooled by that indentation).
I suspect the error is not in the function above, but its calling.
 
I

Ian Collins

Gregc. said:
Hi

I am trying to calculate a persons grade, here is my code:
missing said:
// Purpose: To calculate the grade code awarded for a given mark
// Arguments: mark is used to calculate grade code
// Ret.Val: The grade as a character
// Pre: The mark has been attained
// Post: The grade has been calculated according to relevant
constants
// and returned.

char getGrade(double mark) {

if (mark >=85) printf("High Distinction");
else if (mark >=75) printf("Distinction");
else if (mark >=65) printf("Credit");
else if (mark >=50) prinf ("Pass");
missing 't'
else printf("Fail Grade");
printf("\n");
return 0.0;
from a function that should return char?

Try adding some braces to make your if-else logic clear.
}

When I compile the code, I get error message the it forbids comparison
between pointer and integer. I know that you can do via switch
statmen, but am unsure on how to go about it. Could someone direct me
the correct direction.
The aren't any pointer comparisons....

I'm sure the above will compile if you include the required header and
fix the typo.
 
R

Robert Gamble

These are C++ comments. You may experience portability problems (if
you care about that) if you use them instead of the more standard /* */
C comments.

They have been Standard C comments for over 5 years now.
The problem is with the return type. You have declared the function to
return a "char", but have returned a float. Given what this function
does, I would change the return type to void and return nothing (i.e.:
return;)

That doesn't appear to be the problem. Despite how distasteful this
example is it is not invalid and certainly wouldn't warrant the error
message the OP described.

Robert Gamble
 
K

Keith Thompson

Gregc. said:
I am trying to calculate a persons grade, here is my code:

// Purpose: To calculate the grade code awarded for a given mark
// Arguments: mark is used to calculate grade code
// Ret.Val: The grade as a character
// Pre: The mark has been attained
// Post: The grade has been calculated according to relevant
constants
// and returned.

char getGrade(double mark) {

if (mark >=85) printf("High Distinction");
else if (mark >=75) printf("Distinction");
else if (mark >=65) printf("Credit");
else if (mark >=50) prinf ("Pass");
else printf("Fail Grade");
printf("\n");
return 0.0;
}

When I compile the code, I get error message the it forbids comparison
between pointer and integer. I know that you can do via switch
statmen, but am unsure on how to go about it. Could someone direct me
the correct direction.

The code you posted doesn't trigger the error message you say it does.
Show us your actual code, not just a fragment, and the actual error
messages.

What is the return value of getGrade supposed to be? It's declared to
return char, but you return 0.0 (which will be implicity converted to
'\0', but that's probably not what you had in mind).
 
K

Keith Thompson

Robert Gamble said:
They have been Standard C comments for over 5 years now.

True, but many compilers don't full support C99 yet (but many (most?)
C90 compilers support // comments as an extension).

But // comments are not recommended for code posted here, since line
wrapping is more likely to cause syntax errors in their presence.
 
K

Keith Thompson

Ian Collins said:
Gregc. wrote: [...]
char getGrade(double mark) {

if (mark >=85) printf("High Distinction");
else if (mark >=75) printf("Distinction");
else if (mark >=65) printf("Credit");
else if (mark >=50) prinf ("Pass");
missing 't'
else printf("Fail Grade");
printf("\n");
return 0.0;
from a function that should return char?

Try adding some braces to make your if-else logic clear.

Usually I'd agree about the braces:

char getGrade(double mark)
{

if (mark >= 85) {
printf("High Distinction");
}
else if (mark >= 75) {
printf("Distinction");
}
else if (mark >= 65) {
printf("Credit");
}
else if (mark >= 50) {
printf("Pass");
}
else {
printf("Fail Grade");
}
printf("\n");
return 0.0;
}

but in this case, as long as the code is carefully formatted, it can
be clear enough without them:

char getGrade(double mark)
{

if (mark >= 85) printf("High Distinction");
else if (mark >= 75) printf("Distinction");
else if (mark >= 65) printf("Credit");
else if (mark >= 50) printf("Pass");
else printf("Fail Grade");
printf("\n");
return 0.0;
}

A disadvantage of the second form is that it makes it more difficult
to add statements.

My personal rule is: Always use braces with control structures
*unless* the while thing is on a single line -- which should be done
only rarely. (If you prefer to make it "never" rather than "rarely",
I won't argue.)
 
C

CBFalconer

Gregc. said:
I am trying to calculate a persons grade, here is my code:

// Purpose: To calculate the grade code awarded for a given mark
// Arguments: mark is used to calculate grade code
// Ret.Val: The grade as a character
// Pre: The mark has been attained
// Post: The grade has been calculated according to relevant
constants
// and returned.

char getGrade(double mark) {

if (mark >=85) printf("High Distinction");
else if (mark >=75) printf("Distinction");
else if (mark >=65) printf("Credit");
else if (mark >=50) prinf ("Pass");
else printf("Fail Grade");
printf("\n");
return 0.0;
}

When I compile the code, I get error message the it forbids comparison
between pointer and integer. I know that you can do via switch
statmen, but am unsure on how to go about it. Could someone direct me
the correct direction.

Try this modification. Notice the organization of if/else if
clauses. Why is mark a double in the first place, seems an integer
would be more than adequate. Also don't use C99 comments unless
you have a C99 compiler (you don't) and when posting code on
newsgroups. Use the /* comment */ form instead, which survives
line wraps.

You did well to use an if/else if structure in the first place.
This is not really suited for a switch anyhow.

#include <stdio.h>

char getGrade(double mark) {

char *s;

if (mark >= 85) s = "High Distinction";
else if (mark >= 75) s = "Distinction";
else if (mark >= 65) s = "Credit";
else if (mark >= 50) s = "Pass";
else s = "Fail Grade";
printf("%s\n", s);
return s[0];
}

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
S

santosh

Gregc. said:
Hi

I am trying to calculate a persons grade, here is my code:

// Purpose: To calculate the grade code awarded for a given mark
// Arguments: mark is used to calculate grade code
// Ret.Val: The grade as a character
// Pre: The mark has been attained
// Post: The grade has been calculated according to relevant
constants
// and returned.

char getGrade(double mark) {

if (mark >=85) printf("High Distinction");
else if (mark >=75) printf("Distinction");
else if (mark >=65) printf("Credit");
else if (mark >=50) prinf ("Pass");
else printf("Fail Grade");
printf("\n");
return 0.0;
}

When I compile the code, I get error message the it forbids comparison
between pointer and integer. I know that you can do via switch
statmen, but am unsure on how to go about it. Could someone direct me
the correct direction.

You say above that the function returns a character as a grade value
and you return a floating point value in the code snippet above.

Also as given above the code doesn't contain any comparision between
pointer and integer. I suspect it is beacuse your wrongly passing a
pointer to a double to the function when it expects a double.

Unless you post your actual code, (typo in printf suggests it's not),
we can only guess at the problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,147
Latest member
CarenSchni
Top