473,406 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Calcluating Grades

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

Mar 26 '06 #1
10 1609
Gregc. wrote:
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

Mar 26 '06 #2
Gregc. wrote:
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.)

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Mar 26 '06 #3
"Robert Gamble" <rg*******@gmail.com> writes:
Gregc. wrote:
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.

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.

--
Chris.
Mar 26 '06 #4
Gregc. wrote:
Hi

I am trying to calculate a persons grade, here is my code:
missing <stdio.h>
// 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.

--
Ian Collins.
Mar 26 '06 #5

we******@gmail.com wrote:
Gregc. wrote:
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.


They have been Standard C comments for over 5 years now.
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;)


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

Mar 26 '06 #6
"Gregc." <gr*********@bigpond.com> writes:
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).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 26 '06 #7
"Robert Gamble" <rg*******@gmail.com> writes:
we******@gmail.com wrote:
Gregc. wrote:
> 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.


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 26 '06 #8
Ian Collins <ia******@hotmail.com> writes:
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 26 '06 #9
"Gregc." wrote:

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:
news:news.announce.newusers
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
Mar 26 '06 #10
Gregc. wrote:
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.

Mar 26 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Penfold | last post by:
I'd appreciate help converting student average test scores into grades. My problem is that I need to allocate one of about 20 grades (3a,3b,3c,4a,4b,4c etc through to 8c plus a couple of others)....
1
by: waldoruns | last post by:
I need help on this code. I have to write a code that reads in a list of exam scores and outputs the total number of grades and the total number of grades in each letter category. Here is what I...
4
by: vadrama | last post by:
takes a list of grades and counts the amount A's, B's, C's etc... I am using a while statement to count the total number of grades which works great, but my if and else statements to count how...
2
by: tatum | last post by:
I really just need some pointers... jumping points from here... something the problem: Write a grading program for a class with the following grading policies: 1. There are two quizzes,...
1
by: angeldeveloper | last post by:
Start Declare: lastName, finalGrade, outfile Open "grades.dat" For Output As outfile Write "Enter last name and final grade separated by spaces: " Write "Enter 0 for both when...
2
by: gdarian216 | last post by:
the program reads input from a file and then outputs the averages and grade. for some reason it is reading in the same line twice and it doesn't print out the grade. everything else is correct, if...
2
by: Richard Hollenbeck | last post by:
I originally wrote my grades program in MS-Access to help community college teachers in the California community colleges keep track of their students' grades and produce reports for the students...
8
by: midknight5 | last post by:
Heya everyone, I am a freshman in college and am taking C++. This is not a begging thread for someone to solve my problem because I am interested in learning this myself. So here is my question:...
3
by: Energizer100 | last post by:
Hey. I'm new to Java and I'm trying to make a grades program for a class assignment. This is what I have so far. import java.util.Scanner; import static java.lang.System.out; public class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.