Hi I recently wrote a code that determines average and gives a letter grade using if else statements and now I need to change it to a switch statement but have no idea how to do it. The code works fine its just now I need the if else to be a switch statement.
//This program will prompt the user for 5 numbers and output the average of these numbers
//and a letter grade
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int avg=0;
int sum=0;
int num1=0;
int num2=0;
int num3=0;
int num4=0;
int num5=0;
char letter;
letter='A';
letter='B';
letter='C';
letter='D';
letter='F';
string firstname;
//Prompt for 5 numbers and your name
cout << "Please input your name\n" << endl;
cin >> firstname;
cout << "Okay "<< firstname <<" please input five numbers each separated by a space: \n";
cin >> num1 >> num2 >> num3 >> num4 >> num5;
//Calculate the average
sum = num1+num2+num3+num4+num5;
avg=sum/5;
cout << "These are the numbers: " << num1 << "," << num2 << "," << num3 << "," << num4 << "," << num5 << endl;
cout << "This is the sum: " << num1+num2+num3+num4+num5 << endl;
cout << "This is the average: " <<sum/5 << endl;
//Determine letter grade based on average
if(avg>=90)
letter = ('A');
elseif(avg>=80)
letter = ('B');
elseif(avg>=70)
letter = ('C');
elseif(avg>=60)
letter = ('D');
elseif (avg<60)
letter = ('F');
else cout << "There is an error";
cout << "This is the letter grade: " << letter << endl;
system ("pause");
return 0;
}
switch (avg/10) // integer division will give us a number from 0 - 10
{
case 9:
case 10: letter = 'A';
break;
case 8: letter = 'B';
break;
case 7: letter = 'C';
break;
case 6: letter = 'D';
break;
default: letter = 'F';
}