Discount C++ program using a switch statement

Hello there, so basically im trying to do a small program that let the user enter a value then if the value equal or greater than x make discount something like 12%
here is an example
1
2
3
4
15% discount, if sales are greater than or equal to 1000
10% discount, if sales are greater than or equal to 500
5% discount, if sales are greater than or equal to 250
0, otherwise.


i know how to do it using if statement, but in switch i have no idea, there could be million cases about the entered value, so i need help if its possible

Thanks
switch expects an exact number to compare like ==, so you cannot use it for values >=.

Why do you want to use switch?

It is limited in its use but might be a little bit faster than the according if(...) else if(...) ... cascade. But the gain is not overwhelming. So it's not worth any 'acrobatics' just for having switch.
is this a cheesy example or your actual problem? It looks like percent can be directly computed here with some math equation, but if this problem is just a quick example, that may not help.

regardless, you can chain ifs or convert it to a small set of cases.
eg if you can process sales into a number 0,1,2,3 or 1,2,3,4 etc with a simple equation you can use switch or lookup table.

the brute force way is
pct = 0;
pct += (sales>=1000)*15+ (sales >=500 && sales <1000)*10 + ....
this exploits the fact that comparisons become 0 or 1.
Last edited on
my professor asked us to do an assignment as the following picture https://imgur.com/G6Q3HOT im just trying to figure it out if it was possible
Last edited on
Given the current situation, it's a rather tacky question, with a not-very-good strategy for solving. I should go back to your lecturer and ask whether "switch" was intended as the actual c++ statement of that name or as a somewhat misleading indication of a choice of options (if ... else if ... else, or, with more generality, looping through parallel arrays).

It makes no sense to use a c++ switch statement here. That is for a small number of discrete options.
not every assignment makes sense :(

int swkey = sales /250; //0-250 gives 0. 250-499 gives 1. 500-749 gives 2. 750-999 gives 3. 1000+ gives 4,5,6.... forever.
swkey = min(4, swkey); // it can't be bigger than 4.
switch (swkey)
{
case 4:
code;
break;

case 3: //do you understand what this does?
case 2:
code
break;

case 1:
code;
break;

default:
code;
}
OR, you can default it to the 1000+ case and have zero be a case too. then you would not need to force the key to be 4 or less. Why not try it that way for fun?

Remember: this works because the question has nice, easy to work with numbers. If the cutoffs were not easily related, its a chore to force fit a switch here.
Last edited on
thats right jonnis
we can do something like this
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
//sales>=500, discount = 15%...

//sales>=300, discount = 10%...[300..499]...300/100=3, 301/100=3,399/100=3,410/100=410

//470/100=4,499/100=4

//sales>=100, discount = 5%...[100....299]...100/100=1, 121/100=1, 199/100=1, 

//280/100=2, 299/100=2..
//otherwise, discount = 0%...[1-99]...1/100=0, 50/100=0, 99/100=0...

//required_value_to_be_paid = sales - sales*discount
// 1000-1000*0.15 = 1000-150 = 850, 400-400*0.1 = 400-40 = 360


#include<iostream>

using namespace std;


int
main ()
{

  float sales;

  cout << "Please enter your sales value\n";

  cin >> sales;

  switch (int (sales) / 100)

    {

    case 3:

    case 4:
      cout << "You got a discount of 10%\n";
      break;

    case 1:

    case 2:
      cout << "You got a discount of 5%\n";
      break;

    case 0:
      cout << "Your discount is 0%\n";
      break;

    default:
      cout << "You got a discount of 15%\n";
      break;

    }

  return 0;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main()
{
   int trigger[] = { 250, 500, 1000 };
   int value  [] = {   5,  10,   15 };
   int N = sizeof value / sizeof value[0];

   int sales;
   cout << "Enter sales: ";   cin >> sales;

   int discount = 0;
   for ( int i = 0; i < N && sales >= trigger[i]; i++ ) discount = value[i];

   cout << "Discount is " << discount << "%\n";
}



1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;

int main()
{
   int sales;
   cout << "Enter sales: ";   cin >> sales;
   cout << "Discount is " << 5 * ( ( sales >= 250 ) + ( sales >= 500 ) + ( sales >= 1000 ) ) << "%\n";
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main()
{
   int sales;   
   cout << "Enter sales: ";   cin >> sales;
   int category = ( sales >= 250 ) + ( sales >= 500 ) + ( sales >= 1000 );
   int discount = 0;
   switch( category )
   {
      case 3: discount += 5;
      case 2: discount += 5;
      case 1: discount += 5;
   }
   cout << "Discount is " << discount << "%\n";
}
Last edited on
Topic archived. No new replies allowed.