pwndaman pwndaman - 3 years ago 109
C Question

Re-writing from switch to if else

Need help changing this C program from switch to if or if else statements for a homework assignment. I'm new to coding so just a push in the right direction is all I need. The program is designed to read letters i.e "a/A" and then output the total number of times that letter was inputted. I just need a way to convert it from switch cases to if and if else statements.

/* Counting letter grades */

#include <stdio.h>

/* function main begins program execution */
int main( void )
{
int grade; /* one grade */
int aCount = 0; /* number of As */
int bCount = 0; /* number of Bs */
int cCount = 0; /* number of Cs */
int dCount = 0; /* number of Ds */
int fCount = 0; /* number of Fs */

printf( "Enter the letter grades.\n" );
printf( "Enter the EOF character ('m')to end input.\n" );

/* loop until user types 'm' */
while ((grade=getchar()) !='m') {

/* determine which grade was input */
switch (grade) { /* switch nested in while */

case 'A': /* grade was uppercase A */
case 'a': /* or lowercase a */
++aCount; /* increment aCount */
break; /* necessary to exit switch */

case 'B': /* grade was uppercase B */
case 'b': /* or lowercase b */
++bCount; /* increment bCount */
break; /* exit switch */

case 'C': /* grade was uppercase C */
case 'c': /* or lowercase c */
++cCount; /* increment cCount */
break; /* exit switch */

case 'D': /* grade was uppercase D */
case 'd': /* or lowercase d */
++dCount; /* increment dCount */
break; /* exit switch */

case 'F': /* grade was uppercase F */
case 'f': /* or lowercase f */
++fCount; /* increment fCount */
break; /* exit switch */

case '\n': /* ignore newlines, */
case '\t': /* tabs, */
case ' ' : /* and spaces in input */
break; /* exit switch */

default: /* catch all other characters */
printf( "Incorrect letter grade entered." );
printf( " Enter a new grade.\n" );
break; /* optional; will exit switch anyway */
} /* end switch */
} /* end while */

/* output summary of results */
printf( "\nTotals for each letter grade are:\n" );
printf( "A: %d\n", aCount ); /* display number of A grades */
printf( "B: %d\n", bCount ); /* display number of B grades */
printf( "C: %d\n", cCount ); /* display number of C grades */
printf( "D: %d\n", dCount ); /* display number of D grades */
printf( "F: %d\n", fCount ); /* display number of F grades */
return 0; /* indicate program ended successfully */
} /* end function main */

Answer Source

It's as simple as that:

if(grade == 'A' || grade == 'a')
    ++aCount;
else if(grade == 'B' || grade == 'b')
    ++bCount;
else if(grade == 'C' || grade == 'c')
    ++cCount;    
else if(grade == 'D' || grade == 'd')
    ++dCount;  
else if(grade == 'F' || grade == 'f')
    ++fCount;
else if(grade == '\n' || grade == '\t' || grade == ' ')
    ++fCount; 
else {
    printf( "Incorrect letter grade entered." );
    printf( " Enter a new grade.\n" );
}

Here if a body of an if or else-if statement is executed, the other bodies will not be executed.

PS: Check this helpful Cprogramming Tutorial.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download