Tips on making this work.

This program is supposed to give me the total entrance fee to an event. It asks how many people are in the group and it adjusts the price based on how many people are in the group. My problem is that if 11 or more people are in the group they are charged 60 dollars per person. But I don't have the knowledge on how to make a statement that reads from 11 or more people. Or should I make an "else" calculating "=>11 people do this". Any tips on how to do do this?

1 - 4 people = $100
5 - 10 people = $80
11 or more = $60

Here's what I have so far: (The output is in spanish.)


//Fee.cpp
//Creado/Revisado por <Jose Mercado> en <23 de noviembre de 2010>

#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::setprecision;
using std::ios;
using std::setiosflags;


int main()
{
int numReg = 0;
const double feePP14 = 100;
const double feePP510 = 80;
const double feePP11 = 60;
double totalFee = 0.0;

cout << "Entrar numero de personas: ";
cin >> numReg;


if (numReg < 0)
cout << "Error.";
else
{

cout << setiosflags(ios::fixed) << setprecision(2);

switch (numReg)
{
case 1: totalFee = feePP14 * numReg;
cout << "Total a pagar: $" << totalFee << endl;
break;
case 2: totalFee = feePP14 * numReg;
cout << "Total a pagar: $" << totalFee << endl;
break;
case 3: totalFee = feePP14 * numReg;
cout << "Total a pagar: $" << totalFee << endl;
break;
case 4: totalFee = feePP14 * numReg;
cout << "Total a pagar: $" << totalFee << endl;
break;
case 5: totalFee = feePP510 * numReg;
cout << "Total a pagar: $" << totalFee << endl;
break;
case 6: totalFee = feePP510 * numReg;
cout << "Total a pagar: $" << totalFee << endl;
break;
case 7: totalFee = feePP510 * numReg;
cout << "Total a pagar: $" << totalFee << endl;
break;
case 8: totalFee = feePP510 * numReg;
cout << "Total a pagar: $" << totalFee << endl;
break;
case 9: totalFee = feePP510 * numReg;
cout << "Total a pagar: $" << totalFee << endl;
break;
case 10: totalFee = feePP510 * numReg;
cout << "Total a pagar: $" << totalFee << endl;

}

}


return 0;

}



In your switch there are many repetitions. It would be better to use ifs:
1
2
3
4
if(numReg <= 4) totalFee = feePP14 * numReg;
else if(numReg <= 10) totalFee = feePP510 * numReg;
else totalFee = feePP11 * numReg;
cout << "Total a pagar: $" << totalFee << endl;
I agree but I have trouble using if and else statements cause I don't know how to align them properly. That's why I'm using switch. Can I put an if or else somewhere in the code that calculates the 11 or more people without drastically changing what I have now? If so where would I put it?

If you want to use switch, you can add a default: case. Though ifs would be better. You'll have to learn it at some point anyway..
closed account (z05DSL3A)
if..else if...else would definitely be the better choice.

With the switch code you could even move the repetitive code outside of the switch and make use of fall through:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
double feePP = 0. ;
switch (numReg)
{
    case 1: 
    case 2: 
    case 3: 
    case 4:  feePP = feePP14;  break;
    case 5: 
    case 6: 
    case 7: 
    case 8: 
    case 9: 
    case 10: feePP = feePP510; break;
    default: feePP = feePP11;  break;

}
totalFee = feePP * numReg;
cout << "Total a pagar: $" << totalFee << endl;


Edit:
you could even do it with two ifs
1
2
3
4
5
6
7
8
9
double feePP = feePP11;

if(numReg <= 10) 
    feePP = feePP510;
if(numReg <= 4) 
    feePP = feePP14;

totalFee = feePP * numReg;
cout << "Total a pagar: $" << totalFee << endl;
Last edited on
Thanks guys big help. I used if else at the beginning and finished it off with a default.
Topic archived. No new replies allowed.