Task for a function with a variable number of heterogeneous parameters

47 views Asked by At

Good morning forum users!))

Task: "Create a function with a variable number of parameters, which for each of its numeric parameters that specifies the ordinal number of the day of the year, determines and displays the corresponding date: < Month name>, for example: 47th day - February 16th. Function parameters ( possibly other than the first one) are numbers specifying the numbers of days in the current year. For each of the days you need to specify the corresponding date or indicate an error (it is advisable to use an auxiliary function to form dates). The result is displayed in the form of a table."

It was necessary to make 2 versions of the program. In the first, all parameters are of the same type, and in the other, they are of different types. The first one works fine, but the second one is a problem: Thats what it must print:

Enter 1 day : 78
Enter 2 day : 56
Enter 3 day : 116
Enter 4 day : 91
Enter 5 day : 260
 ---------------
 |date: 19 mar |
 ---------------
 |date: 25 feb |
 ---------------
 |date: 26 apr |
 ---------------
 |date: 01 apr |
 ---------------
 |date: 17 sep |
 ---------------

And that what it`s actually print:

-------------
Wrong day number
Wrong day number
Wrong day number
Wrong day number
Wrong day number

Tell me what to fix or change in the code:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdarg.h>

void put_data(int);
void get_data(const char* types, ...);

int main(void) {

printf("  -------------\n");
get_data("%d", 47, 116, 200, 89, 26);
return 0;
}

// -------------------------------------------------------------
void get_data(const char* types, ...) {

double d_numb;
int i_numb;
long l_numb;

va_list day;
va_start(day, types);

for (const char* ptr = types; *ptr; ptr++) {
    if (*ptr != '%')
    {
        printf("%c", *ptr);
        continue;
    }
    switch (*++ptr) {
    case 'i':
        for (int i = 0; i < *ptr; i++) {
            i_numb = va_arg(day, int);
            put_data(i_numb);
        }
        break;
    case 'd':
        for (int i = 0; i < *ptr; i++) {
            d_numb = va_arg(day, double);
            put_data(d_numb);
        }
        break;
    case 'l':
        for (int i = 0; i < *ptr; i++) {
            l_numb = va_arg(day, long);
            put_data(l_numb);
        }
        break;
    }
}
    va_end(day);
}

// -------------------------------------------------------------
void put_data(int day) {
    int max_day = 365;
    if (day < 1 || day > max_day) {
        puts("Wrong day number");
        return;
    }
    int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    char months[12][4] = { "jan", "feb", "mar", "apr", "may", "jun",
        "jul", "aug", "sep", "oct", "nov", "dec" };

    int month = 0, s = 0;
    while (s + days[month] < day)
        s += days[month++];
    int d = day - s;
    printf(" |date: %02d %s |\n", d, months[month]);
    printf("  -------------\n");
}
// ------------------------------------------------------------
0

There are 0 answers