Thread: Help me PLEASE........

  1. #1
    Unregistered
    Guest

    Unhappy Help me PLEASE........

    i am stuck on this assignment, i am a newbie just started learning c and i can't seems to figure out how to finish this.

    This is what i have so far, i have been sitting for the whole night and still trying, help me please....thanks

    ----------------------------
    A program that will read a set of names and dates from the command line; format the names and display the person’s age.

    Input
    At the command line the user will enter zero or more strings. The strings will include names and dates. The names are always first and last, and the dates are always in the format mm/dd/yyyy. The program will support several command line arguments, shown below:

    -f
    Display each name with the first letter of each name capitalized, and the remaining letters lower-case

    -l
    Display only the last name

    -y
    Display age in years

    -h
    Help; not supported with the other options

    A sample session is shown below:

    c:> hw6 -fn john smith 10/01/1965 malik kaleel 3/01/1954

    The user may not enter any strings, at which time your program will display a help message (of your choice) and terminate.

    Output
    Continuing the previous example, you will output the following:

    Student Age

    ---------- ---------

    Smith, John 35.58

    Kaleel, Malik 47.17

    The age is the person’s actual age. For example Malik, in the example above is 47 years and 2 months, thus 47 + 2/12. If the ‘y’ argument was provided you would only display “47”. If no strings are provided, simply display the header information, and display a help message. You will also display help, if the user enters an incorrect argument (an argument is a single character preceded by an '-').

    Functions
    Your program must provide the following functions:

    You will code the following functions:

    1. int getDOB(char * cpDOB, int * ipMonth, int *ipDay, int * ipYear); given a date of birth as a string (cpDOB [mm/dd/yyyy], determine the month, day, and year; return 1 if successful, 0 if failed; fails if the format is incorrect, or if the date is invalid

    2. int formatName(char * cpFirstName, char * cpLastName, char * cpFullName); creates a full name, in the form, <LAST, FIRST>, i.e. “Smith, John”; returns 1 if successful, 0 if failed

    3. void howTo(); display help info on how to correctly use the program; include bonus info if you complete the bonus options (see below)


    Bonus
    5 pts add another argument (-r) that will display the person’s year of retirement (add a column to the display)

    10 pts; add an argument (-p) which will create a random password and display the passwords (add a column to the display); the password should be 8 characters, and include both alphabets, numbers.

    -----------------------------------------
    #include <stdio.h>
    #include <time.h>

    #define SWITCH '-'
    #define FLCAPITALIZED 'f'
    #define LASTNAME 'l'
    #define YEARS 'y'
    #define HELP 'h'

    int getDOB (char * cpDOB, int * ipMonth, int * ipDay, int * ipYear);
    int formatName (char * cpFirstName, char * cpLastName, char *cpFullName);
    void howTo ();
    void DisplayHeader ();

    void main (int argc, char * argv []) {

    int i = 0;
    int j = 0;

    char cSwitch;
    char cOption1;
    char cOption2;
    char cOption3;

    cSwitch = * (argv [1]);
    cOption1 = * (argv [1] + 1);
    cOption2 = * (argv [1] + 2);
    cOption3 = * (argv [1] + 3);



    if (argc > 1) {

    if ((cOption1 == FLCAPITALIZED) || (cOption2 == FLCAPITALIZED) || (cOption3 == FLCAPITALIZED)) {



    }


    if ((cOption1 == LASTNAME) || (cOption2 == LASTNAME) || (cOption3 == LASTNAME)) {

    printf ("Students by last name only\n");
    printf ("----------\n");
    printf ("%s\n", argv[3]);

    }

    if ((cOption1 == YEARS) || (cOption2 == YEARS) || (cOption3 == YEARS)) {



    }




    }

    else if (argc == 1) { //display help screen if no string enter

    howTo();

    }


    }



    void howTo () {

    printf("Help Screen\n");
    printf("------------\n\n");
    printf("c:> hw6 [options] [string1] [string2] [string3]..........\n\n");
    printf("-f\tDisplay each name withthe first letter of each name capitalized, and the\tremaining letter lower-case\n");
    printf("-l\tDisplay onlu the last name\n");
    printf("-y\tDisplay age in years");
    printf("-h\tHelp; not supported with the other options\n");

    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    8
    I am relatively new to c, but think I could help with your assignement. It would help all the users here if you could break down what part specifically you are having problems with. Just putting the whole assignment down is very difficult to work with.

  3. #3
    Unregistered
    Guest

    Unhappy

    thanks for your reply,

    i need help on how to write the part (Display each name with the first letter of each name capitalized, and the remaining letters lower-case, and Display only the last name, Display age in years)

    i think i got the part to get the computer to see the user is enter a command.

    but now i am stuck on how to convert the first letter of name to upper case, i think i sorta got how to print the last name, i think i am wrong, how do i search the string for the last name if the user enters like alot of names, and the last part is how to convert from mm/dd/yyyy to their age.

    if (argc > 1) {

    if ((cOption1 == FLCAPITALIZED) || (cOption2 == FLCAPITALIZED) || (cOption3 == FLCAPITALIZED)) {

    }

    if ((cOption1 == LASTNAME) || (cOption2 == LASTNAME) || (cOption3 == LASTNAME)) {

    printf ("Students by last name only\n");
    printf ("----------\n");
    printf ("%s\n", argv[3]);

    }

    if ((cOption1 == YEARS) || (cOption2 == YEARS) || (cOption3 == YEARS)) {

    }

    }

    else if (argc == 1) { //display help screen if no string enter

    howTo();

    }

  4. #4
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    To make the entire string lowercase you can use strlwr(String). Then to make the first letter capital you can use the topupper(char) function.
    Code:
    strlwr(String);
    toupper(String[0]);
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  5. #5
    Unregistered
    Guest

    Unhappy

    but then i can't use the string library to do this........

    can you explain in more detail on how to use topupper(char) function, thanks

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    int x;
    a[0] = toupper ( a[0] );
    for ( x = 1; a[x] != '\0'; x++ )
      a[x] = tolower ( a[x] );
    -Prelude
    My best code is written with the delete key.

  7. #7
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by Unregistered
    but now i am stuck on how to convert the first letter of name to upper case
    If you can use the toupper and tolower function you can do something like this:
    Code:
    #include <stdlib.h>
    #include <ctype.h>
    
    int main()
    {
        int i = 0;
        char name[] = "namE";
    
        name[0] = toupper(name[0]);
        for(i = 1; name[i] != '\0'; i++)
            name[i] = tolower(name[i]);
        printf("name = %s\n", name);
    }
    Display name with the first letter of each name capitalized, and the remaining letters lower-case.

  8. #8
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

    Re: Help me PLEASE........

    Originally posted by Unregistered
    cSwitch = * (argv [1]);
    cOption1 = * (argv [1] + 1);
    cOption2 = * (argv [1] + 2);
    cOption3 = * (argv [1] + 3);
    It's dangerous to assume argv[1] is at least 4 characters long. Always check your string length before reading from it (strlen). Here's an example of reading options.
    Code:
    int main(int argc, char *argv[])
    {
    	int i;
    
    	if(argc == 1)
    	{
    		/* show help */
    		return -1;
    	}
    	
    	if(argv[1][0] == '-')
    	{
    		for(i = 1; argv[1][i] != '\0'; i++)
    		{
    			switch(argv[1][i])
    			{
    				case 'f': /* do something */ break;
    				case 'y': /* do something */ break;
    				case 'l': /* do something */ break;
    				default : printf("Invalid argument: -%c\n", argv[1][i]);
    						  break;
    			}
    		}
    	}
    	return 0;
    
    }

  9. #9
    Unregistered
    Guest
    Everyone thanks for your help also Monster, thanks for your advise, that works a lot better than what i have before.

    now how do i put this picec of code into the main or do i put in the format name?

    {
    int i = 0;
    char name[] = "namE";

    name[0] = toupper(name[0]);
    for(i = 1; name[i] != '\0'; i++)
    name[i] = tolower(name[i]);
    printf("name = %s\n", name);
    }

    also, how do i search in the string for the names that were entered and convert the first letter to upper case?

  10. #10
    Unregistered
    Guest
    also, how do i search the string and display only the last name....

    printf ("Students by last name only\n");
    printf ("----------\n");
    printf ("%s\n", argv[]);

    what should i modify?

  11. #11
    Unregistered
    Guest
    help please, i don't know why this doesn't work, what i am trying to do it when it is case 'f', it will get the names from the command line that the user entered like "hw6 -f john smith 10/21/2002 the rock 02/19/2000" then get the word john smith and the rock and capitalize the first letter of the word and print them.

    case 'f':

    c = argv[x];
    while (c != '\0')

    {

    lastname [a] = argv[x+1];
    firstname [b] = argv[x];

    a++;
    b++;

    printf ("last name is %s and first name is %s", lastname , firstname);

    c = argv [x+3];

    }

    break;

  12. #12
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Reading first name, last name and date:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      int i = 0;
    
      /* argv[0] = program name, argv[1] = options */
      for(i = 2; i < (argc-2); i+=3)
      {
        printf("First name: %s\n", argv[i]);
        printf("Last name: %s\n", argv[i+1]);
        printf("Date: %s\n", argv[i+2]);
      }
      return 1;
    }

Popular pages Recent additions subscribe to a feed