switches????

how do i create a program so that when i put characters like A,B,C. And they have to calculate a value, like average. Ofcourse A,B,C are constants. And I want to use switch statements. But im totally lost
Last edited on
So are we - can you give us a more thorough explanation of what you need from us? What have you done so far?
Ok so the question is like this,

Im supposed to write a program that will calculate the grade point average . It is calculated as follows (A=4.0, B=3.0, C=2.0, D=1.0 and F=0.0).

The program is supposed to read the grades of 4 subjects, and calculate the grade point average.

eg: Grade for SUB 1: A
Grade for SUB 1: C
Grade for SUB 1: A
Grade for SUB 1: D

Grade point average is 4+2+4+1=11


Hope that iv made it clear


Bettie, I wrote a little program here that uses the constants A, B, & C set to some numbers, and then gives a menu for choosing what to do with those numbers. It uses a switch statement to handle the choice menu.
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream.h>
using namespace std;

const int A=5;
const int B=7;
const int C=10;

int main()
{
  int choice;
  bool quit=false;
  while (!quit) {
    cout << endl << "We have three constants: A, B & C." << endl;
    cout << endl;
    cout << "A equals " << A << endl;
    cout << "B equals " << B << endl;
    cout << "C equals " << C << endl;
    cout << endl;
    cout << "What do you want to do with A, B, & C?" << endl << endl;
    cout << "1. Add: A + B + C" << endl;
    cout << "2. Subtract: A - B - C" << endl;
    cout << "3. Multiply: A * B * C" << endl;
    cout << "4. Divide: A / B / C" << endl;
    cout << "5. Average: A, B, & C" << endl;
    cout << "6. Quit" << endl;
    cout << endl;
    cout << "Enter choice: ";
    cin >> choice;
    cout << endl;
    switch (choice)
    {
      case 1:
        cout << "We will ADD them." << endl;
        cout << A << "+" << B << "+" << C << "=" << A+B+C << endl;
        cout << endl;
        break;
      case 2:
        cout << "We will SUBTRACT them." << endl;
        cout << A << "-" << B << "-" << C << "=" << A-B-C << endl;
        cout << endl;
        break;
     case 3:
        cout << "We will MULTIPLY them." << endl;
        cout << A << "*" << B << "*" << C << "=" << A*B*C << endl;
        cout << endl;
        break;
      case 4:
        cout << "We will DIVIDE them." << endl;
        cout << A << "/" << B << "/" << C << "=" <<  (float)A/B/C << endl;
        cout << endl;
        break;
      case 5:
        cout << "We will AVERAGE them." << endl;
        cout << "(" << A << "+" << B << "+" << C << ")/3=" << (A+B+C)/3.0 << endl;
        cout << endl;
        break;
      case 6:
        cout << "You want to quit!" << endl;
        quit=true;
        break;
      default:
        cout << "You need to choose a number of 1 through 6 from the menu." << endl;
        cout << endl;
    }
  }
  return 0;
}


Look the program over and see if you can understand how the parts work. If you need help or have questions, you know what to do...
And so far i have only started it. Im stuck where im supposed to write the switches. And im not so sure whether what iv written is correct either.
See if this one works right on your compiler:
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
#include <iostream>
#include <iomanip>

const float A=4.0;
const float B=3.0;
const float C=2.0;
const float D=1.0;
const float F=0.0;

int main()
{
  char ch='\0';
  float grades=0;
  int numberOfGrades=0;
  while (ch!='X' && ch!='x') {
    cout << endl;
    cout << "Enter grade: A, B, C, D, or F.  Enter X to quit adding grades." << endl;
    cin >> ch;
    switch (ch) {
      case 'A': case 'a':
        grades = grades + A;
        numberOfGrades = numberOfGrades + 1;
        break;
      case 'B': case 'b':
        grades = grades + B;
        numberOfGrades = numberOfGrades + 1;
        break;
      case 'C': case 'c':
        grades = grades + C;
        numberOfGrades = numberOfGrades + 1;
        break;
      case 'D': case 'd':
        grades = grades + D;
        numberOfGrades = numberOfGrades + 1;
        break;
      case 'F': case 'f':
        grades = grades + F;
        numberOfGrades = numberOfGrades + 1;
        break;
    }
  }
  cout << setprecision(1) << setiosflags(ios::fixed);
  cout << endl;
  cout << "You entered " << numberOfGrades << " grades and the average of those grades is:" << endl;
  cout << grades/numberOfGrades << endl;
}

I gave up switches, cox it was too confusing. Tried doing it with "for" loop. But im not getting it write. The grade point average is the addition of all the four subject values. Only addition.

Hi Bettie,
I tried to understand your program, and I tried to solve your problem in my own way. Forgive me for any lackness, for I am still at the beginner stage.
One thing I still don't understand--> Why is that the point average calculated based on the addition only?
Here is my program:
Last edited on
#include<iostream.h>
main()
{
int i;
float total=0.00,p,g[5]={4.00,3.00,2.00,1.00,4.00};
char gr,gd[5]={'A','B','C','D','F'};
for(i=0;i<4;i++)
{
start:
cout<<"\n---------------------------------------------------";
cout<<"\nEnter grade for Subject "<<(i+1)<<"(A/B/C/D/F):";
cin>>gr;
if(gr==gd[0])p=g[0];
else if (gr==gd[1])p=g[1];
else if (gr==gd[2])p=g[2];
else if (gr==gd[3])p=g[3];
else if (gr==gd[4])p=g[4];
else
{
cout<<"Invalid!";
goto start;
}
cout<<"-->Point for Subject "<<(i+1)<<":"<<p;
total=total+p;
}
cout<<"\nTotal grades point for all subjects is "<<total;


return 0;
}
Topic archived. No new replies allowed.