where to input another question in the same if else program

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 2046 Views - Last Post: 12 August 2012 - 07:11 PM Rate Topic: -----

#1 lee03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-August 12

where to input another question in the same if else program

Posted 11 August 2012 - 10:31 PM

i'm already done creating c program that will ask the user to input his birthday, then the output will bu his/her zodiac signs and horoscope, but i want to add another question in the same program that will ask the user to enter his birthyear(e.g. year==1978||year==1990) > "year of the horse. i already tried that nested-if and dangling if statement but when i add another else-if statement the compiler said that it was an error due to "misplaced else"..
#include<stdio.h>
#include<conio.h>

int month, date, year;

main()
{
clrscr();
gotoxy(5,1);
printf("HOROSCOPE\n");
printf("\nEnter birthday:\t");
scanf("%d%d", &month);
scanf("%d%d", &date);

{
if ((month==05&&date>=20)||(month==06&&date<=19))
{
printf("\nYour Zodiac Sign: GEMINI");
printf("\nYour Horoscope);
}
else if ((month==06&&date>=20)||(month==07&&date<=21))
{
printf("\nYour Zodiac Sign: CANCER");
printf("\nYour Horoscope");
}
else if ((month==07&&date<=22)||(month==8&&date<=21))
{
printf("\nYour Zodiac Sign: LEO");
printf("\nYour Horoscope. ");
}
else if ((month==8&&date>=22)||(month==9&&date<=21))
{
printf("\nYour Zodiac Sign: VIRGO");
printf("\nYour Horoscope. ");
}
else if ((month==9&&date>=22)||(month==10&&date<=21))
{
printf("\nYour Zodiac Sign: LIBRA");
printf("\nYour Horoscope.");
}
else if ((month==10&&date>=22)||(month==11&&date<=20))
{
printf("\nYour Zodiac Sign: SCORPIO");
printf("\nYour Horoscope. ");
}
else if ((month==11&&date>21)||(month==12&&date<20))
{
printf("\nYour Zodiac Sign: SAGITTARIUS");
printf("\nYour Horoscope");
}
else if ((month==12&&date>=21)||(month==01&&date<=18))
{
printf("\nYour Zodiac Sign: CAPRICORN");
printf("\nYour Horoscope");
}
else if ((month==01&&date>=19)||(month==02&&date<=17))
{
printf("\nYour Zodiac Sign: AQUARIUS");
printf("\nYour Horoscope");
}
else if ((month==02&&date>=18)||(month==03&&date<=19))
{
printf("\nYour Zodiac Sign: PISCES");
printf("\nYour Horoscope");
}
else if ((month==03&&date>=20)||(month==04&&date<=18))
{
printf("\nYour Zodiac Sign: ARIES");
printf("\nYour Horoscope");
}
else if ((month==04&&date>=19)||(month==05&&date<=19))
{
printf("\nYour Zodiac Sign: TAURUS");
printf("\nYour Horoscope.");
}
else
{
printf("invalid");
}
}
getch();
return(0);
}



Is This A Good Question/Topic? 0
  • +

Replies To: where to input another question in the same if else program

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,124
  • Joined: 05-May 12

Re: where to input another question in the same if else program

Posted 11 August 2012 - 11:07 PM

You seriously need to pick an indent style and apply it consistently. Having everything flush left makes it difficult to parse out the flow of your program.
Was This Post Helpful? 0
  • +
  • -

#3 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: where to input another question in the same if else program

Posted 11 August 2012 - 11:12 PM

I'd suggest dividing your program up into functions.

You'd definitely want a function just for the astrology stuff (it could take the birthday info as a parameter). You'd then have a function dedicated to the Chinese Zodiac information. Your main function would get the user input and call on these functions. Nice and simple, right?
Was This Post Helpful? 0
  • +
  • -

#4 lee03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-August 12

Re: where to input another question in the same if else program

Posted 11 August 2012 - 11:25 PM

View PostSkydiver, on 11 August 2012 - 11:07 PM, said:

You seriously need to pick an indent style and apply it consistently. Having everything flush left makes it difficult to parse out the flow of your program.


so sorry about that indent style..but thanks anyway, for the idea.. :)

View Postatraub, on 11 August 2012 - 11:12 PM, said:

I'd suggest dividing your program up into functions.

You'd definitely want a function just for the astrology stuff (it could take the birthday info as a parameter). You'd then have a function dedicated to the Chinese Zodiac information. Your main function would get the user input and call on these functions. Nice and simple, right?

you mean creating another program with that function? but the needed output with the program should be like this:

enter birthday day:
Your zodiac sign is:
Your Horoscope is..

enter birthyear:
Year of the...

:helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,124
  • Joined: 05-May 12

Re: where to input another question in the same if else program

Posted 11 August 2012 - 11:26 PM

There is a major problem with your scanf() formatters.

You can always call a function to do the Chinese zodiac sign check instead of making your if/else if pile of pasta become even more complex.

And by the way a look up table maybe better suited to your needs:
typedef struct
{
    int Month;
    int Day;
} Date;

