Grade Program Question

H

Henry

I was doing this program for an exercise in a book. The point was to create
a program that would take a numerical grade from a user and convert it to a
letter grade (yeah really easy). I tried to incorporate the while loop to
keep the program running continuously so the user could keep entering grades
to get the letter number, but I had little success and just kept creating
infinite loops. My question is:

How could I modify my program to read the last letter of the input and add +
or - to the end of the letter grade..
1-3 = -
4-6 = <blank>
7-9 = +

Here is my program so far:

#include <stdio.h>
#include <string.h>
main()
{
int letter_grade;
int numeric_grade;
int grade[100];

(void)printf("Enter the Numeric grade: ");
(void)fgets(grade, sizeof(grade), stdin);
(void)sscanf(grade, "%d", &numeric_grade);

if (numeric_grade <= 60)
letter_grade = "F";
if (numeric_grade > 60 && numeric_grade <= 70)
letter_grade = "D";
if (numeric_grade > 70 && numeric_grade <= 80)
letter_grade = "C";
if (numeric_grade > 80 && numeric_grade <= 90)
letter_grade = "B";
if (numeric_grade > 90 && numeric_grade <= 100)
letter_grade = "A";

(void)printf("The Letter grade is: %s\n", letter_grade);
}
 
J

Jeff

Henry said:
I was doing this program for an exercise in a book. The point was to create
a program that would take a numerical grade from a user and convert it to a
letter grade (yeah really easy). I tried to incorporate the while loop to
keep the program running continuously so the user could keep entering grades
to get the letter number, but I had little success and just kept creating
infinite loops. My question is:

How could I modify my program to read the last letter of the input and add +
or - to the end of the letter grade..
1-3 = -
4-6 = <blank>
7-9 = +

Here is my program so far:

#include <stdio.h>
#include <string.h>
main()

It should be " int main() ". If you C book write "main()", it is wrong and
it does not follow the ANSI standard.

{
int letter_grade;
int numeric_grade;
int grade[100];

(void)printf("Enter the Numeric grade: ");
(void)fgets(grade, sizeof(grade), stdin);
(void)sscanf(grade, "%d", &numeric_grade);

The (void) is unnecessary, it is just confusing. You can write

printf("Enter the Numeric grade: ");
gets(grade, sizeof(grade), stdin);
sscanf(grade, "%d", &numeric_grade);
if (numeric_grade <= 60)
letter_grade = "F";

"F" is string literal, and your "letter_grade" is an integer. You can only
write

letter_grade = 'F';
if (numeric_grade > 60 && numeric_grade <= 70)
letter_grade = "D";
if (numeric_grade > 70 && numeric_grade <= 80)
letter_grade = "C";
if (numeric_grade > 80 && numeric_grade <= 90)
letter_grade = "B";
if (numeric_grade > 90 && numeric_grade <= 100)
letter_grade = "A";

(void)printf("The Letter grade is: %s\n", letter_grade);
}

You can't use letter_grade for %s. Remember, letter_grade IS NOT a string.
You can write

printf("the letter grade is: %c\n", letter_grade);
 
A

Artie Gold

Jeff said:
I was doing this program for an exercise in a book. The point was to
create

a program that would take a numerical grade from a user and convert it to
a

letter grade (yeah really easy). I tried to incorporate the while loop to
keep the program running continuously so the user could keep entering
grades

to get the letter number, but I had little success and just kept creating
infinite loops. My question is:

How could I modify my program to read the last letter of the input and add
+

or - to the end of the letter grade..
1-3 = -
4-6 = <blank>
7-9 = +

Here is my program so far:

#include <stdio.h>
#include <string.h>
main()


It should be " int main() ". If you C book write "main()", it is wrong and
it does not follow the ANSI standard.


