Skip to the task
Layout
Back to classic view

You are looking at lesson 3. Your place is still lesson 5, task 1: Functions.

CREATETask 4 of 22 · step 3: A grade in wordsA grade in words
A grade in words in practice/03/

Read a grade, check it with if and write it in words with switch.

Checklist

  • Every task is one commit with a clear message.

Numbered notes in the code explain the important lines.

Details & hint
  • A grade outside 1–10 is an error: say so and end with return 1;.
  • Grades 10 to 5 each get their own case with break.
  • default gets everything else: after the check only 1–4 are left, and those are "Unsatisfactory".

Stuck? Anything but 0 from main means "ended unsuccessfully". CLion shows it as Process finished with exit code 1.

practice 03 main.c Code Diff +36
A · NEW FILE Create this file. Every line is new: see the green bar.
#include <stdio.h>
#include "input.h"
int main(void) {
int grade = read_int("Grade: ");
if (grade < 1 || grade > 10) {
Note 1: ||: it is enough for one side to be true.
printf("Grade must be between 1 and 10.\n");
return 1;
Note 2: Not 0: the program ended unsuccessfully.
}
switch (grade) {
Note 3: switch compares grade with each case.
case 10:
printf("Excellent\n");
break;
Note 4: break leaves the switch; without it the next case runs too.
case 9:
printf("Very good\n");
break;
case 8:
printf("Good\n");
break;
case 7:
printf("Fair\n");
break;
case 6:
printf("Satisfactory\n");
break;
case 5:
printf("Weak\n");
break;
default:
Note 5: Everything else: here only 1–4.
printf("Unsatisfactory\n");
break;
}
return 0;
}