0

So I was trying to make a program that grades the user based on the marks they enter, it works but sometimes it doesn't like when I input 67 as marks for CSC111 and 45 as marks for CSC115 it doesn't display the grade for CSC115, but when I input 45 for both it shows the grade for both.

#include<stdio.h>
int main()
{
    char firstName[100];
    char lastName[100];
    int regNo;
    int marksCSC115;
    int marksCSC111;
    char grade11;
    char grade15;
    printf("Enter your name (first and last)> ");
    scanf("%s %s", firstName, lastName);
    printf("Enter your registration number> ");
    scanf("%d", &regNo);
    printf("Enter your marks in CSC111> ");
    scanf("%d", &marksCSC111);
    printf("Enter your marks in CSC115> ");
    scanf("%d", &marksCSC115);

    if(marksCSC111 >= 0 && marksCSC111 <= 39){
        grade11 = 'F';}
    else if(marksCSC111 >39 && marksCSC111 <= 49){
        grade11 = 'E'
    ;}
    else if(marksCSC111 >49 && marksCSC111 <= 59){
        grade11 = 'D'
    ;}
    else if(marksCSC111 >59 && marksCSC111 <= 69){
        grade11 = 'C'
    ;}
    else if(marksCSC111 >69 && marksCSC111 <= 79){
        grade11 = 'B'
    ;}
    else if(marksCSC111 >79) {
        grade11 = 'A'
    ;}
    if(marksCSC115 >= 0 && marksCSC111 <= 39){
        grade15 = 'F';}
    else if(marksCSC115 >39 && marksCSC111 <= 49){
        grade15 = 'E'
    ;}
    else if(marksCSC115 >49 && marksCSC111 <= 59){
        grade15 = 'D'
    ;}
    else if(marksCSC115 >59 && marksCSC111 <= 69){
        grade15 = 'C'
    ;}
    else if(marksCSC115 >69 && marksCSC111 <= 79){
        grade15 = 'B'
    ;}
    else if(marksCSC115 >79) {
        grade15 = 'A'
    ;}

printf("\nSTUDENT NAME: %s %s\n\nSTUDENT IDNO: %d\n\nCOURSE CODE   MARKS  GRADE\n\nCCS111         %d     %c\n\nCCS115         %d     %c",firstName,lastName, regNo, marksCSC111, grade11, marksCSC115, grade15);


}
Lundin
  • 195,001
  • 40
  • 254
  • 396
Sen ZmaKi
  • 109
  • 7
  • 3
    Since this code is pure C please do not tag C++ – Cory Kramer Oct 25 '21 at 12:57
  • [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Biffen Oct 25 '21 at 12:58
  • 1
    @CoryKramer Aye and as per C and C++ tag policies (see tag wiki), please don't leave such superfluous comments when you have full edit privileges and even a C++ gold badge. It means that you are trusted enough to simply edit out the irrelevant tag. – Lundin Oct 25 '21 at 12:59
  • Anyway, likely duplicate [scanf() leaves the new line char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). Add a `getchar();` after each scanf call and see if that solves the problem. – Lundin Oct 25 '21 at 13:01
  • You know that there are smarter approaches than a ton of `if`/`else`s? Example: `marks=marks/10; marks=marks>8?8:marks; marks=marks<3?3:marks; grade="ABCDEF"[8-marks];` And use functions. – 12431234123412341234123 Oct 25 '21 at 13:49
  • @ 12431234123412341234123 I just started learning to code so my code is not very efficient so as long as it works,to me I'm ok but thanks anyway even though I did not understand your code – Sen ZmaKi Oct 25 '21 at 13:56

3 Answers3

1

Your check of marksCSC115 contains both seminars. For example see the first compare

if(marksCSC115 >= 0 && marksCSC111 <= 39){

this should be

if(marksCSC115 >= 0 && marksCSC115 <= 39){

Do it for all compares and it should work. To avoid such errors you can add an else case with an error message, something like

} else {
    printf("Could not generate mark");
}
SebastianH
  • 734
  • 5
  • 23
1

This seems to be a typo.

In the if-else tree where you are supposed to calculate the value of grade15 your second half of the if-statement is compared with marksCSC111 instead of marksCSC115.

For issues like this. Check out the link @Biffen commented under your problem. It can be very helpful.

vsr
  • 173
  • 1
  • 10
0

As already noted, there are multiple typos in the posted program and that's one of the risks of duplicated code.

Instead of (wrongly) retyping the logic of the transformation from marks to grades changing only the names of the involved variables, the OP could have written (and tested) a function:

char grade_from_mark(int mark)
{
    // You may want to validate the input.
    if ( mark < 0 || mark > 100 )
        return '?';

    if ( mark < 40 )
        return 'F';
    else if ( mark < 50 )
        return 'E';
    else if ( mark < 60 )
        return 'D';
    else if ( mark < 70 )
        return 'C';
    else if ( mark < 80 )
        return 'B';
    return 'A';
}

// Then, in `main`, you could just call them to assign the grades:
// ...
char grade11 = grade_from_mark(marksCSC111);
char grade15 = grade_from_mark(marksCSC115);
// ...

Other issues may rise from the shown use of scanf. See e.g.:

How do we test the return values from the scanf() function?
How do you allow spaces to be entered using scanf?

Bob__
  • 12,361
  • 3
  • 28
  • 42