{
int letter_grade;
int numeric_grade;
int grade[100];

(void)printf("Enter the Numeric grade: ");
(void)fgets(grade, sizeof(grade), stdin);
(void)sscanf(grade, "%d", &numeric_grade);


The (void) is unnecessary, it is just confusing. You can write

printf("Enter the Numeric grade: ");
gets(grade, sizeof(grade), stdin);

Obviously you meant: fgets(...);
sscanf(grade, "%d", &numeric_grade);




"F" is string literal, and your "letter_grade" is an integer. You can only
write

letter_grade = 'F';




You can't use letter_grade for %s. Remember, letter_grade IS NOT a string.
You can write

printf("the letter grade is: %c\n", letter_grade);

HTH,
--ag

p.s. The cast to `void' on printf, et. al. is sometimes necessary to
make overly aggressive lint-like tools stop complaining because of
the discarded result value -- and, as the OR states, for no other
reason.
 
H

Henry

It should be " int main() ". If you C book write "main()", it is wrong and
it does not follow the ANSI standard.

Well even in K&R second edition it has yet to mention anything about int
main()
The (void) is unnecessary, it is just confusing. You can write

printf("Enter the Numeric grade: ");
gets(grade, sizeof(grade), stdin);
sscanf(grade, "%d", &numeric_grade);

I'm just doing what "Practical C" is telling me to..

"F" is string literal, and your "letter_grade" is an integer. You can only
write

letter_grade = 'F';

I tried it both ways , and both ways worked.
You can't use letter_grade for %s. Remember, letter_grade IS NOT a string.
You can write

printf("the letter grade is: %c\n", letter_grade);

Isn't a string made up of chars? So technically both ways would be right?
.....
 
J

Jeff

Artie Gold said:
[snip]


Obviously you meant: fgets(...);
Yes.

[snip]


p.s. The cast to `void' on printf, et. al. is sometimes necessary to
make overly aggressive lint-like tools stop complaining because of
the discarded result value -- and, as the OR states, for no other
reason.

Yes, it is not the first time to see such code in clc. The "lint compatible
code" is ugly.
 
A

Artie Gold

Jeff said:
Jeff said:
I was doing this program for an exercise in a book. The point was to
[snip]


Obviously you meant: fgets(...);

Yes.

[snip]



p.s. The cast to `void' on printf, et. al. is sometimes necessary to
make overly aggressive lint-like tools stop complaining because of
the discarded result value -- and, as the OR states, for no other
reason.


Yes, it is not the first time to see such code in clc. The "lint compatible
code" is ugly.

Yes. Hideously so!

--ag
 
A

Artie Gold

Henry wrote:

[snip]
Isn't a string made up of chars? So technically both ways would be right?
....

No!
%s expects a pointer to a null terminated sequence of `char's.
%c expects a single `char'.

HTH,
--ag
 
A

Al Bowers

Henry said:
I was doing this program for an exercise in a book. The point was to create
a program that would take a numerical grade from a user and convert it to a
letter grade (yeah really easy). I tried to incorporate the while loop to
keep the program running continuously so the user could keep entering grades
to get the letter number, but I had little success and just kept creating
infinite loops. My question is:

How could I modify my program to read the last letter of the input and add +
or - to the end of the letter grade..
1-3 = -
4-6 = <blank>
7-9 = +

You can use function strchr to position to the digit.
Here is my program so far:

#include <stdio.h>
#include <string.h>
main()
{
int letter_grade;

If the point of the code is to just print the letter grade, you
do not need this variable.
int numeric_grade;
int grade[100];

char grade[100], *s;
variable s is to be used to point to the end of the letter grade.
(void)printf("Enter the Numeric grade: ");
(void)fgets(grade, sizeof(grade), stdin);
(void)sscanf(grade, "%d", &numeric_grade);

No need to cast the above.
if (numeric_grade <= 60)
letter_grade = "F"; putchar('F');
if (numeric_grade > 60 && numeric_grade <= 70)
letter_grade = "D"; putchar('D');
if (numeric_grade > 70 && numeric_grade <= 80)
letter_grade = "C";

and on and on.
if (numeric_grade > 80 && numeric_grade <= 90)
letter_grade = "B";
if (numeric_grade > 90 && numeric_grade <= 100)
letter_grade = "A";

(void)printf("The Letter grade is: %s\n", letter_grade);
}
replace with
if(number_grade > 60)
{
if((s = strchr(grade,'\n')) != NULL)
{
s--;
if(*s < '4') putchar('-');
else if(*s > '6') putchar('+');
}
putchar('\n');

Corrected the code becomes the following. However, the code
assumes valid user input, which is not a very good assumption.

#include <stdio.h>
#include <string.h>

int main(void)
{
int numeric_grade;
char grade[100],*s ;

printf("Enter the Numeric grade: ");
fflush(stdout);
fgets(grade, sizeof(grade), stdin);
sscanf(grade, "%d", &numeric_grade);

if (numeric_grade <= 60)
putchar('F');
if (numeric_grade > 60 && numeric_grade <= 70)
putchar('D');;
if (numeric_grade > 70 && numeric_grade <= 80)
putchar('C');
if (numeric_grade > 80 && numeric_grade <= 90)
putchar('B');
if (numeric_grade > 90 && numeric_grade <= 100)
putchar('A');

if(numeric_grade > 60)
{
if((s = strchr(grade,'\n')) != NULL)
{
s--;
if(*s < '4') putchar('-');
if(*s > '6') putchar('+');
}
}
putchar('\n');
return 0;
}
 
J

Jeff

Henry said:
Well even in K&R second edition it has yet to mention anything about int
main()


You should read comp.lang.c FAQ 11.12

CLC FAQ : http://www.eskimo.com/~scs/C-faq/top.html
I'm just doing what "Practical C" is telling me to..

I know.
I tried it both ways , and both ways worked.

"F" is not same as 'F'.

"F" is the string literal (2 bytes). it contains the letter F and a null
character.

'F' is a single char.
Isn't a string made up of chars? So technically both ways would be right?
....

string is made up of chars, not by a single character.


char mychar = 'A'; /* it is a character, it contains the charset
value of letter 'A' */

char mystring[20]; /* it is an array of "char". */

char mystring2[] = "Hello"; /* it is an array of "char", initialized
to "Hello" */

char *mystring3 = "Hello"; /* it is a pointer to char. it pointers to a
string stored somewhere in memory. */
 
R

Rouben Rostamian

if (numeric_grade <= 60)
putchar('F');
if (numeric_grade > 60 && numeric_grade <= 70)
putchar('D');;
if (numeric_grade > 70 && numeric_grade <= 80)
putchar('C');
if (numeric_grade > 80 && numeric_grade <= 90)
putchar('B');
if (numeric_grade > 90 && numeric_grade <= 100)
putchar('A');

A better way of writing this decision sequence is:

if (numeric_grade > 90)
putchar('A');
else if (numeric_grade > 80)
putchar('B');
else if (numeric_grade > 70)
putchar('C');
else if (numeric_grade > 60)
putchar('D');
else
putchar('F');
 
H

Henry

You can use function strchr to position to the digit.

Well I was thinking of how I could make it work using only the things I have
learnt thus far and I came up with :

fgets(...)
sscanf(..., "%d" "%d", &...)

Couldn't I use that to break the input into 2 seperate digits so I could
manipulate the output accordingly.. I know it would make the program quite
long but I am just trying to use the information I have been presented with.
 
B

Bill Reed

Henry wrote:
How could I modify my program to read the last letter of the input and add +
or - to the end of the letter grade..
1-3 = -
4-6 = <blank>
7-9 = +

Corrected the code becomes the following. However, the code
assumes valid user input, which is not a very good assumption.

#include <stdio.h>
#include <string.h>

int main(void)
{
int numeric_grade;
char grade[100],*s ;

printf("Enter the Numeric grade: ");
fflush(stdout);
fgets(grade, sizeof(grade), stdin);
sscanf(grade, "%d", &numeric_grade);

if (numeric_grade <= 60)
putchar('F');
if (numeric_grade > 60 && numeric_grade <= 70)
putchar('D');;
if (numeric_grade > 70 && numeric_grade <= 80)
putchar('C');
if (numeric_grade > 80 && numeric_grade <= 90)
putchar('B');
if (numeric_grade > 90 && numeric_grade <= 100)
putchar('A');

if(numeric_grade > 60)
{
if((s = strchr(grade,'\n')) != NULL)
{
s--;
if(*s < '4' ) putchar('-');

if(*s said:
if(*s > '6') putchar('+');

if(*s > '6' || *s == '0') // else 100 is A
 
N

Nick Austin

I was doing this program for an exercise in a book. The point was to create
a program that would take a numerical grade from a user and convert it to a
letter grade (yeah really easy). I tried to incorporate the while loop to
keep the program running continuously so the user could keep entering grades
to get the letter number, but I had little success and just kept creating
infinite loops. My question is:

How could I modify my program to read the last letter of the input and add +
or - to the end of the letter grade..
1-3 = -
4-6 = <blank>
7-9 = +

You can do this with the modulus operator, so the expression
numeric_grade % 10 produces an answer between 0 and 9. An
easy to convert this to -/space/+ is to use this value as
an index into a string literal:

char GradeSuffix( int numeric_grade )
{
if ( numeric_grade > 60 && numeric_grade <= 100 )
return "+--- +++"[ numeric_grade % 10 ];
else
return ' ';
}
Here is my program so far:

#include <stdio.h>
#include <string.h>
main()
{
int letter_grade;

const char *letter_grade = "?";
int numeric_grade;
int grade[100];

char grade[100];
(void)printf("Enter the Numeric grade: ");
(void)fgets(grade, sizeof(grade), stdin);
(void)sscanf(grade, "%d", &numeric_grade);

if (numeric_grade <= 60)
letter_grade = "F";
if (numeric_grade > 60 && numeric_grade <= 70)
letter_grade = "D";
if (numeric_grade > 70 && numeric_grade <= 80)
letter_grade = "C";
if (numeric_grade > 80 && numeric_grade <= 90)
letter_grade = "B";
if (numeric_grade > 90 && numeric_grade <= 100)
letter_grade = "A";

The program structure would be cleaner if these were a separate
function.
letter_grade = LetterGrade( numeric_grade );
(void)printf("The Letter grade is: %s\n", letter_grade);

printf("The Letter grade is: %s %c\n", letter_grade,
GradeSuffix( numeric_grade ) );
 
P

pete

Henry said:
Well I was thinking of how I could make it work using only the things I have
learnt thus far and I came up with :

fgets(...)
sscanf(..., "%d" "%d", &...)

Couldn't I use that to break the input into
2 seperate digits so I could manipulate the output accordingly..
I know it would make the program quite
long but I am just trying to use the information
I have been presented with.

/* BEGIN grade.c */

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

#define STRINGLENGTH 8
#define str(x) # x
#define xstr(x) str(x)

void l_grade(char *);

int main(void)
{
char string[STRINGLENGTH + 1] = {'\0'};
int rc;

fputs("Enter the Numeric grade: ", stdout);
fflush(stdout);
rc = scanf("%" xstr(STRINGLENGTH) "[^\n]%*[^\n]", string);
getchar();
while (rc) {
l_grade(string);
printf("The Letter grade is: %s\n", string);
fputs("Enter the Numeric grade: ", stdout);
fflush(stdout);
rc = scanf("%" xstr(STRINGLENGTH) "[^\n]%*[^\n]", string);
getchar();
}
return 0;
}

void l_grade(char *string)
{
char letter[] = {'F','D','C','B','A'};
int number;
int grade;

number = atoi(string);
if (number > 99) {
number = 99;
}
if (number > 60) {
grade = (number - 60) / 10 + 1;
switch (number % 10) {
case 0:
case 1:
case 2:
case 3:
string[1] = '-';
break;
case 7:
case 8:
case 9:
string[1] = '+';
break;
default:
string[1] = '\0';
break;
}
string[2] = '\0';
} else {
grade = 0;
string[1] = '\0';
}
string[0] = letter[grade];
}

/* END grade.c */
 
P

pete

pete said:
#include <string.h>

#define STRINGLENGTH 8
/*
** change the above to:
*/
#include <assert.h>

#define STRINGLENGTH 3

/***************************/
/*
** And insert the assertion line, here:
*/
assert(STRINGLENGTH > 2);
 
D

Default User

Jeff wrote:
It should be " int main() ". If you C book write "main()", it is wrong and
it does not follow the ANSI standard.


So you are saying we should throw away our copies of K&R2?




Brian Rodenborn
 
J

Jeff

Default User said:
So you are saying we should throw away our copies of K&R2?

I meant no offense. We know K&R2 is using void main, and it is not the first
time to talk about this. If one day you teach someone how to write C
language, you will point out the void main is wrong, right ?

comp.lang.c faq

11.12 Can I declare main as void, to shut off these annoying ``main returns
no value'' messages?

11.15 The book I've been using always uses void main().
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top