If/Else/Else If/or ?

Ok so I'm making a program that I will present for the finals tomorrow and I need to finish this asap. So its pretty basic but, still I'm a beginner.
So here's my question, what should i do if i want my program to run only 1 if statement only. So else if is obviously not working because both of my condition is true right? So what should i do?


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
  #include<iostream>
#include<string> //string functions //stores string into a variable
using namespace std;
int main ()
{
	cout<<"Sari-Sari Store"<<endl<<endl;
	
	string products;
	cout<<"Choose a product: \nCandies\nJunk Foods\nSoft Drinks\nJuice\nShampoo\nSoap"<<endl<<endl;
	cout<<"Enter your choice: "<<endl;
	cin>>products;
	
	string candies;
	if (products == "Candies");
	{
		cout<<endl;
		cout<<"Candies: \nSnow Bear =   Php 1.00\nJudge =       Php 1.00\nV-Fresh =     Php 1.00\nDouble Mint = Php 1.00\nMentos =      Php 3.00"<<endl<<endl;
		cout<<"Enter your choice: ";
		cin>>candies;	
	}
	
	string junkfoods;
	if (products == "Junk Foods");
	{
		cout<<endl;
		cout<<"Junk Foods: \nClover =  Php 7.00\nPiaotos = Php 13.00\nNova =    Php 13.00\nXsakto =  Php 1.00 \nVcut =    Php 13.00"<<endl<<endl;
		cout<<"Enter your choice: ";
		cin>>junkfoods;
	}
}
kramsuiluj wrote:
obviously not working because both of my condition is true right?


Wrong.

products isn't Schrodinger's cat: it can't be both "Candies" and "Junk Foods".


BTW, you won't be able to input a multi-word product with cin >> since it will finish at the first space. Use getline( cin, products); instead.
How could both of your if conditions be true at the same time? I don't follow.

Also, because "Junk Foods" and "Soft Drinks" are more than one word, you shouldn't use cin >> here.
You should use getline.

1
2
3
4
5
6
7
8
9
10
11
12
13
std::getline(std::cin, products);

if (products == "Candies")
{
    // Logic for candies
}
else if (products == "Junk Foods")
{
    // Logic for junk foods
    std::string junkfoods;
    std::cin.ignore(); // needed to remove remaining '\n' in buffer
    std::cin >> junkfoods;
}
Last edited on
unrelated but conditions are easier to manage and read etc if you 'factor' a bit.
both start with an endl ... move it out of both. Both read a string variable. Unless you can show that you need 2 strings, just read one. And you can factor out the read statement to after the conditions. see?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	cout<<endl;
	string input;

	if (products == "Candies");
	{	
		cout<<"Candies: \nSnow Bear =   Php 1.00\nJudge =       Php 1.00\nV-Fresh =     Php 1.00\nDouble Mint = Php 1.00\nMentos =      Php 3.00"<<endl<<endl;
	}

     else if (products == "Junk Foods"); //added else will skip the expensive string compare if it cant be true (because the previous one was true).  
	{
		cout<<"Junk Foods: \nClover =  Php 7.00\nPiaotos = Php 13.00\nNova =    Php 13.00\nXsakto =  Php 1.00 \nVcut =    Php 13.00"<<endl<<endl;
	}

		cout<<"Enter your choice: ";
		cin>>input;



it could be that you are about to add more code that makes these changes no good, or that you really needed 2 distinct input strings. Its your design, and I may be wrong, but think about it.
Last edited on
Overall, something is either true or false:
1
2
3
4
if ( condA )
  // condA was true
else
  // condA was false 


When you have independent if-clauses:
1
2
3
4
5
if ( condA )
  // condA was true

if ( condB )
  // condB was true 

the second if-statement does not care about the first if-statement.
Program has to test both conditions.

The 'products' IS-A std::string and strings cannot be "foo" and "bar" simultaneously. They can be either "foo" or "bar" or something else.

One could have:
1
2
3
4
5
6
7
if ( products == "foo" ) {
  products = "bar";
}

if ( products == "bar" ) {
  // something
}

in which the body of the first if-statement affects the condition of the following if-statement. They are no longer entirely independent. You don't have that.

Since you do know that 'products' cannot be simultaneously "foo" and "bar", you can optimize and use the if .. else:
1
2
3
4
5
6
if ( products == "foo" ) {
  // something
}
else if ( products == "bar" ) {
  // another
}

Now the second test is skipped, if products is "foo". Lazy evaluation.


Why do you have both string candies; and std::string junkfoods;?
at most one of them will get "value" due to your conditions.

For that, a single variable would be enough:
1
2
3
4
5
6
7
8
9
10
11
12
string purchase;
cout<<endl;
if (products == "Candies")
{
  cout<<"Candies: \nSnow Bear =   Php 1.00\nJudge =       Php 1.00"<<endl<<endl;
}
else if (products == "Junk Foods")
{
  cout<<"Junk Foods: \nClover =  Php 7.00\nPiaotos = Php 13.00"<<endl<<endl;
}
cout<<"Enter your choice: ";
cin>>purchase;

I presume there is "price" too, but you did not show it's handling in your example.


One more thing:
Pay attention to the semicolon:
1
2
3
4
5
6
7
8
9
10
11
12
13
if (products == "Candies");
{
  cin>>candies;	
}

