If else statement

Pages: 12
Jun 21, 2019 at 2:59am
@Niccolo,

That worked. Maybe its the lack of sleep. When I was playing around with it. I just saved my ideas on notepad so I could paste it and try it out but I didn't try it with the '==' .I may have more questions after I finish up this part of the code. I definitely want to continue with this community. I want learn as much as possible. Now if I can just figure at a different time how to install code blocks on linux. lol
Last edited on Jun 21, 2019 at 3:00am
Jun 21, 2019 at 3:02am
good night fellow programmers! I will keep you posted.
Jun 21, 2019 at 3:03am
'night 'saw
Jun 21, 2019 at 11:11am
Ugh, I'm awake. I just need to add one thing in this code and I should be good. I hope. My least favorite part I have to do is make the flowchart. I hate those.
Jun 21, 2019 at 11:21am
My next piece I need to figure out is something like this, but I don't know where to stick it.

1
2
3
4
5
6
7
8
9
10


if (saving > 30)// no matter what reward level ,you can't have more than 30 for savings
{
saving = 30;
cout<<"You saved: $";
cout<<showpoint<<fixed<<setprecision(2)<<saving<<endl; 
}

Last edited on Jun 21, 2019 at 12:31pm
Jun 21, 2019 at 3:39pm
put it last. do everything else and then check if its too big and put a cap on it at that time.
Jun 21, 2019 at 4:38pm
cap?
Jun 22, 2019 at 8:18pm
In each of the options from your previous code example, savings is calculated with something like:

1
2
3
    saving = receiptAmt * sDiscountAdd;
    cout<<"You saved: $";
    cout<<showpoint<<fixed<<setprecision(2)<<saving<<endl;


This is repeated in each of the clauses, but where the parameter "sDiscountAdd" is chosen differently, they're all otherwise the same. So, this is a typical pattern where a function is used to homogenize a control over the "saving" parameter, and it subsequently simplifies your code.

I assume floats, but use whatever "savings" really is (a double maybe, I'm not sure).

1
2
3
4
5
6
7
8
9
10
11
float calc_saving( float receiptAmt, float discount )
{
float s = receiptAmt * discount;

if ( s > 30 ) s = 30;

   cout<<"You saved: $";
    cout<<showpoint<<fixed<<setprecision(2)<<s<<endl;

return s;
}



Now, where each clause currently says something like the first example in this post, it can read:

saving = calc_saving( receiptAmt, sDiscountAdd );

Thus, the repeated code is now no longer repeated (all those couts in each clause), and the selection according to each case is handled by the parameter, which wraps the concept of "limiting" the value of saving into a function.
Last edited on Jun 22, 2019 at 8:20pm
Jun 22, 2019 at 10:27pm
wow, my mind is blown. so i would have to introduce a new variable calc_saving and maybe discount too? I would assume I add this last after all the other if else statements. Please forgive me for not getting it right away. lol.
Jun 22, 2019 at 11:14pm
calc_saving is a function in my post, not a variable.

Functions are the heart of programming. Thousands of them are likely in a single program.

You have discount already, several version in the code you posted. Examine the code you last took from here that worked. Read and notice everywhere that you already have the calculation for saving. It is a patter repeated 4 times in your code, once for each possible option in the "if/else" logic. Each one you used is different, apparently for the given circumstance.

The function takes whichever is appropriate, and REPLACES all of the repeated code. Read what I posted above carefully and notice what is similar about each "if/else" clause and that function to figure out what it replaces.
Jun 22, 2019 at 11:26pm
Makes sense. These are my variables

1
2
3
4
5
6
7
8
9
10
11
12

char rewardLevel;
double receiptAmt;
double Gdiscount = .12;//Gold discount
double gDiscountAdd = .035;//Gold discount when you spend more than 200
double Sdiscount  = .08;//silver discount
double sDiscountAdd = .0225;//silver discount when you spend more than 200
double Bdiscount = .04;//bronze discount
double bDiscountAdd = .0175;//bronze discount when you spend more than 200
string salesDiscount;//discount in percentage
double saving;//how much savings the member receives when they made a purchase



My if statemtents for like before

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
if (rewardLevel == 'g' || rewardLevel == 'G')//checks to see if the user entered reward level gold. to move on to gold discount
{
 if ( receiptAmt > 200)//if the user spends more than 200, it moves them into a different discount bracket.
 {
  saving = receiptAmt * gDiscountAdd;//calculates how much the member saved
   cout<<"You saved: $";
    cout << showpoint<< fixed<< setprecision(2)<<saving<<endl;

 }
 else
 {
  saving = receiptAmt * Gdiscount;//calculates how much the member saved
    cout<<"You saved: $";
    cout << showpoint<< fixed<< setprecision(2)<<saving<<endl;
 }

}
if (rewardLevel == 's' || rewardLevel == 'S' )
{
  if (receiptAmt > 200)
  {
    saving = receiptAmt * sDiscountAdd;
    cout<<"You saved: $";
    cout<<showpoint<<fixed<<setprecision(2)<<saving<<endl;
  }

  else
  {
     saving = receiptAmt * Sdiscount;
     cout<<"You saved: $";
     cout<<showpoint<<fixed<<setprecision(2)<<saving<<endl;
  }

}
if (rewardLevel == 'b' || rewardLevel == 'B')
{
    if (receiptAmt >200)
    {
        saving = receiptAmt * bDiscountAdd;
        cout<<"You saved: $";
        cout<<showpoint<<fixed<<setprecision(2)<<saving<<endl;
    }
    else
    {
        saving = receiptAmt * Bdiscount;
        cout<<"You saved: $";
        cout<<showpoint<<fixed<<setprecision(2)<<saving<<endl;
    }
}



each little discount has its own level. so do the calc function in each part were it has saving = ? The code you recommended where would that go exactly?
Jun 23, 2019 at 12:08am
I got it!
Jun 23, 2019 at 12:13am
:) Good thing I was out at the store then ;)
Jun 23, 2019 at 12:18am
@Niccolo, I greatly appreciate you and everyone's help here. I do hope I can pm you to pick your brain. I would say I would help in return, but I'm new to this, so I'm not much use. lol.
Jun 24, 2019 at 4:22pm
Cjigsaw wrote:
I do hope I can pm you to pick your brain.

Why would you want to do that? Why would you want to limit the number of people who could give you help to 1, rather than posting publicly so that you can get help from more than 1 person.
Topic archived. No new replies allowed.
Pages: 12