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++:
- 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.
- Instead of declaring an array of 12 months, declare a month_t pointer and dynamically allocate enough memory to hold 12 months.
- Use C++ file pointers and open the input file sent in from the command-line.
- 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.
- Declare a function pointer and assign it to point the printArray() function.
- Call the initializeMonths() function.
- Call the printArray() using the function pointer.
- 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started