// means same as

if (products == "Candies")
{}

{
  cin>>candies;	
}

In other words, no matter what the value of products is, the cin>>candies; will always execute. It is not in the if's body.
Thankyousomuch for all of your help :D
I read your comments and it really helped !
Really appreciate it ! Thanks
Im done with my program

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include<iostream>
#include<string>

using namespace std;

int main ()
{
	int amount = 0; 
	int num = 0;
	cout<<"Mini Sari-Sari Store"<<endl<<endl;
	string products;
	cout<<"Products: \n Candies\n Junk Foods\n Soft Drinks"<<endl<<endl;
	cout<<"Enter your choice: ";
	getline(cin,products);
	cout<<endl;
	

	
	
	
	string candies;
	if (products == "Candies") // CANDIES
	{
		cout<<"Candies: \n Snowbear =   Php 1.00\n Mentos =     Php 3.00\n Judge =      Php 1.00\n DoubleMint = Php 3.00"<<endl<<endl;
		cout<<"Enter your choice: ";
		getline(cin,candies);
		cout<<"How many: ";
		cin>>num;
		cout<<endl<<endl;
		cout<<"Enter the amount of your money: ";
		cin>>amount;
		
		int Snowbear = 1;
		int Mentos = 3;
		int Judge = 1;
		int DoubleMint = 3;
		
		if (candies == "Snowbear")
		{
			cout<<"Change: Php";
			cout<<amount-(Snowbear*num)<<".00";
			cout<<endl;
			cout<<endl;
		}
		else if (candies == "Mentos")
		{
			cout<<"Change: Php ";
			cout<<amount-(Mentos*num)<<".00";
			cout<<endl;
			cout<<endl;
		}
		else if (candies == "Judge")
		{
			cout<<"Change: Php ";
			cout<<amount-(Judge*num)<<".00";
			cout<<endl;
			cout<<endl;
		}
		else if (candies == "DoubleMint")
		{
			cout<<"Change: Php ";
			cout<<amount-(DoubleMint*num)<<".00";
			cout<<endl;
			cout<<endl;
		}
		
			
	}
	
	string junkfoods;
	if (products == "Junk Foods") //JUNK FOODS
	{
		cout<<"Junk Foods: \n Piatos =     Php 14.00\n Clover =     Php 7.00\n Vcut =       Php 14.00\n Cracklings = Php 7.00\n Pillows =    Php 8.00"<<endl<<endl;
		cout<<"Enter your choice: ";
		getline(cin,junkfoods);
		cout<<"How many: ";
		cin>>num;
		cout<<endl<<endl;
		cout<<"Enter the amount of your money: ";
		cin>>amount;
		
		int Piatos = 14;
		int Clover = 7;
		int Vcut = 14;
		int Cracklings = 7;
		int Pillows = 8;
		
		if (junkfoods == "Piatos")
		{
			cout<<"Change: Php ";
			cout<<amount-(Piatos*num)<<".00";
			cout<<endl;
		}
		else if (junkfoods == "Clover")
		{
			cout<<"Change: Php ";
			cout<<amount-(Clover*num)<<".00";
			cout<<endl;
		}
		else if (junkfoods == "Vcut")
		{
			cout<<"Change: Php ";
			cout<<amount-(Vcut*num)<<".00";
			cout<<endl;
		}
		else if (junkfoods == "Cracklings")
		{
			cout<<"Change: Php ";
			cout<<amount-(Cracklings*num)<<".00";
			cout<<endl;
		}
		else if (junkfoods == "Pillows")
		{
			cout<<"Change: Php ";
			cout<<amount-(Pillows*num)<<".00";
			cout<<endl;
		}	
	
	}
	string softdrinks;
	if (products == "Soft Drinks") //SOFT DRINKS
	{
		cout<<"Soft Drinks: \n Coke = Php 15.00\n Royal = Php 15.00\n Pepsi = Php 15.00\n Sprite = Php 15.00\n MountainDew = Php 15.00"<<endl<<endl;
		cout<<"Enter your choice: ";
		getline(cin,softdrinks);
		cout<<"How many: ";
		cin>>num;
		cout<<endl<<endl;
		cout<<"Enter the amount of your money: ";
		cin>>amount;
		
		int Coke = 15;
		int Royal = 15;
		int Pepsi = 15;
		int Sprite = 15;
		int MountainDew = 15;
		
		if (softdrinks == "Coke")
		{
			cout<<"Change: Php ";
			cout<<amount-(Coke*num)<<".00";
			cout<<endl;
		}
		else if (softdrinks == "Royal")
		{
			cout<<"Change: Php ";
			cout<<amount-(Royal*num)<<".00";
			cout<<endl;
		}
		else if (softdrinks == "Pepsi")
		{
			cout<<"Change: Php ";
			cout<<amount-(Pepsi*num)<<".00";
			cout<<endl;
		}
		else if (softdrinks == "Sprite")
		{
			cout<<"Change: Php ";
			cout<<amount-(Sprite*num)<<".00";
			cout<<endl;
		}
		else if (softdrinks == "MountainDew")
		{
			cout<<"Change: Php ";
			cout<<amount-(MountainDew*num)<<".00";
			cout<<endl;
		}														
	}				

}
Last edited on
Topic archived. No new replies allowed.