Hi.
Ive made this program using if statements i see no errors in it but i dont know my program isnt working on my if statements i mean the if statements are not used in the output. Can u plzz figure out the problem? It works only till scanf statement. #include<stdio.h>

#include<conio.h>
void main ()
{
int d,m;
clrscr();
printf ("Enter the date of your birth\t");
scanf("%d",& d);
printf("\nEnter the month of your birth\t");
scanf("%d",& m);
printf("\n%d-%d",d,m);
if (d>=21 && d<=19 && m==3 && m==4)
{
printf("\nYour star is ARIES");
}
if (d>=20 && d<=20 && m==4 && m==5)
{
printf("\nYour star is TAURUS");
}
if (d>=21 && d<=21 && m==5 && m==6)
{
printf("\nYour star is GEMMINI");
}
if (d>=22 && d<=22 && m==6 && m==7)
{
printf("\nYour star is CANCER");
}
if (d>=23 && d<=22 && m==7 && m==8)
{
printf("\nYour star is LEO");
}
if (d>=23 && d<=22 && m==8 && m==9)
{
printf("\nYour star is VIRGO");
}
if (d>=23 && d<=22 && m==9 && m==10)
{
printf("\nYour star is LIBRA");
}
if (d>=23 && d<=21 && m==10 && m==11)
{
printf("\nYour star is SCORPIO");
}
if (d>=22 && d<=21 && m==11 && m==12)
{
printf("\nYour star is SAGITTARIUS");
}
if (d>=22 && d<=19 && m==12 && m==1)
{
printf("\nYour star is CAPRICORN");
}
if (d>=20 && d<=18 && m==1 && m==2)
{
printf("\nYour star is AQUARIUS");
}
if (d>=19 && d<=20 && m==2 && m==3)
{
printf("\nYour star is PISCES");
}
getch();
}

Impossibility is playing a role in your distress. You could not have been born in two different months, nor could you have been born in a day that is earlier and later that a significant period, at the same time.

There's an order of precedent as well to take in consideration. Use () with && and || operators.

Rephrasing one of your if statements into english:

if (d>=21 && d<=19 && m==3 && m==4)

That reads: If the day of the month is both more than or equal to 21 and also less than or equal to 19 {this is impossibility one} and the month number is 3 and the month number is 4 {two}

What you want is something like:

If the month number is 3 and the day of the month is the 21st or later, OR the month number is 4 and the day of the month is the 19th or earlier

so you need to end up with a test that looks something like the following:

if ( ( ___ && ___ ) || ( ____ && ___ ) )

Rephrasing one of your if statements into english:

if (d>=21 && d<=19 && m==3 && m==4)

That reads: If the day of the month is both more than or equal to 21 and also less than or equal to 19 {this is impossibility one} and the month number is 3 and the month number is 4 {two}

What you want is something like:

If the month number is 3 and the day of the month is the 21st or later, OR the month number is 4 and the day of the month is the 19th or earlier

so you need to end up with a test that looks something like the following:

if ( ( ___ && ___ ) || ( ____ && ___ ) )

thx this thing worked but isnt it possible if i dont use the OR operator and i get the same result with &&????

What is the problem with using || ? It is a completely valid part of the C language. There might be some obscure way of doing the same comparison without it, but it would be useless to even try it.

AND and OR are 2 similar but different constructs.

month = 3 && month == 4
Is an impossibility, it will NEVER happen

month == 3 || month == 4
happens for months 3 and 4

Hello I modified your source code try the source code below:

#include<stdio.h>
int d,m;
main ()
{

clrscr();
printf ("Enter the date of your birth: ");
scanf("%d",& d);
printf("\nEnter the month of your birth: ");
scanf("%d",& m);
printf("\n%d-%d",d,m);

if ((d>=21 || d<=19) && (m==3 || m==4))
printf("\nYour star is ARIES");

else if ((d>=20 || d<=20) && (m==4 || m==5))
printf("\nYour star is TAURUS");

else if ((d>=21 || d<=21) && (m==5 || m==6))
printf("\nYour star is GEMMINI");

else if ((d>=22 || d<=22) && (m==6 || m==7))
printf("\nYour star is CANCER");

else if ((d>=23 || d<=22) && (m==7 || m==8))
printf("\nYour star is LEO");

else if ((d>=23 || d<=22) && (m==8 || m==9))
printf("\nYour star is VIRGO");

else if ((d>=23 || d<=22) && (m==9 || m==10))
printf("\nYour star is LIBRA");

else if ((d>=23 || d<=21) && (m==10 || m==11))
printf("\nYour star is SCORPIO");

else if ((d>=22 || d<=21) && (m==11 || m==12))
printf("\nYour star is SAGITTARIUS");

else if ((d>=22 || d<=19) && (m==12 || m==1))
printf("\nYour star is CAPRICORN");

else if ((d>=20 || d<=18) && (m==1 || m==2))
printf("\nYour star is AQUARIUS");

else if ((d>=19 || d<=20) && (m==2 || m==3))
printf("\nYour star is PISCES");

getch();
}

