Thread: need help with C im new to programming

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    4

    Exclamation need help with C im new to programming

    i wanted to write a simple program that tell the user zodiac sign and his life path number , the problem is even when given wrong birth date it keep writing the life path number , i am using Euclidean division
    exmp 12/02/1999

    day 12 = 1 + 2 = 3
    month 2 +0= 2
    year 1999 = 1 + 9 + 9 + 9 =28= 2 + 8 = 10=1 + 0 = 1

    total day +month + year
    2 + 3 + 1 = 6

    Life Path Number is 6

    Code:
    
    
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int year,xmd,xmd1,xy,lpn,lpn1,lpn2,xt,x,xt2,xd,xm,m,day,q1,q2,r,q3,q4,dr ;
    
       printf("Enter your birth month(1-12)\n");
        scanf("%d", &m);
        printf("Enter your birth day\n");
        scanf("%d", &day);
        printf("Enter your birth year\n");
        scanf("%d", &year);
        if(( year<=1920 ) || (year>=2020  ))
            {
                printf("Invalid Year of Birth \n");
            }
    
        else if( (m == 12 && day >= 22) || (m == 1 && day <= 19) )
        {
            printf("Your Zodiac Sign based on your Birth date is Capricorn\n");
        }
        else if( (m == 1 && day >= 20) || (m == 2 && day <= 17) )
        {
            printf("Your Zodiac Sign based on your Birth date is Aquarius\n");
        }
        else if( (m == 2 && day >= 18) || (m == 3 && day <= 19) )
        {
            printf("Your Zodiac Sign based on your Birth date is Pisces\n");
        }
        else if( (m == 3 && day >= 20) || (m == 4 && day <= 19) )
        {
            printf("Your Zodiac Sign based on your Birth date is Aries\n");
        }
        else if( (m == 4 && day >= 20) || (m == 5 && day <= 20) )
        {
            printf("Your Zodiac Sign based on your Birth date is Taurus\n");
        }
        else if( (m == 5 && day >= 21) || (m == 6 && day <= 20) )
        {
            printf("Your Zodiac Sign based on your Birth date is Gemini\n");
        }
        else if( (m == 6 && day >= 21) || (m == 7 && day <= 22) )
        {
            printf("Your Zodiac Sign based on your Birth date is Cancer\n");
        }
        else if( (m == 7 && day >= 23) || (m == 8 && day <= 22) )
        {
            printf("Your Zodiac Sign based on your Birth date is Leo\n");
        }
        else if( (m == 8 && day >= 23) || (m == 9 && day <= 22) )
        {
            printf("Your Zodiac Sign based on your Birth date is Virgo\n");
        }
        else if( (m == 9 && day >= 23) || (m == 10 && day <= 22) )
        {
            printf("Your Zodiac Sign based on your Birth date is Libra\n");
        }
        else if( (m == 10 && day >= 23) || (m == 11 && day <= 21) )
        {
            printf("Your Zodiac Sign based on your Birth date is Scorpio\n");
        }
        else if( (m == 11 && day >= 22) || (m == 12 && day <= 21) )
        {
            printf("Your Zodiac Sign based on your Birth date is Sagittarius\n");
    
    
        }
        else
        {
            printf("Invalid Birth date entered\n");
        }
    
     q1=year/1000;
      r=year%1000;
     q2=r/100;
     r=r%100;
     q3=r/10;
     r=r%10;
     q4=r;
     xy=q1+q2+q3+q4;
    
    
     if (m >= 10 && day >= 10)
        {
    
        x=m/10;
        xm=m%10;
         xd=day/10;
        dr=day%10;
    x=dr+xd+xm+x;
        }
        else { x=m;
    
    
               xd=day;
    x=xd+m;
    
    
    }
    if (x >= 10)
         {
            xmd=x/10;
        xmd1=x%10;
    x=xmd+xmd1;
    }
    
    
    if (xy >= 10 )
        {
        xt=xy/10;
        xt2=xy%10;
      xy=xt+xt2;
        }
    
    lpn=xy+x;
    
    
    if (lpn >= 10 )
        {
    
    
        lpn1=lpn/10;
        lpn2=lpn%10;
      lpn=lpn1+lpn2;
        }
    
    
    printf("your life path is " "%d\n",lpn) ;
    
    
    return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    164
    Can you show us your sample output when the problem occurs?

    I suspect you want to 'loop' around when getting the date, while the day, month or year is invalid.

    Also this might be an easier way to calculate 'lpn' given year, month and day:
    Code:
    int digits = year*10000+month*100+day;
    while(digits > 10) {
       digits = (digits/10) + digits%10;
    }
    lpn = digits;
    Oh, and because you are using 'else', you don't need half of the date tests...

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    4

    output

    the first is correct but in the second one as you can see it say Invalid Birth date entered but it show the life path number
    i need to test (use if function )with all the dates cause every date have different zodiac sign

    the program it self is correct but only the loop , it need to stop at the last else if all the conditions are false and not showing the msg " your life path is ... "
    one last thing how can i optimize it , maybe spread it in other files by functions and call them to the main one
    thanks for the help i really appreciate it
    Attached Images Attached Images need help with C im new to programming-prog-jpg need help with C im new to programming-prog2-jpg 
    Last edited by oussama132; 11-29-2020 at 04:13 PM.

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    164
    Do you need something like this?

    Code:
    printf("Enter your birth year\n");
    scanf("%d", &year);
    while(year<=1920 || year>=2020) {
       printf("Invalid Year of Birth \n");
       printf("Re-enter your birth year\n");
       scanf("%d", &year);
    }
    (Example is just for getting the year)

  5. #5
    Registered User
    Join Date
    Nov 2020
    Posts
    4
    Quote Originally Posted by hamster_nz View Post
    Do you need something like this?

    Code:
    printf("Enter your birth year\n");
    scanf("%d", &year);
    while(year<=1920 || year>=2020) {
       printf("Invalid Year of Birth \n");
       printf("Re-enter your birth year\n");
       scanf("%d", &year);
    }
    (Example is just for getting the year)

    thank you so much it helped for the year , still need the same after verifying the month and the day are not equal to all of the conditions

    thank you, much respect

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,047
    Code:
    #include <stdio.h>
     
    struct {
        const char *month;
        const char *zodiac;
        int         ends;
    } Data[] = {
        { "January",   "Capricorn",   19 },
        { "February",  "Aquarius",    19 },
        { "March",     "Pisces",      20 },
        { "April",     "Aries",       20 },
        { "May",       "Taurus",      20 },
        { "June",      "Gemini",      20 },
        { "July",      "Cancer",      22 },
        { "August",    "Leo",         22 },
        { "September", "Virgo",       22 },
        { "October",   "Libra",       22 },
        { "November",  "Scorpio",     22 },
        { "December",  "Sagittarius", 21 }
    };
     
    int main() {
        int y, m, d;
        printf("y: "); scanf("%d", &y);
        printf("m: "); scanf("%d", &m);
        printf("d: "); scanf("%d", &d);
     
        printf("%s %d, %d\n", Data[m - 1].month, d, y);
     
        int z = (m - (d <= Data[m - 1].ends)) % 12;
        printf("Your Zodiac Sign is %s.\n", Data[z].zodiac);
     
        int lp = y + m + d;
        lp = (lp % 9 ? lp % 9 : 9);
        printf("Your life path is " "%d.\n", lp);
     
        return 0;
    }
    Last edited by john.c; 11-30-2020 at 08:18 AM.
    Philosophy is a battle against the bewitchment of our intelligence by means of language. - Wittgenstein

  7. #7
    Registered User
    Join Date
    Nov 2020
    Posts
    4
    thanks i really appreciate it , but can i keep the function the way i did it ? thanks again just small modification

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 02-01-2019, 12:27 PM
  2. Replies: 0
    Last Post: 02-01-2019, 12:22 PM
  3. Replies: 4
    Last Post: 12-11-2011, 04:25 PM
  4. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM

Tags for this Thread