Case Switching for Pricing

I have an assignment, I can't seem to figure how get it going.
Basically allows the user to input the amount of pens they want
to order and in accordance with the chart, the sales price changes.

// week 2 ilab 1 - pen pricer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int price = 0.0;
int productAmount = 0;

// input data
cout << "Wholesale Pen Pricer";
cout << "________________.___________________\n";
cout << "| Amount | Price |\n";
cout << "| 0-50 | .50 cents |\n";
cout << "| 51-100 | .48 cents |\n";
cout << "| 101-499 | .45 cents |\n";
cout << "| 500+ | .42 cents |\n";
cout << "|_______________|___________________|\n";
//Start function
cout << "Please enter the amount you wish to order.\n";
cin >> productAmount;
// pricing
switch (productAmount) {
case ( > 0 || = 50):;
price = .50;
break;
case ( =>50 || =< 100):;
price = .48;
break;
case ( >= 101 || <= 499):;
price = .45;
break;
case ( >= 500):;
price = .42;
break;

default: ( < 0);
cout << "Error - Please enter a valid amount";
break;
// Final Price

cout << "You have chosen "<< productAmount <<" pens.";
cout <<" at "<< price <<".";
cout << "Your total comes up to: $" << productAmount * price;
}
system("PAUSE");
return 0;
}
int price = 0.0;

You can't store fractions in an int. Use a double instead.

Your switch case is very wrong, look here to see how to do it:
http://www.cplusplus.com/doc/tutorial/control/#switch
How's this one?

// week 2 ilab 1 - pen pricer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>


using namespace std;
namespace circle{
enum priceOptions {price1, price2, price3, price4};
}
int _tmain(int argc, _TCHAR* argv[])
{
double price = 0.0;
int productAmount = 0;

// input data
cout << "Wholesale Pen Pricer\n";
cout << "________________.___________________\n";
cout << "| Amount | Price |\n";
cout << "| 0-50 | .50 cents |\n";
cout << "| 51-100 | .48 cents |\n";
cout << "| 101-499 | .45 cents |\n";
cout << "| 500+ | .42 cents |\n";
cout << "|_______________|___________________|\n";
//Start function
cout << "Please enter the amount you wish to order.\n";
cin >> productAmount;
// pricing calculations
struct circleType {double price1; double price2; double price3; double price4;};
circleType myCircle;
myCircle.price1 = productAmount * .50;
myCircle.price2 = productAmount * .48;
myCircle.price3 = productAmount * .45;
myCircle.price4 = productAmount * .42;

if (productAmount >0||productAmount<51)
price = myCircle.price1;
if (productAmount >50||productAmount<101)
price = myCircle.price2;
if (productAmount>100||productAmount<500)
price = myCircle.price3;
if (productAmount > 499)
price = myCircle.price4;

// Final Price/Math

cout << "You have chosen "<< productAmount <<" pens.";
cout <<" At "<< price <<"cents each.";
cout << "Your total comes up to: $" << productAmount * price;

system("PAUSE");
return 0;
}
1
2
3
4
5
6
struct circleType {double price1; double price2; double price3; double price4;};
circleType myCircle;
myCircle.price1 = productAmount * .50;
myCircle.price2 = productAmount * .48;
myCircle.price3 = productAmount * .45;
myCircle.price4 = productAmount * .42;


What exactly is this doing, besides making the code more obscure? Just put the calculations in the if statements.
Topic archived. No new replies allowed.