Else if...

I love posting here because you guys teach me something everyday. Please help me! In the code below there is a else if and when you enter 9 it is suppose to say "error..." but instead shows "error.." and multiplies 9 by one of the doubles. Anyone have a suggestion?

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
 
#include <iostream>
using namespace std;  

double standard = 35;
double sale = 20;
double discount = 9;
double sweaters; 
int main()		

{	cout << "Sweater purchase for ......"<< endl;

cout << "Enter number of sweaters to purchase:"<< endl;
cin >> sweaters; 
//If Else
//Error Message for Negative Number Input
if (sweaters <= 0)
{  cout << " Error! Number of sweaters cannot be negative"<< endl;   }

else if (sweaters > 3)
{  standard; cout << " Cost of a single sweater: " <<standard <<endl;  
cout << " Cost of sweaters:" <<sweaters * standard << endl;   }


else if(sweaters <= 3)
{ sale; cout << " Cost of a single sweater: " <<sale <<endl;  
cout << " Cost of sweaters: " <<sweaters *sale << endl; }

else if(sweaters  < 6 )
{ discount; cout << " Cost of a single sweater: " <<sale <<endl;  
cout << " Cost of sweaters: " <<sweaters * discount << endl; }

else (sweaters < 9);
{  cout << " Error! Maximum number allowed to purchase is 9"<< endl;   }


	system ("PAUSE");
	
return 0;
}



I'm quite new to C++ myself, but I can try to help with this...

1
2
else (sweaters < 9);
{  cout << " Error! Maximum number allowed to purchase is 9"<< endl; 


If I recall corectly, an else does not consider the condition if its not paired as an else if. I also think your comparaison sign might be the wrong way around... So your last bit of code could be put in as so...

1
2
else if (sweaters > 9);
{  cout << " Error! Maximum number allowed to purchase is 9"<< endl;   }


So if you go through all the prior conditions, then it'll treat your last else...
Last edited on
HI
///instead of doing this

if (sweaters <= 0)
{ cout << " Error! Number of sweaters cannot be negative"<< endl; }


///DO THIS
if (sweaters <= 0)
{ cout << " Error! Number of sweaters cannot be negative"<< endl;

system("pause");
return(1);}
Last edited on
Thank you aLx450 and isaacthebro.


So now my code is.. But when I enter 9 it still multiplies and shows the error message. Is there a step I am missing or is my order mixed up?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
if (sweaters <= 0)
{ cout << " Error! Number of sweaters cannot be negative"<< endl; system("pause");
return(1);}

else if (sweaters < 3)
{  standard; cout << " Cost of a single sweater: " <<standard <<endl;  
cout << " Cost of sweaters:" <<sweaters * standard << endl;   }

else if(sweaters >= 3)
{ sale; cout << " Cost of a single sweater: " <<sale <<endl;  
cout << " Cost of sweaters: " <<sweaters *sale << endl; }

else if(sweaters >= 6)
{ discount; cout << " Cost of a single sweater: " <<discount <<endl;  
cout << " Cost of sweaters: " <<sweaters * discount << endl; }

else if (sweaters > 9);
{  cout << " Error! Maximum number allowed to purchase is 9"<< endl;   }

	system ("PAUSE");
	
return 0;
}
Last edited on
kiri,

First off take

system("PAUSE");

Out of there, you should not get used to using that. There is no need for it here.

Also, just take away the semicolon after

else if (sweaters > 9);

You should be fine after that.
Last edited on
I took the semicolon out, but I need the PAUSE because i am using windows and not Linux ( I believe) because every time I take it out and enter a number it just closes. Other than that I still have the issue. But thanks for the semicolon ( why did that make a difference?)
The semicolon basically tells the program to assess

else if (sweaters > 9);

Now the computer knows that sweaters are or are not over 9. But the computer doesn't care because that semicolon tells it to just assess that and do nothing with that information. When you took the semicolon away it told the computer to assess that code, and if it were to be true go on to whatever is in the brackets. It's basically a stop sign for the code.

Also, don't put system("PAUSE"), what are you using? I am assuming it's Visual Studio?
Duh, didnt even see that. Yeah, that should about fix it.

I like _getch(); to stop it since it'll "keep cycling" on next input (get character), so if its at the end it'll close your program when you press something... There are many more efficient ways to do it, but that's with what I started in the early stages :)

you need to add #include <conio.h> at the top, before your main() if you do so though!
Last edited on
Yes I am using Visual Studio. But why doesn't it execute the cout when it is more than 9?
Can I get a lil more info on _getch(); ( I am also searching and looking in my book, not seeing to much yet :-/ )
It should be executing the std::cout if it is more than 9.

Now, for your command prompt disappearing.

You see the title of your project just below the solution on the right side and above 'External Dependencies'?

Click on that, go to Properties>Linker>System>SubSystem

There will be a drop box where you can pick one of the possible choices, pick the one that says "Console (/SUBSYSTEM:CONSOLE)"

If the drop box does not have those choices just enter that in (without the quotes). All you have to do to compile and run your code (and keep the window open is press shift+f5.
Last edited on
Is the System Pause really a issue? stdout? I didn't find to much about that or how to approach it, same with on_getch. There isn't a way to do it with just if else statements?
My mistake I wrote stdout instead of std::cout. Those are two seperate things. You need to focus on std::cout.

It's not a big issue if you don't care what is printed out in your command prompt. In your case, you do want to see so it is an issue here.

You cannot pause the command prompt with if else statements if that's what you are saying.
Even with std::cout and with a input of 10 the output is

Sweater purchase for..
Enter number of sweaters to purchase: 10
Cost of a single sweater: 20
Cost of sweaters: 200
Press any Key to Continue...

when 10 * 9 = 90 ( sweaters * discount) and and it shouldn't even display that price because it is over 9 sweaters and should show the error message.
I tought your issue was with the final error message not displaying...

Read your second if else again.

1
2
3
else if (sweaters > 3)
{  standard; cout << " Cost of a single sweater: " <<standard <<endl;  
cout << " Cost of sweaters:" <<sweaters * standard << endl;   }


If you put in, lets say, 7 or even 27, that condition will be treated because your sweaters number IS in fact > 3

If you don't want to mess with your structure too much you could add something like

1
2
3
else if (sweaters > 3 && sweaters < 9)
{  standard; cout << " Cost of a single sweater: " <<standard <<endl;  
cout << " Cost of sweaters:" <<sweaters * standard << endl;   }


Same issue will arise with this

1
2
3
else if(sweaters  < 6 )
{ discount; cout << " Cost of a single sweater: " <<sale <<endl;  
cout << " Cost of sweaters: " <<sweaters * discount << endl; }


Since its a later condition, if you put in lets say 4, it'll treat the > 3 condition, keep going down the list and then execute the < 6 condition and everything that goes along with it.

As for _getch(), it tells your program to stop until the next input. Which, if put at the very end of your program, inside the closing main() bracket, will stop your program after displaying the appropriate cout depending on the amount of sweaters before it closes automatically if you press, lets say, enter.

Good luck!
Last edited on
aLx450 , Thank you!!!! All errors fixed.
You're welcome. Here's an important tip for you, one that I've learned to love and that'll save your life for longer projects...

Use the step-by-step debug of Visual Studio. Click to the left of a line of code, in the margin / scrollbar. It'll put a red dot stopper there. Run your program, it'll stop where the red dot is. Put VS in the forefront, press F10 repeatedly as it'll go from one action to another.

That way, you'll see where you're messing up, especially with multiple loops when you get to that. You also see all the values of your variables if you mouse-over them while doing this debug method.

Topic archived. No new replies allowed.