That's very pretty code, but it should have been posted using code tags.

When posting c code, please use c code tags
[code=c] /* Your code here */

[/code]

And logically, the if statements you have don't meet the needed criteria.

For example your first if: if ((d>=21 || d<=19) && (m==3 || m==4)) Is valid for the following dates:
3/1 to 3/19, 3/21 to 3/31, 4/1 to 4/19, and 4/21 to 4/30

(blatantly borrowed from Wikipedia)

Under the tropical zodiac, the Sun is in Aries roughly from March 21 to April 19, by definition beginning at vernal equinox.

yabuki

That's very pretty code, but it should have been posted using code tags.

When posting c code, please use c code tags
[code=c] /* Your code here */

[/code]

And logically, the if statements you have don't meet the needed criteria.

For example your first if: if ((d>=21 || d<=19) && (m==3 || m==4)) Is valid for the following dates:
3/1 to 3/19, 3/21 to 3/31, 4/1 to 4/19, and 4/21 to 4/30

(blatantly borrowed from Wikipedia)

Thanks
hey i dont know how to use the code tags can u plz help me with that too???

Hello I modified your source code try the source code below:

#include<stdio.h>
int d,m;
main ()
{

clrscr();
printf ("Enter the date of your birth: ");
scanf("%d",& d);
printf("\nEnter the month of your birth: ");
scanf("%d",& m);
printf("\n%d-%d",d,m);

if ((d>=21 || d<=19) && (m==3 || m==4))
printf("\nYour star is ARIES");

else if ((d>=20 || d<=20) && (m==4 || m==5))
printf("\nYour star is TAURUS");

else if ((d>=21 || d<=21) && (m==5 || m==6))
printf("\nYour star is GEMMINI");

else if ((d>=22 || d<=22) && (m==6 || m==7))
printf("\nYour star is CANCER");

else if ((d>=23 || d<=22) && (m==7 || m==8))
printf("\nYour star is LEO");

else if ((d>=23 || d<=22) && (m==8 || m==9))
printf("\nYour star is VIRGO");

else if ((d>=23 || d<=22) && (m==9 || m==10))
printf("\nYour star is LIBRA");

else if ((d>=23 || d<=21) && (m==10 || m==11))
printf("\nYour star is SCORPIO");

else if ((d>=22 || d<=21) && (m==11 || m==12))
printf("\nYour star is SAGITTARIUS");

else if ((d>=22 || d<=19) && (m==12 || m==1))
printf("\nYour star is CAPRICORN");

else if ((d>=20 || d<=18) && (m==1 || m==2))
printf("\nYour star is AQUARIUS");

else if ((d>=19 || d<=20) && (m==2 || m==3))
printf("\nYour star is PISCES");

getch();
}

hey thanks, it worked but is it possible if i dont use || and execute this program completely with &&???

What is the problem with using || ? It is a completely valid part of the C language. There might be some obscure way of doing the same comparison without it, but it would be useless to even try it.

theres no problem using || but my assignment is to use && only...

I don't think I can be much more blatant.

When you type:
[code=c] int main() { printf("Hello, World\n"); return 0; }

[/code]

It looks like this when you post it:

int main() {
    printf("Hello, World\n");
    return 0;
}

The article that talks about it is at http://www.daniweb.com/forums/thread93280.html

I don't think I can be much more blatant.

When you type:
[code=c] int main() { printf("Hello, World\n"); return 0; }

[/code]

It looks like this when you post it:

int main() {
    printf("Hello, World\n");
    return 0;
}

The article that talks about it is at http://www.daniweb.com/forums/thread93280.html

thanks, i think i understood next time i'll try with code tags.

Actually, that code tag message was intended as a reply for yabuki

But if you're not posting your code with code tags, feel free to use them :)

I thought of a way to write your program without using the '||' operator would be to double the ifs and have 2 sections for each.

if (m == 3 && d >= 21)
    printf("\nYour star is ARIES");
if (m == 4 && d <= 19)
    printf("\nYour star is ARIES");

See no '||' but it will be longer and return the symbol twice.

I just thought of another alternative (I'm full of good ideas today :) ):

if you did something like:

/* Put the month and day together as MMDD in a single int */
int birthmd = m * 100 + d;
if (birthmd >= 321 && birthmd <= 419)
    printf("\nYour star is ARIES");

Even cleaner and still no pesky '||'

I thought of a way to write your program without using the '||' operator would be to double the ifs and have 2 sections for each.

if (m == 3 && d >= 21)
    printf("\nYour star is ARIES");
if (m == 4 && d <= 19)
    printf("\nYour star is ARIES");

See no '||' but it will be longer and return the symbol twice.

I just thought of another alternative (I'm full of good ideas today :) ):

if you did something like:

/* Put the month and day together as MMDD in a single int */
int birthmd = m * 100 + d;
if (birthmd >= 321 && birthmd <= 419)
    printf("\nYour star is ARIES");

Even cleaner and still no pesky '||'

hmmmm u r smart i liked it ur first idea is better and simpler, y cudnt i think of it...:D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.