typedef struct
{
   const char * Sign;
   Date Start;
   Date End;
} SignDateRange;

#define NUMBER_OF_SIGNS 12

SignDateRange theSigns[12] = {
    { "Aries",  { 3, 21 }, { 4, 19 } },
    { "Taurus", { 4, 20 }, { 5, 20 } },
    { "Gemini", { 5, 21 }, { 6, 20 } },
    :
};

int LookUpGreekSign(int month, int date)
{
    int i;
    for(i = 0; i < NUMBER_OF_SIGNS; i++)
    {
        if (theSigns[i].Start.Month <= month &&
            theSigns[i].Start.Day <= date &&
            theSigns[i].End.Month >= month &&
            theSigns[i].End.Day >= date)
        {
            printf("%s", theSigns[i].Sign);
            break;
        }
    }
    // Returns zero if sign is not found
    return i < NUMBER_OF_SIGNS;
}

void LookUpChineseSign(int year)
{
    // your code here: find Chinese sign
}

int main()
{
    // your code here: input month and date

    if (LookUpGreekSign(month, date) == 0)
    {
        printf("Your sign is a STOP sign.");
    }
    else
    {
        LookUpChineseSign(year);
    }
    return 0;
}


This post has been edited by Skydiver: 11 August 2012 - 11:31 PM

Was This Post Helpful? 0
  • +
  • -

#6 lee03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-August 12

Re: where to input another question in the same if else program

Posted 11 August 2012 - 11:33 PM

[quote name='Skydiver' date='11 August 2012 - 11:26 PM' timestamp='1344752814' post='1683111']
There is a major problem with your scanf() formatters.

You can always call a function to do the Chinese zodiac sign check instead of making your if/else if pile of pasta become even more complex.

And by the way a look up table maybe better suited to your needs:
typedef struct
{
    int Month;
    int Day;
} Date;

typedef struct
{
   const char * Sign;
   Date Start;
   Date End;
} SignDateRange;

#define NUMBER_OF_SIGNS 12

SignDateRange theSigns[12] = {
    { "Aries",  { 3, 21 }, { 4, 19 } },
    { "Taurus", { 4, 20 }, { 5, 20 } },
    { "Gemini", { 5, 21 }, { 6, 20 } },
    :
};

int LookUpGreekSign(int month, int date)
{
    int i;
    for(i = 0; i < NUMBER_OF_SIGNS; i++)
    {
        if (theSigns[i].Start.Month <= month &&
            theSigns[i].Start.Day <= date &&
            theSigns[i].End.Month >= month &&
            theSigns[i].End.Day >= date)
        {
            printf("%s", theSigns[i].Sign);
            break;
        }
    }
    // Returns zero if sign is not found
    return i < NUMBER_OF_SIGNS;
}

void LookUpChineseSign(int year)
{
    // your code here: find Chinese sign
}

int main()
{
    // your code here: input month and date

    if (LookUpGreekSign(month, date) == 0)
    {
        printf("Your sign is a STOP sign.");
    }
    else
    {
        LookUpChineseSign(year);
    }
    return 0;
}


