I have two C program that is complete, but I am trying to also get it to print a
ID: 3857913 • Letter: I
Question
I have two C program that is complete, but I am trying to also get it to print any out of bound error messages. Below is the two C program and each program has explaination of what I am looking for:
-------------------------------------------------------------------------------------------------------
First C Program - I want it to also check for out of bound and print out an error message. For example, A user accidently enters a Character value like "A" when it is asking for a number value for the grade percentage. And keep asking until the user enters the right data type value.
______________________________________________________________
#include <stdio.h>
#include <stdlib.h>
char grade(double percentage){
if (percentage >= 90)
return 'A';
if (percentage >= 80)
return 'B';
if (percentage >= 70)
return 'C';
if (percentage >= 60)
return 'D';
return 'F';
}
int main(){
double percentage;
printf("Enter the grade percentage: ", grade(percentage));
scanf("%lf",&percentage);
printf(" Your class grade is: %c ", grade(percentage));
return 0;
}
-------------------------------------------------------------------------------------------------
2nd C Program - Same thing here. Print out any error message such as if user enters more than 3 numbers or if user enters a letter instead of a number value. The program will keep asking for the correct value.
___________________________________________________________
#include <stdio.h>
#include <stdlib.h>
//This function will use to determine which value is the biggest
int biggest(int a, int b, int c){
if (a > b && a > c){
return a;
}
else if (b > c){
return b;
}
else{
return c;
}
}
//This is the main function that ask the user to input the 3 values and output the biggest one
int main()
{
int num1, num2, num3, max;
printf("Enter three numbers: ");
scanf("%d%d%d", &num1, &num2, &num3);
max = biggest(num1, num2, num3);
printf(" The biggest number is %d ", max);
return 0;
}
Explanation / Answer
Answer to first program:
Since you want to display an error message on the screen when the user gives a wong input, so, the simplest way to get the error message displayed on the screen is to make the program using switch case, here is the solution
#include<stdio.h>
#inlcude<conio.h>
#include <stdio.h>
#include <stdlib.h>
char grade(int percentage){
if (percentage >= 90)
return 'A';
if (percentage >= 80)
return 'B';
if (percentage >= 70)
return 'C';
if (percentage >= 60)
return 'D';
else
return 'F';
}
void main()
{
int per,h;
printf("enter the percentage in round figure");
scanf("%d",&per);
h=per/10;
switch(h)
{
case 9:
{
printf("the grade is %c",grade(per));
break;
}
case 8:
{
printf("the grade is %c",grade(per));
break;
}
case7:
{
printf("the grade is %c",grade(per));
break;
}
case6:
{
printf("the grade is %c",grade(per)););
break;
}
case5:
{
printf("the grade is %c",grade(per));
break;
}
default:
{
printf("wrong input");
break;
}
}
getch();
}
Answer to Second Program:
Since you have used only three integer variables in the scanf statment so the compiler will not take the fourth variable as input, there is no need to display an error message since the complier will not let the user enter more than three inputs