Home > Software design >  My program only works in specific cases when it should in all
My program only works in specific cases when it should in all

Time:10-26

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);


}

CodePudding user response:

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");
}

CodePudding user response:

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.

CodePudding user response:

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?

  • Related