If else statement

Pages: 12
I'm struggling with a code and I'm using if/else statement. I'm extremely need to coding here so bear with me and thank for any help. When I add a formula in my if statement it works, but when it comes to the next condition it takes the first formula and the next condition's formula together.

[code

}[/code]
@Cjigsaw, try posting the code again by editing your post.
Add the code.

I think you mean it's doing what's in both if statements?

If you don't use "else" then it'll check both if statement conditions to run:

1
2
3
4
5
6
7
8
9
10
11
int main()
{
	if (1 > 0) //Will Check This Condition
	{
		std::cout << "WOW!";
	} 
	if (1 < 2) //Will Check This Condition
	{
		std::cout << "REALLY?!";
	}
}


VS

1
2
3
4
5
6
7
8
9
10
11
int main()
{
	if (1 > 0) //Will Check This Condition
	{
		std::cout << "WOW!";
	} 
	else if (1 < 2) //Will Check This Condition ONLY If The Previous If Statement's Condition Was False
	{
		std::cout << "REALLY?!";
	}
}
I'm going to keep plugging. Thats exactly whats its doing. It's adding both of the conditions. Sorry I didn't explain it very well. Question, can you have more than one thing in your condition ? for example in the if statement if(1 > 0). Also do you stupid question, do you always have to end an if statement with else or can it be else if?
Yes, you can have multiple conditions like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
        int check = 10;
	if (1 > 0 && 1 > -12)
	{
		std::cout << "WOW!";
	}

        else if (check > 2 && check < 100)
        {
              std::cout << "NO!"; 
        }
}


I can't really see what the issue is unless you show me your code.
Last edited on
ok thanks for the help. I have been googling and looking for help for my homework assignment. I have been doing a lot of rework lol. I don't expect to just get it off the bat. If you just point in me in the right direction. I don't expect any to just do it for me.
I don't know if its consider bad to post my assignment here either. lol. However the just of if is this:


if (condition)
{


}
else
{

}
//this part works perfect but when i do this


if(condition)//it starts adding part of the condition above
{

}
else
{

}
Make the code you just posted a small, separate experiment. Compile and test it.

See if you find it fails to perform as expected.

Then, yes, POST ALL OF YOUR ACTUAL CODE.

We can't possibly help without that.

99.999999999999999999999999999999999999999999999999999999999999% of the time, the computer does exactly what is written.

It is almost always that the writer does something curious that changes everything about the conditions actually being tested.

People will tell us, and we've seen this for years (me, I've seen this for decades, several of them) - they swear, "THIS is all I did. I promise, that's it. Nothing else, This is it."

Hours later, with dozens of posts later, when we FINALLY see what they actually wrote, it was not true.

WE notice some small thing they simply didn't understand, and NOTHING they promised or exemplified was true.

Show us what you actually wrote. You're not aware of everything to describe it. If you did, you'd probably notice the problem yourself.
Last edited on
I think I understand. You're making and if/else pair and thinking it wont evaluate the next if/else pair?

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
int main()
{
	bool condition = false;

	if (condition) //False
	{


	}
	else //Will Do this
	{

	}


	if (condition) //False
	{

	}
	else //Will Do This
	{

	}

}


VS

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
int main()
{
	bool condition = false;

	if (condition) //False
	{


	}

	else if (condition) //False
	{

	}

	else if (condition) //False
	{

	}

	else //Will Do This
	{

	}
}


if/else will work like this:

The First if statement WILL be evaluated. You can then put and "else" afterwards which is the same as "if that was wrong, then do this". Once the else is done, that's it. The next if statement seen WILL be evaluated.

If you don't want it to be evaluated, you need to make it an else/if combo 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
int main()
{
	bool condition = false;

	if (condition) //False
	{


	}

	else if (!condition) //True!
	{

	}

	else if (condition) //Now This One Will Be Skipped
	{

	}

	else //And This One Will Be Skipped
	{

	}

	if (condition) //THIS One Is Seperate and WILL BE EVALUATED!
	{

	}
}


Putting an "if" statement infront of an "else" statement makes it so that if statement (Which usually would be INDEPENDENT and EVALUATED) will only be evaluated if the previous if statement came out to be false.

Posting the whole assignment wont get you far, maybe some general advice.

