Thread: Please show me

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    16

    Please show me

    This is a slightly more difficult problem. Alter your program from question 2 so that it deals with months as well as years, and produces output such as the following:
    Enter the current year then press RETURN.
    1996
    Enter the current month (a number from 1 to 12).
    10
    Enter your current age in years.
    36
    Enter the month in which you were born (a number from 1 to 12).
    5
    Enter the year for which you wish to know your age.
    2001
    Enter the month in this year.
    6
    Your age in 6/2001: 41 years and 1 month.
    The program should cope with singulars and plurals properly in the output, e.g. "1 month" but "2 months".

    This is my program but i will leave a blank there because i don really know how to show the output for 1 month and 2 months

    #include<stdio.h>
    main()
    {
    int a,b,c,d,i,x,y,z,month,total;
    printf("Enter current year then press RETURN : ");
    scanf("%d",&x);
    printf("Enter the current month (a number from 1 to 12) : ");
    scanf("%d",&a);
    printf("Enter your current age in years : ");
    scanf("%d",&y);
    printf("Enter the month in which you were born (a number from 1 to 12) : ");
    scanf("%d",&b);
    printf("Enter the year for which you wish to know your age : ");
    scanf("%d",&z);
    printf("Enter the month in this year : ");
    scanf("%d",&c);

    i=z-x;
    total=i+y;
    ???????? <== here
    ???????
    printf("Your age in %d/%d : %d years and %d month",c,z,total,month);
    return 0;
    }

    And please tell me the way to make this programe as simple( i mean short)

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    I would recommend, first of all, better variable names.

    a,b,c,d,e would get confusing to me after awhile.
    Just a tip.
    The world is waiting. I must leave you now.

  3. #3
    Registered User char's Avatar
    Join Date
    Apr 2002
    Posts
    31
    First of all, have you designed any algorithm for calculating the age?

  4. #4
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    The algorithm part isn't for me, but this might help offer an idea of how to cope with month vs months.

    Code:
    #include<stdio.h>
    
    int main(void)
    {
    	char *months[2] = { "Month", "Months" };
    	int monthResult;
    	
    	printf("Enter a month (1-12): ");
    	scanf("%d", &monthResult);
    	
    	if(monthResult == 1)
    		printf("That's %d - %s right?", monthResult, months[0]);
    	else if(monthResult < 1 || monthResult > 12)
    		printf("Can't follow directions can you?");
    	else
    		printf("That's %d - %s right?", monthResult, months[1]);
    
    
    	return 0;
    }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    16
    But i failed to combine it, and how to do convert it into function??

  6. #6
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Just test the value that you got for your final month. If it's less than 2 use the first element of the array. As for a function, simply pass both the final values for the year and the month into the function.

    Code:
    void printValues(int, int);
    
    int main(void)
    {
      code to calculate results.
      ..
      
      printValues(month, year);
      ..
    
      return 0;
    }
    
    void printValues(int month, int year)
    {
      char *message[3] = { " month old.", " months old.", "Invalid values detected." };
    
      if(month > 0 && month < 2)
        printf("%d years %s", year, message[0]);
      else if(month >= 2 && month <= 12)
        printf("%d years %s", year, message[1]);
      else
        printf("%s", message[2]);
    }
    Not perfect, but hopefully it will get you what you need.

    Good luck.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Your favorite TV show...
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 53
    Last Post: 04-25-2007, 06:40 AM
  2. show() method does not show...
    By kocika73 in forum C++ Programming
    Replies: 12
    Last Post: 04-09-2006, 09:27 PM
  3. how to show the % sign and remove decimals?
    By seal in forum C Programming
    Replies: 5
    Last Post: 08-31-2005, 05:29 PM
  4. Window won't show
    By Toraton in forum Windows Programming
    Replies: 4
    Last Post: 11-10-2002, 08:07 PM
  5. That 70's show
    By xds4lx in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-27-2002, 04:16 AM