switchnotworking

im in a c++ class and i cant figure out why one of my assignments doesnt work. more specifically the switch doesnt work. heres my code

#include <iostream>
using namespace std;
const double pencil = .3, pen = 1.25, eraser = .72, d1 = .1, d2 = .07, d3 = .05;
void main()
{
int selection, purchasing;
double cost;
cout << "What of the following are you purchasing:" << endl;
cout << "Choose from the following:" << endl;
cout << "A. Pens" << endl;
cout << "B. Pencils" << endl;
cout << "C. Erasers" << endl;
cout << "Enter your selection: ";
cin >> selection;
cout << endl << endl;
switch (selection)
{
case 'A':
cout << "How many are you purchasing: ";
cin >> purchasing;
if (purchasing >= 150)
{
cost = (pen * purchasing) * d1;
cout << "The cost is $" << cost;
}
else if (purchasing < 150 && purchasing >= 100)
{
cost = (pen * purchasing) * d2;
cout << "The cost is $" << cost;
}
else
{
cost = (pen * purchasing) * d3;
cout << "The cost is $" << cost;
}
case 'B':
cout << "How many are you purchasing: ";
cin >> purchasing;
if (purchasing >= 150)
{
cost = (pencil * purchasing) * d1;
cout << "The cost is $" << cost;
}
else if (purchasing < 150 && purchasing >= 100)
{
cost = (pencil * purchasing) * d2;
cout << "The cost is $" << cost;
}
else
{
cost = (pencil * purchasing) * d3;
cout << "The cost is $" << cost;
}
case 'C':
cout << "How many are you purchasing: ";
cin >> purchasing;
if (purchasing >= 150)
{
cost = (eraser * purchasing) * d1;
cout << "The cost is $" << cost;
}
else if (purchasing < 150 && purchasing >= 100)
{
cost = (eraser * purchasing) * d2;
cout << "The cost is $" << cost;
}
else
{
cost = (eraser * purchasing) * d3;
cout << "The cost is $" << cost;
}
default:
cout << "Invalid selection!!";
}
}
1. Variable 'selection' should not be of int type
2. put break; statement where it's needed
ref: http://www.cplusplus.com/doc/tutorial/control/

p/s use code tags, and int main()
You need a break after each case. Ex:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
switch (var)
{
     case 1:
          whatever;
     break;
     case 2:
          whatever2;
     break;
     case 3:
          whatever3;
     break;
     default:
          whatever4;
     break;
}
In addition to what matsom said about tags, if you recieve compiler errors, please post those as well.
Topic archived. No new replies allowed.