Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
int i=0, marks,pass=0, fail=0;
int A_Plus=0,A=0,A_minus=0,B_Plus=0,B =0,B_minus=0,C_Plus=0,C=0,C_minus=0,D_Plus=0,D=0,F=0;
while(marks !=-1)
{
scanf("%d", &marks);
if(marks<-1 || marks>100)
{
printf("You enter invalid mark: %d \n",marks);
}
if (marks != -1 &&(marks>=0 && marks <=100))
{
if(marks < 60)
{
fail = fail + 1;
}
else
{
pass = pass + 1;
}
i=i+1;
}
}
Although I do not recommend using a switch statement for "yes/no"-type decisions, such as yours, you could convert your if ... else blocks into the following switch block:
switch (marks/60) // If marks is less than 60, this expression will truncate to zero ...
{
case 0:
++fail;
break;
default: // ... otherwise, we'll get here.
++pass;
break;
}
Note that this assumes that you have previously ruled out negative numbers (as you have done).
This approach (dividing the 'switch variable' by a constant) can be useful when you have more than two possible outcomes, determined by adjacent, fixed-size ranges. For example, rather than just "fail" and "pass", you may have 4 different grades (ranges 0..29, 30..59, 60..89 and 90+); in such a case, a switch such as the following may be a good approach:
switch (marks/30)
{
case 0: // < 30
++fail;
break;
case 1: // 30 thru 59
++poor;
break;
case 2: // 60 thru 89
++good;
break;
default: // >= 90
++excellent;
break;
}
I assume this is some academic exercise rather than some useful design aim? So I'll play the silly game. Expanding on Adrian Mole's idea:
// Grade: 0 = fail
// 1 = pass
// other = invalid input
int grade == marks > 100 ?
-marks :
marks /= 60 ;
switch( grade )
{
case 0: fail++ ; break ;
case 1: pass++ ; break ;
default:
if( marks != -1) printf( "You enter invalid mark: %d \n", marks ) ;
}
Note in any case your original code can be simplified in any case:
if( marks == -1 ) // do nothing
else if( marks < -1 || marks > 100 ) printf( "You enter invalid mark: %d \n", marks ) ;
else if( marks < 60) fail++ ;
else pass++ ;
An "at face value" solution:
switch( marks )
{
case -1 : break
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9:
case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 19:
case 20: case 21: case 22: case 23: case 24: case 25: case 26: case 27: case 28: case 29:
case 30: case 31: case 32: case 33: case 34: case 35: case 36: case 37: case 38: case 39:
case 40: case 41: case 42: case 43: case 44: case 45: case 46: case 47: case 48: case 49:
case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: case 58: case 59:
fail++ ; break ;
case 60: case 61: case 62: case 63: case 64: case 65: case 66: case 67: case 68: case 69:
case 70: case 71: case 72: case 73: case 74: case 75: case 76: case 77: case 78: case 79:
case 80: case 81: case 82: case 83: case 84: case 85: case 86: case 87: case 88: case 89:
case 90: case 91: case 92: case 93: case 94: case 95: case 96: case 97: case 98: case 100:
pass++ ; break ;
default:
printf( "You enter invalid mark: %d \n", marks ) ;
}
With this latter solution at least you could insert the grade breaks that the Plus_A, A variables hint at perhaps. If you were to do that you should calculate the pass/fail count by adding the grade counts rather then incrementing for each grade case.
Moreover if you are to have multiple grades, do not have a separate variable for each one - use an array, then an enumeration to index the array:
int grade_count[12] = {0} ;
enum eGrade
{
A_Plus, A, A_minus,
B_Plus, B, B_minus,
C_Plus, C, C_minus,
D_Plus, D, F
} ;
Then you might have:
grade_count[B_Plus]++ ;
If you use gcc like compiler (gcc, clang etc) you can use the extension:
switch(marks)
{
case 0 ... 59:
fail = fail + 1;
break;
default:
pass = pass + 1;
break;
}
But it is not standard C
You have only two cases (greater or less than 60), so a switch would just be more complex. If you have a case with more (e.g. A, B, C etc. letter grades), you'd have to map [0, 100] to [A, B, C, D, F] to give a code or an enum, then switch on that result. Making the mapping would be more complex than just using a series of if / else if / else statements, but if you want to do that anyway, a loop like this would work:
enum GRADE {
GRADE_A,
GRADE_B,
...
GRADE_F,
GRADE_COUNT, // Not an actual grade.
};
...
GRADE grade = GRADE_F; // default
int mark = /* comes from somewhere */
struct {
GRADE grade;
int lowest;
} grade_list[] = {
{ GRADE_A, 90 },
{ GRADE_B, 80 },
{ GRADE_C, 70 },
{ GRADE_D, 60 },
{ GRADE_F, 0 },
};
for (int grade_idx = 0;
grade_idx < GRADE_COUNT;
grade_idx++
) {
if (marks >= grade_list[grade_idx].lowest) {
grade = grade_list[grade_idx].grade
break;
}
}
switch (grade) {
case GRADE_A:
...
break;
case ...:
...
break;
case GRADE_F:
...
break;
}
Don't do this as-is, instead collect the total count and subtract off the failures or successes later
// reject < 0 or > 100
...
marks_total++;
if (mark - 60 > 0) marks_passing++;
}
marks_failed = marks_total - marks_passing;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm trying to do switch then if else in it. I don't know where I missed but this doesn't work.
Here's my code:
include<stdio.h>
include<conio.h>
int main()
{
clrscr();
int grade;
printf("Input Grade");
scanf("%d",&grade);
switch(grade<101)
{
case 1:
if (grade>=95)
printf("A+");
else
printf("Invalid");
break;
case 2:
if (grade>=85)
printf("A");
else
printf("Invalid");
break;
default:
printf("Invalid");
}
getch();
}
So I don't know what's the matter. Only the first case work, when i enter a lower number than 95 'A' isn't the result. Please help me out.
If you have to use switch you should bring the if else statements outside of your switch in order to switch over a proper integer value. Something like this:
int state = 0;
if ( grade < 101 && grade >= 95 )
state = 1;
else if ( grade < 101 && grade >= 85 )
state = 2;
switch state {
case 1:
printf("A+");
break;
case 2:
printf("A");
break;
default:
printf("invalid");
break;
}
You can express this with switch statements in the following way (though you probably do not want to):
switch(grade)
{
case 100:
case 99:
case 98:
case 97:
case 96:
case 95:
printf("A+");
break;
case 94:
case 93:
case 92:
case 91:
case 90:
case 89:
case 88:
case 87:
case 86:
case 85:
printf("A");
break;
default:
printf("Invalid");
//or an if statement if more options are required.
break;
}
I'm making this program using switch statements that will assign letter grades based on if the user enters numbers 0 - 10. If the user enters a number that is not 0-10, the program outputs an error message and has the user re-enter. However, if the user enters a character the program will loop at the default case. I want it to output the error message from the default case once, and have them re-enter if they enter a character. I'm not sure as to why it loops the default case when a character is entered though.
#include <stdio.h>
int main()
{
int grade;
int r;
while((r = scanf("%i", &grade)) != EOF)
{
switch(grade)
{
case 10:
case 9:
printf("Your grade is an A\n");
break;
case 8:
printf("Your grade is a B\n");
break;
case 7:
printf("Your grade is a C\n");
break;
case 6:
printf("Your grade is a D\n");
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
printf("Your grade is an F\n");
break;
default:
printf("Invalid score, please re-enter\n");
}
}
return 0;
}
Try something like:
#include <stdio.h>
int main()
{
int grade;
int r=0;
while(r != 1)
{
scanf("%i", &grade);
switch(grade)
{
case 10:
case 9:
printf("Your grade is an A\n");
r=1
break;
case 8:
printf("Your grade is a B\n");
r=1
break;
case 7:
printf("Your grade is a C\n");
r=1
break;
case 6:
printf("Your grade is a D\n");
r=1
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
printf("Your grade is an F\n");
r=1
break;
default:
printf("Invalid score, please re-enter\n");
break;
}
}
return 0;
}
This will clear the input buffer on an invalid input and allow a retry.
#include <stdio.h>
int main()
{
int grade;
int r;
while((r = scanf("%i", &grade)) != EOF)
{
if ( r != 1) {//r == 1 is successful input of integer
grade = -1;//reset grade on invalid input
}
switch(grade)
{
case 10:
case 9:
printf("Your grade is an A\n");
break;
case 8:
printf("Your grade is a B\n");
break;
case 7:
printf("Your grade is a C\n");
break;
case 6:
printf("Your grade is a D\n");
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
printf("Your grade is an F\n");
break;
default:
printf("Invalid score, please re-enter\n");
while ( getchar() != '\n');//clear input buffer
}
}
return 0;
}
The reason your code always loops is because there is no way to exit out of your while other than to kill the program. Remember that break only breaks out of the inner-most switch or loop.
The cleanest way to break out of multiple levels is to use a flag. One way to do what you want is like this:
bool valid_grade = false;
while(!valid_grade && (r = scanf("%i", &grade)) != EOF)
{
valid_grade = true;
switch(grade)
{
case 10:
// unchanged from your code
default:
valid_grade = false;
printf("Invalid score, please re-enter\n");
}
}
So my professor asked us to create a switch statement. We are allowed to use only the "SWITCH" statement to do the program. He wants us to input a number and then display it if it is on the number range and what briefcase number will be taken as shown below. Now... I know that for this type of program it is easier to use the IF statement. Doing Case 1: Case 2: Case 3...Case 30 will work but will take too much time due to the number range.
#include <stdio.h>
main()
{
int x;
char ch1;
printf("Enter a number: ");
scanf("%d",&x);
switch(x)
{
case 1://for the first case #1-30
case 30:
printf("The number you entered is >= 1 and <= 30");
printf("\nTake Briefcase Number 1");
break;
case 31://for the second case #31-59
case 59:
printf("The number you entered is >= 31 and <= 59");
printf("\nTake Briefcase Number 2");
break;
case 60://for the third case #60-89
case 89:
printf("The number you entered is >= 60 and <= 89");
printf("\nTake Briefcase Number 3");
break;
case 90://for the fourth case #90-100
case 100:
printf("The number you entered is >= 90 and <= 100");
printf("\nTake Briefcase Number 4");
break;
default:
printf("Not in the number range");
break;
}
getch();
}
My professor told us that there is a shorter way on how to do this but won't tell us how. The only way I can think of shortening it is by using IF but we are not allowed to. Any Ideas on how I can make this work out?
With GCC and Clang, you can use case ranges, like this:
switch (x){
case 1 ... 30:
printf ("The number you entered is >= 1 and <= 30\n");
break;
}
The only cross-compiler solution is to use case statements like this:
switch (x){
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
printf ("The number you entered is >= 1 and <= 6\n");
break;
}
Edit: Using something to the effect of switch (x / 10) is another good way of doing this. It may be simpler to use GCC case ranges when the ranges aren't differences of 10, but on the other hand your professor might not take a GCC extension as an answer.
If the ranges are consistent, then you can throw away some of the data:
switch (x / 10 )
{
case 0:
case 1:
case 2: // x is 0 - 29
break ;
// etc ...
}
Otherwise you'll have to do a little bit of hackery around the edges.
Try this ...
#include <stdio.h>
main()
{
int x;
char ch1;
printf("Enter a number: ");
scanf("%d",&x);
int y=ceil(x/30.0);
switch(y)
{
case 1:
printf("The number you entered is >= 1 and <= 30");
printf("\nTake Briefcase Number 1");
break;
case 2:
printf("The number you entered is >= 31 and <= 60");
printf("\nTake Briefcase Number 2");
break;
case 3:
printf("The number you entered is >= 61 and <= 90");
printf("\nTake Briefcase Number 3");
break;
case 4:
printf("The number you entered is >= 91 and <= 100");
printf("\nTake Briefcase Number 4");
break;
default:
printf("Not in the number range");
break;
}
getch();
}
I want to delete the contents of an int variable when it gets to an else statement.
The program requests a number between 1 and 5 using scanf and the number is stored in the int variable and if the number isn't between 1 and 5 then the user is directed to an else statement and I have used a goto statement to take it back to the start and I was wondering how I removed the contents of the variable during the else statement so I don't create a continuos loop.
With getchar it's fpurge(stdin). I'm running Mac OS X.
BELOW IS THE CODE:
#include <stdio.h>
int main (int argc, const char * argv[])
{
int code;
start:
puts("Please type your error code.");
puts("Range 1-5: ");
scanf("%d", &code);
switch(code)
{
case 1:
printf("INFORMATION\n");
case 2:
printf("INFORMATION\n");
case 3:
printf("INFORMATION\n");
case 4:
printf("INFORMATION\n");
case 5:
printf("INFORMATION\n");
default:
printf("INFORMATION\n");
goto start;
}
}
Just set the int value to something else, eg:
theValue = 0;
You are probably looking for a do...while loop
Edit Don't forget your break statements!!
do
{
puts("Please type your error code.");
puts("Range 1-5: ");
scanf("%d", &code);
switch(code)
{
case 1:
printf("INFORMATION\n");
break;
case 2:
printf("INFORMATION\n");
break;
case 3:
printf("INFORMATION\n");
break;
case 4:
printf("INFORMATION\n");
break;
case 5:
printf("INFORMATION\n");
break;
default:
printf("INVALID CODE\n");break;
}
} while(code<1 || code> 5);