Need some quick help, would appreciate

Hello guys, I have problem printing what I just inputted from line 26th. When I try cout in line 48th I don't get the same results. I should get when I cout :

2 3 2
3 2
4 3
2 1 2
2 2

However, that's not the case and every number just becomes 2 :

2 2 2
2 2
2 2
2 2 2
2 2

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>
#include <fstream>
#include <string>

using namespace std;

struct Keleivis
{
    string pavarde;
    int bagazoKiekis;
    int vienetoMase;
};

void skaityti_Faila(Keleivis A[], int& n, int& m, int& x, int& y)
{
    ifstream in("duomenys.txt");

    in >> n;

    in.ignore(80, '\n');

    for(int i = 0; i < n; i++)
    {
        in >> A[i].pavarde >> A[i].bagazoKiekis;
        for(int j = 0; j < A[i].bagazoKiekis; j++)
            in >> A[j].vienetoMase;
        in.ignore(80, '\n');
    }

    in >> m >> x >> y;
}

void tikrinti_Keleivius(Keleivis A[], int n, int m, int x, int y)
{
    int kiekKeleiviu = 0;

    for(int i = 0; i < n; i++)
        if(A[i].bagazoKiekis <= m)
            kiekKeleiviu++;

    cout << kiekKeleiviu << endl;

    int kiekVirsijaKg = 0;

    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < A[i].bagazoKiekis; j++)
            cout << A[j].vienetoMase << " ";
        cout << endl;
    }

}

int main()
{
    int n, m, x, y;
    Keleivis A[100];

    skaityti_Faila(A, n, m, x, y);
    tikrinti_Keleivius(A, n, m, x, y);

    return 0;
}
Your data structure makes no sense to me. On line 24 you read a value into A[i].bagazoKiekis and then you read A[i].bagazoKiekis integers, which suggests you're getting new data that you should be saving somewhere unique, yet you put everything into A[j].vienetoMase, where 0 <= j < A[i].bagazoKiekis. So, the A[0].bagazoKiekis is getting overwritten over and over again.
I think you misunderstood the problem.
I tried to cout while reading values into A[j].vienetoMase and it gave me the right values I have read into them. The whole code from 22nd line to 28th worked perfectly fine.
Hello DdavidDLT,

After translating the best I could I see that the "skaityti_Faila" function reads a file that I do not have and have no idea what it should look like,

Provide the input file, or at least a good sample, to test the program with.

You have these variables int n, m, x, y;. Unfortunately you are the only one who knows what they are for. Give them a proper name so people do not have to waste time trying to figure out what they are.

Andy
I agree with helios.
Your data structure makes no sense.

BTW, you could have easily translated things to english in this short program.
Don't be so lazy next time.
C++ Keyboard thug has logged on with “advice”
I tried to cout while reading values into A[j].vienetoMase and it gave me the right values I have read into them. The whole code from 22nd line to 28th worked perfectly fine.
I'm willing to bet that you printed out the values as you read them. The point that Helios is making is that you're overwriting the data each time through the loop at line 22.

To see this, try changing lines 25 & 26 to:
1
2
3
4
        for(int j = 0; j < A[i].bagazoKiekis; j++) {
            in >> A[j].vienetoMase;
            cout << "A[" << j <<"] = " << A[j].vienetoMase << '\n';
        }
Since original data are too secret to be shared, let’s venture funny hypotheses:
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <chrono>
#include <fstream>
#include <iostream>
#include <random>
#include <string>
#include <vector>


struct Keleivis
{
    std::string pavarde;
    int bagazo_kiekis {};
    int * vieneto_mase { nullptr };
};


// Generate random integers:
int genRndInt( int min, int max )
{
    static std::mt19937 eng {
        static_cast<unsigned>(
            std::chrono::high_resolution_clock::now().time_since_epoch().count()
        )
    };
    std::uniform_int_distribution<> dst( min, max );
    return dst( eng );
}


// Provide random mames from a list:
std::string getRndName()
{
    static std::vector<std::string> names {
        "Adelaide",
        "Denisse",
        "Lennon",
        "Ximena",
        "Sebastian",
        "Norah",
        "Celia",
        "Freddy",
        "Ayden",
        "Tiffany",
        "Harrison",
        "Justin"        
    };
    return names.at( genRndInt(0, names.size() - 1 ) );
}