If I still didn't get what you mean right, then please upload the relevant code (doesn't have to be all of it in this case). And use code tags around your code, its the <> formatting button on the right of the dialog box. Highlight your code and press it.
if ((rewardLevel = 'g' || 'G') && (receiptAmt > 200))
{
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' || 'S') && (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;
}
// I know its a mess and not exactly fully completed either. I'm just working little by little
its hard to get help without giving the answer lol. I have been working on this for days now. I don't know how to give an example of my code really. If you can give me hints that would be good enough to put in the right direction.
@Cjigsaw,

This helps more.....but first, use code tags, they work like this:


[code]

// all your code here

[/code]


That said, here's the first problem:

if ((rewardLevel = 'g' || 'G') && (receiptAmt > 200))

Specifically, this does not check rewardLevel for a lower OR upper case 'G'.

That has to be written like this:

if ((rewardLevel == 'g' || rewardLevel == 'G') && (receiptAmt > 200))


In your version, you use "=" not "==".

"=" is assigment, you actually assigned rewardLevel to 'g', not tested it.

the "==" is evaluation, a comparison for equality. THAT will test for 'g'....

But then, you MUST test again, not just a dangling 'G' by itself.

Think with that, change your code and come back if that helps.


See....I KNEW FULL WELL we HAD TO HAVE the ACTUAL CODE to SEE THE FACTS.

Facts you simply couldn't recognize without help.


@zapshe,

You were working on a reasonable line of thinking, but.....just not what the OP was actually writing.
Last edited on
[code]

if ((rewardLevel == 'g' || rewardLevel == 'G') && (receiptAmt > 200))
{
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') && (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;
}




[/cpde]
@Cjigsaw, how did that change things?

@moralito00, when you have questions start a new thread and you'll likely get help.
Last edited on
It stayed the same. It seems so easy but yet I can't figure it out.


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
if ((rewardLevel == 'g' || rewardLevel == 'G') && (receiptAmt > 200))
{
 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') && (receiptAmt > 200))
{
    saving = receiptAmt * sDiscountAdd;
    cout<<"You saved: $";
    cout<<showpoint<<fixed<<setprecision(2)<<saving<<endl;
}

 else //it adds the else statement above
 {
     saving = receiptAmt * Sdiscount;
     cout<<"You saved: $";
     cout<<showpoint<<fixed<<setprecision(2)<<saving<<endl;
 }



Let's be clear here.


Consider:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if ( condition1 )
{
 // condition1 was true
}
else
{
// condition1 was false
}

if ( condition2 )
{
 // condition2 was true
}
else
{
 // condition2 was false
}


It does not matter whether condition1 was false or not, if condition2 was false, then the block "condition2 was false" will be executed.

That's what is written.

What would you expect instead?


Now, is THIS what you're expecting?

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
if (rewardLevel == 'g' || rewardLevel == 'G')
{ 
 if ( receiptAmt > 200 )
 {
  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 //it adds the else statement above
  {
     saving = receiptAmt * Sdiscount;
     cout<<"You saved: $";
     cout<<showpoint<<fixed<<setprecision(2)<<saving<<endl;
  }
}
Last edited on
So the issue is that it find BOTH "if" statements to be False?

You say on line 22 that it "adds" to the else statement, does that mean the code is going over Both Else statements? If so then both if statements are evaluating to false.

I suggest before this piece of code you "cout" the variable "rewardLevel" to see what it actually is.
see, Niccolo, I tried that too and go the same issue. zapshe, what do you mean? lol, I'm starting to think, I'm not much of a programmer. lol.
@zapshe, this whole thing boils down to a "else" condition that should be separated from the "rewardLevel" condition.

If reward were "g", nothing in "s" should execute.

Yet, it must as an else as fashioned.

That "else" should only apply to the amount > 200, blocked entirely if "S" was not true.

I was editing while you posted, an example of the refactor.
Last edited on
@Cjigaw....


You say you tried it, but WHEN?

Until you changed the tests from "=" to "==" you had nothing valid.

Try what I posted.
I'm going to make rest on it. I haven't slept yet and I saw the sun rise and everything. Hopefully I can wake up in a few hours with a fresh mind. I do appreciate the help. You guys rock! Would it help if I send the while code? See, I figured if I could fix this part, the rest would be smooth sailing. I don't know it seemed so easy, but boy I was mistaken. lol. Coding makes you think like you never have before lol.
Pages: 12