Challenge 26

Description

The purpose of this challenge is to write files. This challenge creates a pseudo time card from user input, arrays and character arrays to format data.

Requirements

      1. Create an array of doubles to contain 5 values
      2. Prompt the user to enter 5 values that will be stored in the array. These values represent hours worked. Each element represents one day of work.
      3. Create 3 c-strings: full[40], first[20], last[20]
      4. Create a function parse_name(char full[], char first[], char last[]). This function will take a full name such as “Noah Zark” and separate “Noah” into the first c-string, and “Zark” into the last c-string.
      5. Create a function void create_timecard(double hours[], char first[], char last[]). This function will create (write to) a file called “timecard.txt”. The file will contain the parsed first and last name, the hours, and a total of the hours. See the final file below.
        First Name: Noah
        Last Name: Zark
        Day 1 : 8.0
        Day 2 : 8.5
        Day 3 : 8.0
        Day 4 : 8.5
        Day 5 : 8.0
        Total : 41.0
        
      6. In main, you will prompt the user to enter full name only. The parse_name() function will be used to parse full name into first and last.
      7. Don’t forget to close your files in your functions. You may create as many functions as you need in addition to the ones outlined above. Try to keep your main() function clean and get in the habit of separating functionality into separate functions that perform lean tasks.

Do Not Use

The strtok() function

Hint

void parse_name(char full[], char first[], char last[])
{
  // to get this function working immediately, first 
  // stub it out as below. When you have everything else 
  // working, come back and really parse full name

  // this always sets first name to "Noah"
  strcpy(first, "Noah");

  // this always sets last name to "Zark"
  strcpy(last, "Zark");

  // replace the above calls with the actual logic to separate
  // full into two distinct pieces
}

Sample main()

int main()
{
  char full[40], first[20], last[20];
  double hours[5];

  // ask the user to enter full name

  // parse the full name into first and last
  parse(full, first, last);

  // ask the user to enter the hours

  // create the time card
  create_timecard(hours, first, last);

  return 0;
}

Sample Interaction / Output

What is your name? John Doe
Enter hours for day 1: 8.0
Enter hours for day 2: 8.5
Enter hours for day 3: 8.0
Enter hours for day 4: 8.5
Enter hours for day 5: 8.0
Timecard is ready. See timecard.txt

LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT

CATALOG ID: CPP-CHAL0026

Print Requirements