I'm currently trying to edit this program so that it would read 4 inputs on the first line of data, instead of one. All other lines contains just 1 input. I created a program to assign a letter grade to an integer score. This was my first data file:

95
85
80
75
70
68
50
104
-10

and this is my new data file:

90 80 70 60
95
85
80
75
70
68
50
104
-10

I am trying to read in the first 4 inputs on the first line as minimum grades for A, B, C, & D.
When running the program, this is my expected output:
95: A
85: B
80: B
75: C
70: C
68: D
50: F
104: illegal score
-10: illegal score


Here are the other parts of the program:

/*  A program to assign letter grades to scores  */

#include <stdio.h>


int main()
{
    /* Declare variables */
    int score;
    char grade;

        /* While the score input is equal to one */
       while(scanf("%d", &score) == 1)
       {
              /* Print score input */
              printf("%d: ", score);

              /* Call function to convert score into letter grade */
              grade = assign_grade(score);

              if (grade == '?')
              {
                printf("illegal score\n");
              }
              else
              {
              /* Print letter grade */
              printf("%c\n", grade);
              }
       }

}
/* Header file declaring assign_grade utility function */

char assign_grade(int score);
/* Given : Number score (integer)
 * Return: Letter grade (character)
 */
/* This file contains grader.c's utility function to assign a
 * character grade, given an integer score at input */

#include "assign_grade.h"

char assign_grade(int score)
/* Given : Number score (integer)
 * Return: Letter grade (character)
 */

{
  /* Variable declarations */
   char grade;

        if (score <= 100 && score >= 90)
        {
          grade = 'A';
        }
        if (score <= 89 && score >= 80)
        {
          grade = 'B';
        }
        if (score <= 79 && score >= 70)
        {
          grade = 'C';
        }
        if (score <= 69 && score >=60)
        {
          grade = 'D';
        }
        if (score <= 59 && score >= 0)
        {
          grade = 'F';
        }
        if (score < 0 || score > 100)
        {
          grade = '?';
        }


       return grade;
}

I am extremely lost. Will someone please suggest any tips or hints?

Recommended Answers

All 7 Replies

Did you actually try to read the first 4 numbers on one line? That wold be your first job. Just try something. If it doesn't work, we can help. If it does, you don't need us, nor the big wait time (1.5 hours for this one) waiting for an answer.

Did you actually try to read the first 4 numbers on one line? That wold be your first job. Just try something. If it doesn't work, we can help. If it does, you don't need us, nor the big wait time (1.5 hours for this one) waiting for an answer.

I edited main() and assign_grade2()

/*  A program to assign letter grades to scores  */

#include <stdio.h>


int main()
{
    /* Declare variables */
    int score;
    char grade;

    int s1, s2, s3, s4; /* Grading scale input */

    /* Obtain input for grading scale */
    printf("Enter four scores for grading scale: ");
    scanf("%d %d %d %d", &s1, &s2, &s3, &s4);

    /* Obtain input for scores */
    printf("Enter one score: ");

         /* While the score input is equal to one */
         while(scanf("%d", &score) == 1)
         {

              /* Print score input */
              printf("%d: ", score);

              /* Call function to assign grading scale & convert scores into letter grades */
              grade = assign_grade2(s1, s2, s3, s4, score);

              /* Print results according to grade returned from function */
              if (grade == '?')
              {
                printf("illegal score\n");
              }
              else
              {
              /* Print letter grade */
              printf("%c\n", grade);
              }
       }

}
/* This file contains mygrader2.c's utility function to assign a
 * character grade, given an integer score at input */

#include <stdio.h>
#include "assign_grade.h"
#define FLUSH while(getchar() != '\n');

char assign_grade2(int s1, int s2, int s3, int s4, int score)
/* Given : 4 inputs for gradings scale (int) & 1 number score (int)
 * Return: Letter grade (character)
 */

{
  /* Variable declarations */
   char grade;
   int gs_A, gs_B, gs_C, gs_D; /* Grading scale */

        /* Assign grading scale */
        if(s1 <= 100 && s1 >=90)
        {
         s1 = gs_A;
        }

        if(s2 <= 89 && s2 >= 80)
        {
         s2 = gs_B;
        }

        if(s3 <= 79 && s3 >= 70)
        {
         s3 = gs_C;
        }

        if(s4 <= 69 && s4 >=60)
        {
         s4 = gs_D;
        }


        /* Assign letter grade */
        if (score == gs_A)
        {
          grade = 'A';
        }
        if (score == gs_B)
        {
          grade = 'B';
        }
        if (score == gs_C)
        {
          grade = 'C';
        }
        if (score == gs_D)
        {
          grade = 'D';
        }
        if (score <= 59 && score >= 0)
        {
          grade = 'F';
        }
        if (score < 0 || score > 100)
        {
          grade = '?';
        }

       #ifdef DEBUG
       printf("debug: grade = '%c'\n", grade);
       #endif

        return grade;
}

I created the program to obtain input from a user instead. The program compiles, but when I run it, it get stuck at the function call in my while loop in main. I can't seem to figure out what's wrong with my function?

Remove all the stuff related to gs_A,B,C,D. You don't need them. You are passing in the thresholds for the grades, so use them.
If I were to say, ok, answer in terms of s1, what is the highest grade acceptable for a B? What is the lowest grade (in terms of s1) for an A. Do the same for s2,s3,s4 with the other ranges. Keep your function the way it was and translate your hard coded values into ones in terms of s1 through s4.

I created the program to obtain input from a user instead. The program compiles, but when I run it, it get stuck at the function call in my while loop in main. I can't seem to figure out what's wrong with my function?

Since we can't see your screen, "it get stuck" is too vague. If you give us details for complete understanding, you wouldn't have to wait hours for a fix.

try adding #include "assign_grade.h" above int main, so that you can reference that function. could be why it's stuck, it doesn't know where assign_grade2 is

u can use this code. only problem is that u need to start input with a blankspace or else 1st digit is lost. I f u can remove this bug do tell me.

#include<stdio.h>
main()
{
    int i=0,a[4];
    printf("enter the min for grades\n");
    while((getchar()!='\n'))
    scanf("%d",&a[i++]);

    printf("\n");
    for(i=0;i<=3;i++)
    printf("%d\t",a[i]);

    }

u can use this code. only problem is that u need to start input with a blankspace or else 1st digit is lost. I f u can remove this bug do tell me.

#include<stdio.h>
main()
{
int i=0,a[4];
printf("enter the min for grades\n");
while((getchar()!='\n'))
scanf("%d",&a[i++]);

printf("\n");
for(i=0;i<=3;i++)
printf("%d\t",a);

}

Next time, please, read the rules of the forum. It is not appropriate to write chat-room English. it is not appropriate to post code without wrapping it around code tags. See if you can find how to do that.

Concerning your posted code. It is a broken piece. It will produce a buggy program.
pranaykumar> only problem is that u need to start input with a blankspace or else 1st digit is lost.
It is not the "only problem", however that is an easy one to fix

int digit;
while ( (digit = getchar()) != '\n' ) /* that will not loose any input */

However, it will not fix your code.

while((getchar()!='\n'))
	scanf("%d",&a[i++]);

A user can enter more input that array `a' can hold and your code happily will continue overwriting beyond the boundaries of `a'.
I am not going to get into the issues of using scanf() for this purpose. There are too many.
Be warned!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.