your "horrorscope"......

This is a discussion on your "horrorscope"...... within the C Programming forums, part of the General Programming Boards category; (to the board editors: sorry for my previous thread !!) We're asked to write an astrology program, 1. the user ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    60

    your "horrorscope"......

    (to the board editors: sorry for my previous thread !!)

    We're asked to write an astrology program,

    1. the user types his/her birthday..
    2. the program responds with the user's zodiac sign...

    So far, Im the only one on class who got it...
    and I know I have to edit this source code -to make it simple..

    Do you have any suggestions... comments ...

    Please share your codes or *hints* so I may be able to research on the new stuff.....


    Code:
    #include <stdio.h>
    #include<string.h>
    main()
    {
     char s[80];
     int d;
    
     clrscr();
    
     printf("enter the month of your birthday: ");
     gets(s);
    
     if(!(strcmp(s, "january")))
     {
      printf("\n enter the date of your birthday: ");
      scanf("%i", &d);
      if(d<=19)
      printf("\n CAPRICORN");
      else
      printf("\n AQUARIUS");
     }
    
     if(!(strcmp(s, "february")))
    {
     printf("\n enter the date of your birthday: ");
     scanf("%i", &d);
     if(d<=18)
     printf("\n AQUARIUS");
     else
     printf("\n PISCES");
    }
    
     if(!(strcmp(s, "march")))
    {
     printf("\n enter the date of your birthday: ");
     scanf("%i", &d);
     if(d<=20)
     printf("\n PISCES");
     else
     printf("\n ARIES");
    }
    
     if(!(strcmp(s, "april")))
    {
     printf("\n enter the date of your birthday: ");
     scanf("%i", &d);
     if(d<=19)
     printf("\n ARIES");
     else
     printf("\n TAURUS");
    }
    
     if(!(strcmp(s, "may")))
    {
     printf("\n enter the date of your birthday: ");
     scanf("%i", &d);
     if(d<=20)
     printf("\n TAURUS");
     else
     printf("\n GEMINI");
    
    }
    
     if(!(strcmp(s, "june")))
    {
     printf("\n enter the date of your birthday: ");
     scanf("%i", &d);
     if(d<=20)
     printf("\n GEMINI");
     else
     printf("\n CANCER");
    }
    
     if(!(strcmp(s, "july")))
    {
     printf("\n enter the date of your birthday: ");
     scanf("%i", &d);
     if(d<=22)
     printf("\n CANCER");
     else
     printf("\n LEO");
    }
    
     if(!(strcmp(s, "august")))
    {
     printf("\n enter the date of your birthday: ");
     scanf("%i", &d);
     if(d<=22)
     printf("\n LEO");
     else
     printf("\n VIRGO");
    }
    
     if(!(strcmp(s, "september")))
    {
     printf("\n enter the date of your birthday: ");
     scanf("%i", &d);
     if(d<=22)
     printf("\n VIRGO");
     else
     printf("\n LIBRA");
    }
    
     if(!(strcmp(s, "october")))
    {
     printf("\n enter the date of your birthday: ");
     scanf("%i", &d);
     if(d<=22)
     printf("\n LIBRA");
     else
     printf("\n SCORPIO");
    }
    
     if(!(strcmp(s, "november")))
    {
     printf("\n enter the date of your birthday: ");
     scanf("%i", &d);
     if(d<=21)
     printf("\n SCORPIO");
     else
     printf("\n SAGITTARIUS");
    }
    
     if(!(strcmp(s, "december")))
    {
     printf("\n enter the date of your birthday: ");
     scanf("%i", &d);
     if(d<=21)
     printf("\n SAGITTARIUS");
     else
     printf("\n CAPRICORN");
    }
      getch();
    }

    Thanks in advance !!!!

  2. #2
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    here's a hint: do something to all those if blocks to make them more readable, and simpler. Just a little switch in how you check your input could make a big difference in simplifying your code.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    60

    switch... yes !!

    back then (while fixing this code) I got the idea of using switches..

    but Im having a TOTAL mess with using strings as "variables" and case "constants"...

    how should i write:
    Code:
     switch('month') {
        case 'january':
          { statement sequence}
           break;
        case 'february':
           { statement sequence }
           break;
    
       etc... etc,...
           }
    anyway... how could i use characters and strings as switch/case conditions...? Im used to numbers only...

    I really couldn't get it....

    if you could give me a simple example,.. it might help me through this !!

    Thanks !!

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    Have you though about using a enumeration?

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    you can't use strings that way. I would probably do an enum type thing as suggested. Besides, string compares take a lot longer.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    OK, ready? Tighten your seatbelts, cuz here we go!!!

    1) Your main() should be int main()
    and put a return 0; at the bottom of the code.

    2) gets(s); should be changed to fgets(s,80,stdin);. gets() will allow you to blow your data array and cause problems

    3) Set up arrays for your values:
    char *months[12]; // Hold the month names
    char *signs[12]; // holds the sign names
    int datechg[12]; // day of month the sign changes

    4) Move the "enter the date of ..." input immediately after the "enter the month...", that way you only need it once.

    5) Instead of a bunch of if's, use a loop to test the month entered with months. Be sure to use a case insensitive test to distinguish between april, April, and APRIL. This loop will give you an index (say m) into the other two arrays.

    6) Test datechg[m] against your d and if d is >, add one to m (if m is now 12, replace it with 0)

    7) m now contains the index into the signs array for the person's horrorscope.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    60

    Oi !! WaltP.. !!!

    Oi !

    You deduced my problem to the point of REAL simplicity !!

    Yeah !! Thanks !!

    I got edited it all to using: fgets(s, 80,stdin),... the int main(),..
    and return 0; (as you clarified)...

    But i couldnt follow your point in determing the zodiac sign,--given the month and date,...

    I don't mean to push you through this....

    but could you please give me a straight formula.. if there is ?

    anyway,.. Thanks A Lot Dude !!!!!!

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157

    Re: Oi !! WaltP.. !!!

    Originally posted by imbecile in C
    I got edited it all to using: fgets(s, 80,stdin),... the int main(),..
    and return 0; (as you clarified)...
    I assume you also put the input for the day of the month directly under the month input.

    But i couldnt follow your point in determing the zodiac sign,--given the month and date,...
    Step 1, figure out what month was entered. I'm going to use a different problem and you can figure out how to use it in your program:

    Code:
    char *continent = {"Australia, Asia, Africa, Europe, NAmerica, SAmerica"};
    char contin[20];
    
    printf("Enter a continent: ");
    fgets(contin, 20, stdin);
    
    i=0;
    while (strcmp(contin, continent[i]) != 0)
    {
        i++;
        if (i >= 6) break;
    }
    if (i >= 6)
    {
        print "Invalid continent entered: %s\n", contin);
    }
    else
    {
        /* the rest of the code, with i as your index */
    }
    You need to figure out what action to take if they type in something invalid. Also, depending on your compiler, the strcmp() could be a case insensitive comparison like stricmp() on some Windows compilers. Otherwise you might want to write your own.

    That should get you started.

    Yes, I forgot a continent, but it's too cold to even consider...
    Last edited by WaltP; 08-13-2003 at 11:56 AM.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>You need to figure out what action to take if they type in something invalid.<<
    You even need to add further code to check a valid entry (hint: newline)

    Code:
    char *continent = {"Australia, Asia, Africa, Europe, NAmerica, SAmerica"};
    Oopps, that ain't right I think you've missed something there, and no, I don't mean a continent!

    The while loop is pretty poor too imho, a for loop would be a neater way to do it (and therefore better to teach the newbies with...)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by Hammer
    >>You need to figure out what action to take if they type in something invalid.<<
    You even need to add further code to check a valid entry (hint: newline)

    Code:
    char *continent = {"Australia, Asia, Africa, Europe, NAmerica, SAmerica"};
    Oopps, that ain't right I think you've missed something there, and no, I don't mean a continent!

    The while loop is pretty poor too imho, a for loop would be a neater way to do it (and therefore better to teach the newbies with...)
    Okay, okay, be that way
    Code:
    char *continent = {"Australia", "Asia", "Africa", "Europe", "NAmerica", "SAmerica"};
    I disagree about the for loop. It is of course a personal choice. I dislike for loops for looking thru a list and breaking prematurely if found. It should go thru to the comparison except in an extreme case. IMHO I feel a while loop or do-while is better suited for this situation.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by WaltP
    Code:
    char *continent = {"Australia", "Asia", "Africa", "Europe", "NAmerica", "SAmerica"};
    That doesn't compile You still forgot something!

    And for the for loop, here's my suggestion:

    Code:
    for (i = 0; i < 6 && strcmp(contin, continent[i]) != 0; i++)
        ;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Registered User
    Join Date
    Jun 2003
    Posts
    60

    Hey Dudes !! C'mon !!

    Code:
     char *continent ={"Australia", "Asia", "Africa", "Europe", "NAmerica", "SAmerica"};
    Ok.. How do you declare the continent right ??

    God ! your concepts are to advanced for me to follow...
    the truth is im grappling on a hook for this one,..

    I do really SURRENDER !!!

    I wish to understand,..

    Thanks !!

  13. #13
    Registered User
    Join Date
    Jun 2003
    Posts
    60
    How could I simplify this more...


    [note: fgets(s, 80, stdin) doesn't work for me... (sorry) it doesnt compile !!]

    it doesn't matter if the user types the month and date:

    June 6789 (no error traps for a while)...

    Code:
    #include <stdio.h>
    int main()
    {
     char s[80];
     int d;
     clrscr();
    
     printf("enter the MONTH of your birthday: ");  gets(s);
     printf("enter the DATE  of your birthday: ");  scanf("%i", &d);
    
     if(!strcmp(s, "january")) {
      if(d<=19)
      printf("\n CAPRICORN");
      else
      printf("\n AQUARIUS");
     }
    
     if(!strcmp(s, "february")) {
      if(d<=18)
      printf("\n AQUARIUS");
      else
      printf("\n PISCES");
     }
    
     if(!strcmp(s, "march")) {
      if(d<=20)
      printf("\n PISCES");
      else
      printf("\n ARIES");
     }
    
     if(!strcmp(s, "april")) {
      if(d<=19)
      printf("\n ARIES");
      else
      printf("\n TAURUS");
     }
    
     if(!strcmp(s, "may")) {
      if(d<=20)
      printf("\n TAURUS");
      else
      printf("\n GEMINI");
     }
    
     if(!strcmp(s, "june")) {
      if(d<=20)
      printf("\n GEMINI");
      else
      printf("\n CANCER");
     }
    
     if(!strcmp(s, "july")) {
      if(d<=22)
      printf("\n CANCER");
      else
      printf("\n LEO ");
     }
    
     if(!strcmp(s, "august")) {
      if(d<=22)
      printf("\n LEO ");
      else
      printf("\n VIRGO");
     }
    
     if(!strcmp(s, "september")) {
      if(d<=22)
      printf("\n VIRGO");
      else
      printf("\n LIBRA ");
     }
    
     if(!strcmp(s, "october")) {
      if(d<=22)
      printf("\n LIBRA ");
      else
      printf("\n SCORPIO ");
     }
    
     if(!strcmp(s, "november")) {
      if(d<=21)
      printf("\n SCORPIO");
      else
      printf("\n SAGITTARIUS");
     }
    
     if(!strcmp(s, "december")) {
      if(d<=21)
      printf("\n SAGITTARIUS");
      else
      printf("\n CAPRICORN");
     }
      getch(); return 0;
    }

    anyway, I've submitted this as an assignment... Im the only one on class who got it right,..

    but I just wanna know any techniques to simplify this code !
    pls give some examples,..

    Thanks Dudes !!

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Hey Dudes !! C'mon !!

    Originally posted by imbecile in C
    Code:
     char *continent[] ={"Australia", "Asia", "Africa", "Europe", "NAmerica", "SAmerica"};
    Ok.. How do you declare the continent right ??
    The [] was missing. Without it, the original attempt:
    char *continent = {"one two three"};
    .. creates a single string consisting of many words, it does not create a 2d array.

    With the [] and comma delimited list, you are creating an array of char pointers, and assigning them default values, pointing to the string literals.

    Here's a simple program to show you:
    Code:
    #include <stdio.h>
    
    /* There are better ways to express the array size, 
    but I won't cover them here, I'll keep things simple */
    #define NUM_CONTINENTS 6
    
    int main(void)
    {
      char *continents[NUM_CONTINENTS] = {"Australia", "Asia", "Africa", "Europe", "NAmerica", "SAmerica"};
      int i;
      
      for (i = 0; i < NUM_CONTINENTS; i++)
        puts (continents[i]);
      
      return(0);
    }
    
    /* Output
    
    Australia
    Asia
    Africa
    Europe
    NAmerica
    SAmerica
    
    */
    And now a more useful sample for you to study:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    /* There are better ways to express the array size, 
    but I won't cover them here, I'll keep things simple */
    #define NUM_CONTINENTS 6
    
    int main(void)
    {
      char *continents[NUM_CONTINENTS] = {"Australia", "Asia", "Africa", "Europe", "NAmerica", "SAmerica"};
      int i;
      char buf[BUFSIZ];  /* BUFSIZ is a number, at least 256 in value */
      
      printf ("Enter a continent: ");
      fflush(stdout);
      
      if (fgets(buf, sizeof(buf), stdin))
      {
        strtok (buf, "\n\r"); /* Remove the newline character from the input */
        
        for (i = 0; i < NUM_CONTINENTS; i++)
        {
          if (strcmp (continents[i], buf) == 0)
          {
            printf ("Found a match for %s\n", continents[i]);
            break;
          } 
        }
        if (i == NUM_CONTINENTS)
        {
          puts ("No match found");
        }
        
      }  
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #15
    Registered User
    Join Date
    Jun 2003
    Posts
    60

    Oi !! Hammer it all !!

    Oi !!

    Thats real help dude !!
    I had copied your codes on my Very Creative !! Thanks !!!
    =======

    Now.. may i ask something again...??

    I have a source code below,... and notice that I have simplified it thoroughly (thanks for your help)

    Notice the switch part:

    I gotta simplify it somehow,.. same printf's repeat 2 times...

    What should I do to revise the code.. ??
    Code:
    #include <stdio.h>
    #define N 12
    int main(void)
    {
      char m[80], *months[N] = {"january", "february", "march", "april", "may",
    			    "june", "july", "august", "september", "october",
    			    "november", "december"};
      int i, d;
      clrscr();
      printf("enter MONTH of birth: ");  gets(m);
      printf("enter DATE  of birth: ");  scanf("%i", &d);
      for(i=0; i<N && strcmp(m, months[i]) != 0; i++);
      switch(i) {
        case 0:  if (d<=19) printf("CAPRICORN"); else printf("AQUARIUS"); break;
        case 1:  if (d<=18) printf("AQUARIUS"); else printf("PISCES"); break;
        case 2:  if (d<=20) printf("PISCES"); else printf("ARIES"); break;
        case 3:  if (d<=19) printf("ARIES"); else printf("TAURUS"); break;
        case 4:  if (d<=20) printf("TAURUS"); else printf("GEMINI"); break;
        case 5:  if (d<=20) printf("GEMINI"); else printf("CANCER"); break;
        case 6:  if (d<=22) printf("CANCER"); else printf("LEO"); break;
        case 7:  if (d<=22) printf("LEO"); else printf("VIRGO"); break;
        case 8:  if (d<=22) printf("VIRGO"); else printf("LIBRA"); break;
        case 9:  if (d<=22) printf("LIBRA"); else printf("SCORPIO"); break;
       case 10:  if (d<=21) printf("SCORPIO"); else printf("SAGITTARIUS"); break;
       case 11:  if (d<=21) printf("SAGITTARIUS"); else printf("CAPRICORN"); break;
       }
      getch();
    
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21