[/quote
:oops: may i ask if this is applicable with using turboc?
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,124
  • Joined: 05-May 12

Re: where to input another question in the same if else program

Posted 11 August 2012 - 11:44 PM

structs and arrays are pretty standard for C.

The 'const' keyword may make some compilers choke so just don't use 'const'.

Although this site claims that Turbo C supports it: http://wiki.answers....ords_in_turbo_c

Anyway, abandon Turbo C and move up to modern compiler. I hear that Code::Blocks is good and free.

This post has been edited by Skydiver: 11 August 2012 - 11:48 PM

Was This Post Helpful? 0
  • +
  • -

#8 lee03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-August 12

Re: where to input another question in the same if else program

Posted 11 August 2012 - 11:52 PM

View PostSkydiver, on 11 August 2012 - 11:44 PM, said:

structs and arrays are pretty standard for C.

The 'const' keyword may make some compilers choke so just don't use 'const'.

i'm gladly appreciating your help but we're only allowed to use else-if statement to do that program. we're not yet taught to use arrays and structs.. :sad3:

View PostSkydiver, on 11 August 2012 - 11:44 PM, said:

structs and arrays are pretty standard for C.

The 'const' keyword may make some compilers choke so just don't use 'const'.

Although this site claims that Turbo C supports it: http://wiki.answers....ords_in_turbo_c

Anyway, abandon Turbo C and move up to modern compiler. I hear that Code::Blocks is good and free.


i also wanted to move up with much modern compiler, but turbo c is what we're using in the school
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,124
  • Joined: 05-May 12

Re: where to input another question in the same if else program

Posted 11 August 2012 - 11:56 PM

I find it sad that a course is so strict that you can't use anything that you learn outside of class.

Does that mean if you've learned how to the the fast division by 10, you still have to do long division instead of just moving the decimal point? That sucks.

So if you are stuck with if-else's, how come you are using clrscr() and gotoxy()?
Was This Post Helpful? 0
  • +
  • -

#10 lee03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-August 12

Re: where to input another question in the same if else program

Posted 12 August 2012 - 12:07 AM

View PostSkydiver, on 11 August 2012 - 11:56 PM, said:

I find it sad that a course is so strict that you can't use anything that you learn outside of class.

Does that mean if you've learned how to the the fast division by 10, you still have to do long division instead of just moving the decimal point? That sucks.

So if you are stuck with if-else's, how come you are using clrscr() and gotoxy()?


most of the syntax i found it more clearer in the discussion and videos i read and watch in the internet. :sad3:

yes, because turbo c is the only compiler use in this semester.

that clrscr() and gotoxy() syntax was taught to use in the program, is just i am a little bit having a problem where to insert another else if-statement, i already tried that nested-if, dangling if, if-else statement but all i got is error....

thank you so much skydiver for your replies.. i really appreciated it... :genius:
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,124
  • Joined: 05-May 12

Re: where to input another question in the same if else program

Posted 12 August 2012 - 12:34 AM

Show us your code with the one of your failed attempts and we'll try point you the right direction.

Personally, if I was stuck with if-else's then my code would look something like:
enum Month { Jan = 1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };

int IsInRange(Month checkMonth, int checkDate,
              Month startMonth, int startDate,
              Month endMonth,   int endDate)
{
    return startMonth <= checkMonth &&
           startDate <= checkDate &&
           checkMonth <= endMonth &&
           checkDate <= endDate;
}

int main()
{
    int validDate = 1;

    if (IsInRange(month, date, Mar, 21, Apr, 19))
    {
        // print details about Aries
    }
    else if(IsInRange(month, date, Apr, 20, May, 20))
    {
        // print details about Aries
    }
    :
    else
    {
        // print invalid date
        validDate = 0;
    }

    if (validDate == 1)
    {
        // do code for Chinese new year
    }
}


Was This Post Helpful? 0
  • +
  • -

#12 lee03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-August 12

Re: where to input another question in the same if else program

Posted 12 August 2012 - 04:35 AM

View PostSkydiver, on 12 August 2012 - 12:34 AM, said:

Show us your code with the one of your failed attempts and we'll try point you the right direction.

Personally, if I was stuck with if-else's then my code would look something like:
enum Month { Jan = 1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };

int IsInRange(Month checkMonth, int checkDate,
              Month startMonth, int startDate,
              Month endMonth,   int endDate)
{
    return startMonth <= checkMonth &&
           startDate <= checkDate &&
           checkMonth <= endMonth &&
           checkDate <= endDate;
}

int main()
{
    int validDate = 1;

    if (IsInRange(month, date, Mar, 21, Apr, 19))
    {
        // print details about Aries
    }
    else if(IsInRange(month, date, Apr, 20, May, 20))
    {
        // print details about Aries
    }
    :
    else
    {
        // print invalid date
        validDate = 0;
    }

    if (validDate == 1)
    {
        // do code for Chinese new year
    }
}



here's one of the way i'd tried..but the compiler said that it was a "misplaced else".
#include<stdio.h>
#include<conio.h>

int month, date, year;

main()
{
clrscr();
gotoxy(5,1);
printf("HOROSCOPE\n");
printf("\nEnter birthday:\t");
scanf("%d%d", &month);
scanf("%d%d", &date);

{
if ((month==05&&date>=20)||(month==06&&date<=19))
{
printf("\nYour Zodiac Sign: GEMINI");
printf("\nYour Horoscope...;
}
else if ((month==10&&date>=22)||(month==11&&date<=20)
prinf("\nYour Zodiac Sign: Scorpio");
printf("\nYour Horoscope...;
}
else
{
printf("invalid");
}
}
[color="#FF0000"]printf("\nEnter Birthyear:");
scanf("%d%d%d%d", &year);
{
if(year==1978||year==1990);
{
printf("year of the horse");
}
else if(year==1979||year==1991);
{
printf("year of the sheep");
}
else
{
printf("invalid")[/color];
}
}
getch();
return(0);
}


Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,124
  • Joined: 05-May 12

Re: where to input another question in the same if else program

Posted 12 August 2012 - 07:53 AM

On 33 and 37, why do you have semicolons?
Was This Post Helpful? 0
  • +
  • -

#14 lee03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-August 12

Re: where to input another question in the same if else program

Posted 12 August 2012 - 08:56 AM

View PostSkydiver, on 12 August 2012 - 07:53 AM, said:

On 33 and 37, why do you have semicolons?

:oops: my bad. it's just a typographical error due to mixing of character in mind, but in my program in turbo c, there's none..
Was This Post Helpful? 0
  • +
  • -

#15 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1345
  • View blog
  • Posts: 4,635
  • Joined: 19-February 09

Re: where to input another question in the same if else program

Posted 12 August 2012 - 04:12 PM

Hi, you don't need a %d for each digit of a number.

31	scanf("%d%d%d%d", &year);




here the whole number is read, eg. 1984 could be read to year.

31	scanf("%d", &year);


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2