Struct

What's missing in this program? i'm with some details problems.
I think there's something wrong in the return of the function, but i don't know how to correct.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
#define QUTY 12

int number_of_days (int);

struct months {
       int  num_mon;
       char name[10];
       char abrev[3];
       int  num_days;
};

struct months month[QUTY];

int main () {
    int i, mes;
    
    char *name[] = {"Janeiro", "Fevereiro", "Marco", "Abril", "Maio",
                   "Junho", "Julho", "Agosto", "Setembro", "Outubro",
                   "Novembro", "Dezembro"};
    char *abrev[] = {"Jan", "Fev", "Mar", "Abr", "Mai", "Jun", "Jul",
                    "Ago", "Set", "Out", "Nov", "Dez"};
                   
    for (i = 0; i < QUTY; i++) {
        strcpy (month[i].name, name[i]);
        strcpy (month[i].abrev, abrev[i]);
        if (i == 3 || i == 5 || i == 8 || i == 10)
             month[i].num_days = 30;
        else if (i == 1)
             month[i].num_days = 28;
        else
             month[i].num_days = 31;
        month[i].num_mon = i  + 1;
        
        cout << "Month: " << month[i].name << "(" << month[i].abrev << ")" << "     Number: ";
        cout << month[i].num_mon << "    Days: " << month[i].num_days << endl;
    }
    
    cout << "\nEnter the number of one month: ";
    cin >> mes;
    
    cout << "\n\nTotal days of the year until " << month[mes - 1].name << ": " << number_of_days(mes);
   
    getch();
}

int number_of_days (int a_month) {
    int index, days = 0;
    
    for (index = 0; index < a_month - 1; index++)
        days += month[index].num_days;
   
   return days; 
}


Details of the question:
Write an struct to describe a month of one year.
The struct should be able to store the name of the month, the three-letter abbreviation month, the number of days and the number of the month.
Define a matrix of 12 structs and initialize it with data from a not leap year.
Write a function that takes the number of months and returns the total days of the year until that month. Assume that the struct and the matrix were declared as external.
Last edited on
i noticed that you are not using string but you are inserting strings in a char array at each position at row 21 and 24 why?
also dont forget to include string if you are working with them.



Last edited on
Indeed, be sure to #include <cstring>

Aside from that, the program appears to work fine... what's the issue, may I ask?

-Albatross
I can't believe the problem was only this.
It's working fine now.
Thank you.

Albatross (1592)
what's the issue, may I ask?


The question is under the program.

Tamao (30)
i noticed that you are not using string but you are inserting strings in a char array at each position at row 21 and 24 why?


Do you know a better way to do it?
1
2
3
4
5
 char *name[] = {"Janeiro", "Fevereiro", "Marco", "Abril", "Maio",
                   "Junho", "Julho", "Agosto", "Setembro", "Outubro",
                   "Novembro", "Dezembro"};
    char *abrev[] = {"Jan", "Fev", "Mar", "Abr", "Mai", "Jun", "Jul",
                    "Ago", "Set", "Out", "Nov", "Dez"};


" A" this means it's a string, 'A ' means it's a char. Right now you are inserting strings in a char* array you might want to try
also string is an array of chars a char is a single letter

1
2
3
4
5
6
7
8
#include <string>

 string name[] = {"Janeiro", "Fevereiro", "Marco", "Abril", "Maio",
                   "Junho", "Julho", "Agosto", "Setembro", "Outubro",
                   "Novembro", "Dezembro"};
    string abrev[] = {"Jan", "Fev", "Mar", "Abr", "Mai", "Jun", "Jul",
                    "Ago", "Set", "Out", "Nov", "Dez"};
Last edited on
Topic archived. No new replies allowed.