what's wrong in this struct?

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
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
#define QTD 12

struct mesdoano {
       char *nome[10];
       char *abrev[3];
       int  num_dias;
       int  num_mes;
};

int main () {
    int i;
    struct mesdoano mes[QTD];
    
    char *nome[] = {"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 < QTD; i++) {
        mes[i].nome = nome[i];
        mes[i].abrev = abrev[i];
        if (i == 3 || i == 5 || i == 8 || i == 10)
             mes[i].num_dias = 30;
        else if (i = 1)
             mes[i].num_dias = 28;
        else
             mes[i].num_dias = 31;
        mes[i].num_mes = i  + 1;
       
        cout << "Numero do mes: "<< mes[i].num_mes;
        cout << "\tNome do mes: " << mes[i].nome;
       cout << "("<< mes[i].abrev << ")\t";
        cout << "Numero de dias: " << mes[i]num_dias << endl;
    }
  
   getch();
   return 0;
}


I didn't use pointers at first
But it happened an error on lines 20 and 22 that was corrected by using it
i don't know why, and i'm lost now.
what's more wrong in the program?
Last edited on
Please refrain from adding the code numbers yourself, use code-tags instead. (When you scroll to the right, there's a button underneath Format: that looks like this <>)
It's very hard to debug, because we need to remove the line numbers, and it's not readable because indention is removed when not inside a code block.
sorry. now it's ok.
This:

1
2
3
4
5
6
struct mesdoano
{
    char *nome[10];
    char *abrev[3];
    //...
};

Should be:

1
2
3
4
5
6
struct mesdoano
{
    char nome[10];
    char abrev[3];
    //...
};

This:

1
2
mes[i].nome = nome[i];
mes[i].abrev = abrev[i];

Should be:

1
2
strcpy(mes[i].nome,nome[i]);
strcpy(mes[i].abrev,abrev[i]);

You may have to #include <cstring> for this to work.

This -> else if (i = 1) should be -> else if (i == 1)

And, finally, you forgot something here -> mes[i]num_dias
Is there another way only using pointers?!
because my teacher never told us about this string copy
Maerle wrote:
Is there another way only using pointers?!

Yes.

1
2
3
4
5
6
struct mesdoano
{
    char *nome;
    char *abrev;
    //...
};

1
2
mes[i].nome = nome[i];
mes[i].abrev = abrev[i];

That should do it.
Another question.

Why do we have to use pointers in lines 18 and 21?
What you use in lines 18 and 21 is arrays of pointers to char. The first element of nome is a char pointer that points to the address of the first element of the "Janeiro" string, i.e. the address of 'J', the next element of nome is a char pointer that points to the address of 'F' ("Fevereiro") and so on...

Here -> mes[i].nome = nome[i]; you pick the i-th element of your nome array and assign it to the nome data member of your i-th element of your mes array. Both operands are char pointers, so everything is ok.
Last edited on
Topic archived. No new replies allowed.