Having trouble.
Whats the difference between "if" and "else if"?
If I'm going to make a "nested if statement" does it affect the usage of "if" and "else if"?
cause here in my code(not yet finished):
(I'm trying to make a program that will determine your zodiac sign)
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
|
#include <iostream>
using namespace std;
int main ()
{
char a,b,c,d,e,f,g,h,i,j,k,l,m;
int n;
cout<<"Enter Your Birth Month. Press "<<endl;
cout<<"a= January"<<endl;
cout<<"b= February"<<endl;
cout<<"c= March"<<endl;
cout<<"d= April"<<endl;
cout<<"e= May"<<endl;
cout<<"f= June"<<endl;
cout<<"g= July"<<endl;
cout<<"h= August"<<endl;
cout<<"i= September"<<endl;
cout<<"j= October"<<endl;
cout<<"k= November"<<endl;
cout<<"l= December"<<endl;
cin>>m;
cout<<"Enter your Birth Date (1-31)"<<endl;
cin>>n;
if (m=='a')
{
if ((n<=19)&&(n>=1))
cout<<"Your zodiac sign is Capricorn.";
}
else if (m=='l')
{
if ((n<=31)&&(n>=22))
cout<<"Your zodiac sign is Capricorn.";
}
else if (m=='a')
{
if ((n<=31)&&(n>=20))
cout<<"Your zodiac sign is Aquarius.";
}
else if (m=='b')
{
if ((n<=18)&&(n>=1))
cout<<"Your zodiac sign is Aquarius.";
}
else if (m=='b')
{
if ((n<=29)&&(n>=19))
{
cout<<"Your zodiac sign is Pisces.";
}
}
return 0;
}
|
I'm having trouble with the underlined statement above.
If the statement will remain like that, the "Aquarius" word will not be printed out.
But if I'm going to remove "else" in
"else if (m=='a')", the "Aquarius" word will be printed out.
Why is that happening?
Does that mean that "if" and "else if" must be alternately written?
And if I'm going to write "else if" consecutively,then it means it is considered wrong?
Help pls..