Convert from If/If Else to Switch Statement - how is it done?
I've searched the world over and have not been able to find an example that addresses using a switch statement vs. if/if else statements.
This is for a homework assignment and the assigned text has one example that is not even close to assisting me with completing this assignment. Last week's assignment was to create a grade calculator using if statements. I completed that, but I can't figure out how to convert it. Please help! :(
Each grade should post to a textarea on a form. I've got the form created, but I can't get the switch statement to write to that area. I'm a novice, so I need detailed instructions please. Thanks for your response!
Code:
if (gradeOut >= 90)
gradeOut+= "My letter grade is: A";
else if (totalGrade < 90 && totalGrade >= 80)
gradeOut+= "My letter grade is: B";
else if (totalGrade < 80 && totalGrade >= 70)
gradeOut+= "My letter grade is: C";
else if (totalGrade < 70 && totalGrade >= 60)
gradeOut+= "My letter grade is: D";
else if (totalGrade < 60)
gradeOut+= "My letter grade is: F";
convert if to switch in c++
while(infile>>character)
{
if(character=='A')
{
infile>>value;
append(arr,i,value);
}
else if(character=='I')
{
infile>>find>>value;
if(i>98)
cout<<"insert cannot be performed as the array is full"<<endl;
else
insert(arr,i,find,value);
}
else if(character=='D')
{
infile>>value;
if(i==1)
cout<<"there are no elements to delete"<<endl;
else
del(arr,i,value);
}
else
display(arr);
}
system("pause");
}
hope it solve your problem
Quote:
Originally Posted by
lilrhino
I've searched the world over and have not been able to find an example that addresses using a switch statement vs. if/if else statements.
This is for a homework assignment and the assigned text has
one example that is not even close to assisting me with completing this assignment. Last week's assignment was to create a grade calculator using if statements. I completed that, but I can't figure out how to convert it. Please help! :(
Each grade should post to a textarea on a form. I've got the form created, but I can't get the switch statement to write to that area. I'm a novice, so I need detailed instructions please. Thanks for your response!
Code:
if (gradeOut >= 90)
gradeOut+= "My letter grade is: A";
else if (totalGrade < 90 && totalGrade >= 80)
gradeOut+= "My letter grade is: B";
else if (totalGrade < 80 && totalGrade >= 70)
gradeOut+= "My letter grade is: C";
else if (totalGrade < 70 && totalGrade >= 60)
gradeOut+= "My letter grade is: D";
else if (totalGrade < 60)
gradeOut+= "My letter grade is: F";
Code:
//By King Lord...
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
double studentGrade = 0, sumOfGrades = 0, average = 0;
double highestGrade = 0, lowestGrade = 100;
int numberOfGrades = 0;
string answer;
do {
//Step 1: Gather the input of the studentGrade
cout << "Please enter in the student grade as a percentage: "<<endl;
cin >> studentGrade;
cout <<"====================================================="<<endl;
cout << "You entered the grade as "<<studentGrade<<endl;
//Step 2: Add the grade to the sumOfGrades
sumOfGrades += studentGrade;
cout << "The sum of grade is "<<sumOfGrades<<endl;
//Step 3: Add one to numberOfGrades
numberOfGrades++;
//Step 4: Calculate the average = sumOfGrades / numberOfGrades
//average = sumOfGrades / numberOfGrades;
//cout << "The average is "<<average<<endl;
// Step 5: check if studentGrade > highestGrade
// then highestGrade = studentGrade
if (studentGrade > highestGrade)
highestGrade = studentGrade;
//Step 6: check if studentGrade < lowestGrade
// then lowestGrade = studentGrade
if (studentGrade < lowestGrade)
lowestGrade = studentGrade;
cout <<"=====================================================\n"<<endl;
cout << "Do you like to enter another grade? (y/n)"<<endl;
cin >> answer;
cout <<"====================================================="<<endl;
cout <<"=====================================================\n"<<endl;
}while(answer == "y" || answer == "yes" || answer == "Yes" || answer == "YES");
//Step 7: Output the average, highestGrade, lowestGrage
cout <<"======================================================"<<endl;
//Step 6.5: Calculate the average = sumOfGrades / numberOfGrades
average = sumOfGrades / numberOfGrades;
cout << "The average is "<<average<<endl;
cout << "The sum of grade is "<<sumOfGrades<<endl;
cout << "The highest grade is "<<highestGrade<<endl;
cout << "The lowest grade is "<<lowestGrade<<endl;
cout <<"====================================================="<<endl;
int avg = average/10;
//Step 8: Output a character grade for the average
switch(avg)
{
case 9:
cout << "The grade is an A "<<endl;
break;
case 8:
cout << "The grade is an B "<<endl;
break;
case 7:
cout << "The grade is an C "<<endl;
break;
case 6:
cout << "The grade is an D "<<endl;
break;
default:
cout << "The grade is an F "<<endl;
break;
}
return 0;
}