Question: Convert the following program to C++: Use command-line arguments so that when you run this program, you also include the name of an input file.

Convert the following program to C++:

  1. Use command-line arguments so that when you run this program, you also include the name of an input file. For example, if your input file name is months.txt, you would type a.out theMonths.txt
    • The input file should contain the number of days and the 3 month abbreviation for all 12 months represented as a C++ string instead of a character array.
  2. Instead of declaring an array of 12 months, declare a month_t pointer and dynamically allocate enough memory to hold 12 months.
  3. Use C++ file pointers and open the input file sent in from the command-line.
  4. Create an initializeMonths() function with parameters for the months and the input file
    • Using both arguments, use a loop to read in the number of days and the month for each month from the input file.
  5. Declare a function pointer and assign it to point the printArray() function.
  6. Call the initializeMonths() function.
  7. Call the printArray() using the function pointer.
  8. Use C++ print statements and formatting of output

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx

#include

typedef struct month { int numberOfDays; char name[4]; }month_t;

void printArray(month_t monthArray[]);

int main(void) { struct month months[12] = { {31, "Jan"}, {28, "Feb"}, {31, "Mar"}, {30, "Apr"}, {31, "May"}, {30, "Jun"}, {31, "Jul"}, {31, "Aug"}, {30, "Sep"}, {31, "Oct"}, {30, "Nov"}, {31, "Dec"} }; printArray(months); return 0; } void printArray(month_t monthArray[]) { int i; printf(" "); printf("Month Number of Days "); printf("----- -------------- "); for (i=0; i<12; i++) { printf(" %s %i ", monthArray[i].name, monthArray[i].numberOfDays); } printf(" "); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!