Help with a switch statement

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//This program will prompt the user for 5 numbers and output the average of these numbers
//and a letter grade
#include <iostream>
#include <string>
using namespace 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');
 else if(avg>=80)
 letter = ('B');
 else if(avg>=70)
 letter = ('C');
 else if(avg>=60)
 letter = ('D');
 else if (avg<60)
 letter = ('F');
 else cout << "There is an error";

 cout << "This is the letter grade: " << letter << endl;
 
 
system ("pause");
  return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
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';
}

Topic archived. No new replies allowed.