Challenge 15

Description

The purpose of this challenge is to read and write files. This challenge creates a pseudo time card from data that exists in a text file, using arrays and character arrays to format data.

Requirements

      1. Using vi, create a file called hours.txt. This file contains 5 numbers as below. You don’t need to write C++ code to create this file.
        8.0 8.5 8.0 8.5 8.0
        
      2. Create an array of doubles to contain 5 values
      3. Create 3 c-strings: full[40], first[20], last[20]
      4. Create a function void load_hours(double data[]). This function will read the hours.txt file and read every number into the data array. You can temporarily cout array values in this function to ensure that your file read works.
      5. 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.
      6. 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
        
      7. 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.
      8. 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.

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

  // load the hours

  // create the time card

  return 0;
}

Sample Interaction / Output

What is your name? John Doe
Timecard is ready. See timecard.txt

LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT

CATALOG ID: CPP-CHAL0015

Print Requirements