Home > Software design >  Switch statements- grading system
Switch statements- grading system

Time:02-06

#include <stdio.h>

void main() {
  int a, b, c, d, e, f, avg;

  printf("Enter marks of six subjects: ");
  scanf("%d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f);
  avg = (a   b   c   d   e   f) / 6;
  printf("Average is %d", avg);

  switch (avg) {
    case 10:
      printf("Your grade is 10.");
      break;
    case 9:
      printf("Your grade is 9.");
      break;
    case 8:
      printf("Your grade is 8.");
      break;
    case 7:
      printf("Your grade is 7.");
      break;
    case 6:
      printf("Your grade is 6.");
      break;
    case 5:
      printf("Your grade is 5.");
    default:
      printf("You are fail.");
  }
}

After I enter the marks of six subjects, the code is not running, it's neither showing any error message as well.

CodePudding user response:

Problems include

Not enabling all compiler warnings

Mis-match

"%d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f: 7 specifiers and 6 following arguments.

Not checking return value of scanf()

// scanf("%d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f);
if (scanf("%d %d %d %d %d %d", &a, &b, &c, &d, &e, &f) != 6) {
  ; // TBD: Handle error, maybe print some error message and exit
}

No break

case 5:
  printf("Your grade is 5.");
  break; // add 
default:
  printf("You are fail.");

CodePudding user response:

you can use

#include <stdio.h>

int main() {
  int a, b, c, d, e, f, avg;

  printf("Enter marks of six subjects: ");
  scanf("%d %d %d %d %d %d", &a, &b, &c, &d, &e, &f);
  avg = (a   b   c   d   e   f) / 6;
  printf("Average is %d", avg);

  switch (avg) {
    case 10:
      printf("Your grade is 10.");
      break;
    case 9:
      printf("Your grade is 9.");
      break;
    case 8:
      printf("Your grade is 8.");
      break;
    case 7:
      printf("Your grade is 7.");
      break;
    case 6:
      printf("Your grade is 6.");
      break;
    case 5:
      printf("Your grade is 5.");
      break;
    default:
      printf("You are fail.");
  }
  return 0;
}

or

const readline = require("readline").createInterface({
  input: process.stdin,
  output: process.stdout
});

function calcAverage(a, b, c, d, e, f) {
  return (a   b   c   d   e   f) / 6;
}

function getGrade(avg) {
  switch (avg) {
    case 10:
      return "Your grade is 10.";
    case 9:
      return "Your grade is 9.";
    case 8:
      return "Your grade is 8.";
    case 7:
      return "Your grade is 7.";
    case 6:
      return "Your grade is 6.";
    case 5:
      return "Your grade is 5.";
    default:
      return "You are fail.";
  }
}

readline.question("Enter marks of six subjects: ", marks => {
  let marksArray = marks.split(" ");
  let avg = calcAverage(
    parseInt(marksArray[0]),
    parseInt(marksArray[1]),
    parseInt(marksArray[2]),
    parseInt(marksArray[3]),
    parseInt(marksArray[4]),
    parseInt(marksArray[5])
  );
  console.log("Average is", avg);
  console.log(getGrade(avg));
  readline.close();
});   

or this

def calc_average(a, b, c, d, e, f):
  return (a   b   c   d   e   f) / 6

def get_grade(avg):
  if avg == 10:
    return "Your grade is 10."
  elif avg == 9:
    return "Your grade is 9."
  elif avg == 8:
    return "Your grade is 8."
  elif avg == 7:
    return "Your grade is 7."
  elif avg == 6:
    return "Your grade is 6."
  elif avg == 5:
    return "Your grade is 5."
  • Related