code won't work ?

i have this assignment creating a c++ program for drugstores and registering drugs in drugstore but this code wont work, dont know how to fix it , the problem is at the line 42, also i havent finished the code yet.
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
58
59
60
61
62
63
  #include <iostream>
using namespace std;
const int nrA=5;
const int nrB=3;
enum OrariPunes
{
    Paradite,
    Pasdite
}orari;
struct Barnat
{
    char EmertimiArtikullit[20],Pershkrimi[20];
    int Sasia,NrArtikullit;
};
struct Barnatoret
{
    char EmriBarnatores[20],PikaEShitjes[20],Lokacioni[20];
    int NrFiskal,NrTelefonit;
    Barnat b[nrA];
    void RegjistroBarnat(Barnat b);

};
int main()
{
    Barnat b;
    Barnatoret Barnatorja[nrB];
    int i,j;
    for(i=0;i<nrB;i++)
    {
        cout<<"Te dhenat per barnatoren "<<i+1;
        cout<<"\nEmri i Barnatores: ";
        cin>>Barnatorja[i].EmriBarnatores;
        cout<<"\nPika e shitjes: ";
        cin>>Barnatorja[i].PikaEShitjes;
        cout<<"\nLokacioni: ";
        cin>>Barnatorja[i].Lokacioni;
        cout<<"\nNumri Fiskal: ";
        cin>>Barnatorja[i].NrFiskal;
        cout<<"\nNumri i Telefonit: ";
        cin>>Barnatorja[i].NrTelefonit;
        cout<<"\nTe dhenat per artikujt: "<<endl;
        cin>>Barnatorja[i].RegjistroBarnat(Barnat b);

    }
}
void Barnatoret::RegjistroBarnat(Barnat b)
{
    int j,i;
    Barnat k[nrA];
    for(j=0;j<nrA;j++)
    {
        cout<<"Emri i Artikullit: ";
        cin>>k[j].EmertimiArtikullit;
        cout<<"Pershkrimi Artikullit: ";
        cin>>k[j].Pershkrimi;
        cout<<"Numri i Artikullit: ";
        cin>>k[j].NrArtikullit;
        cout<<"Sasia e Artikullit: ";
        cin>>k[j].Sasia;

    }
}
Last edited on
Line 25: b is an uninitialized struct and is not subsequently initialized.

Line 42: You're passing this uninitialized struct to RegjistroBarnat(), but that function doesn't do anything with the passed struct.

Lines 52-59: You prompt for a bunch of input, but don't do anything with it. k goes out of scope when you exit the function.

Since you have an array of Barnet objects at line 19, I suspect what you want to do is to initialize that array at line 42. If that's your intent, then try the following:
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
 
#include <iostream>
using namespace std;
const int nrA=5;
const int nrB=3;
enum OrariPunes
{   Paradite,
    Pasdite
}orari;
struct Barnat
{   char EmertimiArtikullit[20],Pershkrimi[20];
    int Sasia,NrArtikullit;
};
struct Barnatoret
{   char EmriBarnatores[20],PikaEShitjes[20],Lokacioni[20];
    int NrFiskal,NrTelefonit;
    Barnat b[nrA];    
    void RegjistroBarnat(Barnat b[]);	// pass array 
};
int main()
{   Barnatoret Barnatorja[nrB];
    for(int i=0;i<nrB;i++)
    {   cout<<"Te dhenat per barnatoren "<<i+1;
        cout<<"\nEmri i Barnatores: ";
        cin>>Barnatorja[i].EmriBarnatores;
        cout<<"\nPika e shitjes: ";
        cin>>Barnatorja[i].PikaEShitjes;
        cout<<"\nLokacioni: ";
        cin>>Barnatorja[i].Lokacioni;
        cout<<"\nNumri Fiskal: ";
        cin>>Barnatorja[i].NrFiskal;
        cout<<"\nNumri i Telefonit: ";
        cin>>Barnatorja[i].NrTelefonit;
        cout<<"\nTe dhenat per artikujt: "<<endl;
        Barnatorja[i].RegjistroBarnat(Barnatorja[i].b);
    }
}

void Barnatoret::RegjistroBarnat(Barnat k[])  // pass array
{   for(int j=0;j<nrA;j++)
    {   cout<<"Emri i Artikullit: ";
        cin>>k[j].EmertimiArtikullit;
        cout<<"Pershkrimi Artikullit: ";
        cin>>k[j].Pershkrimi;
        cout<<"Numri i Artikullit: ";
        cin>>k[j].NrArtikullit;
        cout<<"Sasia e Artikullit: ";
        cin>>k[j].Sasia;
    }
}

Last edited on
oh man thanks, it worked i had no idea what i was doing wrong.Thats exactly what i needed.
Topic archived. No new replies allowed.