// Load passengers data in passengers array. Returns array size
// Beware!! All parameters are uninitialize, so arguments contain garbage.
int skaitytiFaila( Keleivis * const arr,
                   int& max_weight,
                   int& x,
                   int& y )
{
/*
    std::ifstream in("duomenys.txt");
    if (!in) {
        std::cerr << "Cannot open 'duomenys.txt'.\n";
        // deal with the problem
    }
*/

    int keleivis_num;
    // in >> keleivis_num;
    keleivis_num = genRndInt( 5, 8 );

/*
    in.ignore(80, '\n');        // Please show an example from the file
*/

    std::cout << "Generating " << keleivis_num << " passengers:\n";
    for (int i {}; i < keleivis_num; ++i) {
        // in >> arr[i].pavarde >> arr[i].bagazo_kiekis;
        arr[i].pavarde = getRndName();
        arr[i].bagazo_kiekis = genRndInt( 1, 4 );

        arr[i].vieneto_mase = new int[arr[i].bagazo_kiekis] {};

        std::cout << "\narr[" << i << "].pavarde: " << arr[i].pavarde
                  << "\narr[" << i << "].bagazo_kiekis: "
                  << arr[i].bagazo_kiekis << '\n';
        for (int j {}; j < arr[i].bagazo_kiekis; ++j) {
            // in >> arr[j].vieneto_mase;
            arr[i].vieneto_mase[j] = genRndInt( 1, 20 );
            std::cout << "\tarr[" << i << "].vieneto_mase[" << j << "]: "
                      << arr[i].vieneto_mase[j] << '\n';
        }
        // in.ignore(80, '\n');    // // Please show an example from the file
    }

    // in >> m >> x >> y;
    max_weight = genRndInt( 10, 15 );
    x = genRndInt( 11, 20 );
    y = genRndInt( 21, 30 );
    return keleivis_num;
}


// Print on screen passenger details:
void tikrintiKeleivius( const Keleivis * const arr,
                        int size,
                        int max_weight,
                        [[maybe_unused]] int x,
                        [[maybe_unused]] int y )
{
    int kiek_keleiviu {};

    for (int i {}; i < size; ++i) {
        if (arr[i].bagazo_kiekis <= max_weight) {
            kiek_keleiviu++;
        }
    }

    std::cout << "\nPassenger summary:\nNumber: " << kiek_keleiviu << '\n';

    for(int i {}; i < size; ++i) {
        std::cout << "arr[" << i << "]: ";
        for(int j {}; j < arr[i].bagazo_kiekis; ++j) {
            std::cout << "vieneto_mase[" << j << "]: "
                      << arr[i].vieneto_mase[j] << ' ';
        }
        std::cout << '\n';
    }
}


int main()
{
    int max_luggage_weight, x, y;
    Keleivis keleivis[100];

    int keleivis_num { skaitytiFaila(keleivis, max_luggage_weight, x, y) };
    tikrintiKeleivius(keleivis, keleivis_num, max_luggage_weight, x, y);
}


Example output:
Generating 8 passengers:

arr[0].pavarde: Justin
arr[0].bagazo_kiekis: 3
        arr[0].vieneto_mase[0]: 6
        arr[0].vieneto_mase[1]: 12
        arr[0].vieneto_mase[2]: 6

arr[1].pavarde: Justin
arr[1].bagazo_kiekis: 1
        arr[1].vieneto_mase[0]: 7

arr[2].pavarde: Justin
arr[2].bagazo_kiekis: 3
        arr[2].vieneto_mase[0]: 18
        arr[2].vieneto_mase[1]: 3
        arr[2].vieneto_mase[2]: 18

arr[3].pavarde: Celia
arr[3].bagazo_kiekis: 3
        arr[3].vieneto_mase[0]: 15
        arr[3].vieneto_mase[1]: 6
        arr[3].vieneto_mase[2]: 20

arr[4].pavarde: Ayden
arr[4].bagazo_kiekis: 3
        arr[4].vieneto_mase[0]: 1
        arr[4].vieneto_mase[1]: 3
        arr[4].vieneto_mase[2]: 6

arr[5].pavarde: Lennon
arr[5].bagazo_kiekis: 3
        arr[5].vieneto_mase[0]: 1
        arr[5].vieneto_mase[1]: 11
        arr[5].vieneto_mase[2]: 8

arr[6].pavarde: Lennon
arr[6].bagazo_kiekis: 4
        arr[6].vieneto_mase[0]: 11
        arr[6].vieneto_mase[1]: 7
        arr[6].vieneto_mase[2]: 5
        arr[6].vieneto_mase[3]: 6

arr[7].pavarde: Harrison
arr[7].bagazo_kiekis: 4
        arr[7].vieneto_mase[0]: 17
        arr[7].vieneto_mase[1]: 18
        arr[7].vieneto_mase[2]: 19
        arr[7].vieneto_mase[3]: 17

Passenger summary:
Number: 8
arr[0]: vieneto_mase[0]: 6 vieneto_mase[1]: 12 vieneto_mase[2]: 6
arr[1]: vieneto_mase[0]: 7
arr[2]: vieneto_mase[0]: 18 vieneto_mase[1]: 3 vieneto_mase[2]: 18
arr[3]: vieneto_mase[0]: 15 vieneto_mase[1]: 6 vieneto_mase[2]: 20
arr[4]: vieneto_mase[0]: 1 vieneto_mase[1]: 3 vieneto_mase[2]: 6
arr[5]: vieneto_mase[0]: 1 vieneto_mase[1]: 11 vieneto_mase[2]: 8
arr[6]: vieneto_mase[0]: 11 vieneto_mase[1]: 7 vieneto_mase[2]: 5 vieneto_mase[3]: 6
arr[7]: vieneto_mase[0]: 17 vieneto_mase[1]: 18 vieneto_mase[2]: 19 vieneto_mase[3]: 17

Topic archived. No new replies allowed.