vector of array

closed account (y3091hU5)
Here i have a code that works wrong! But i can't understand what's wrong.

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
int main(){
const int NrSamples = 4;
int NrEvents;
std::ifstream input("Data_Test.txt");

  struct Sample {
  std::string data[NrSamples];
  };
      char str[255];
      int counter = 0;
      int index= 0 ;
      Sample sample;
      vector<Sample> event;

      while(input) {

        input.getline(str, 255); 

           if(input) {

                 index= (int) counter/NrSamples ;

	      cout << counter << ", " << index << ", " <<str << ", "<< endl;                          

                  sample.data[counter-(NrSamples*index)] = str;

                  if (counter%NrSamples == 0)
                  {
                        event.push_back(sample);

                  cout<<"Event: "<<index<<endl;
                  }

		          counter+=1;

            }

        }

        cout << "Event size: "<<event.size()<<endl;
        cout << "Event [1], sample[1]: "<<event[0].data[2]<<endl;
        cout << "Event [3], sample[1]: "<<event[3].data[3]<<endl;



and the Data_Test.txt is a text file.it is

122
135
148
156
178
189
256
114
125
136
630
360

Last edited on
Here i have a code that works wrong! But i can't understand what's wrong.

In these cases, it'd be helpful to others if you could post your output log so that we can read your warnings/errors to properly diagnose your issue instead of having to go through an un-tagged code.
closed account (y3091hU5)
The output is :


Analyzing: wave0.txt
0, 0, 122,
Event: 0
1, 0, 135,
2, 0, 148,
3, 0, 156,
4, 1, 178,
Event: 1
5, 1, 189,
6, 1, 256,
7, 1, 114,
8, 2, 125,
Event: 2
9, 2, 136,
10, 2, 630,
11, 2, 360,
12, 3, 111,
Event: 3
Event size: 4
Event [1], sample[1]:
Event [3], sample[1]: 360


This output is wrong. I expect to out put be:

Event:0
0,0,122
1,0,135
2,0,148
3,0,156
Event:1
5,1,178
6,1,189
7,1,256
8,1,114
Event:2
9,2,125
10,2,136
11,2,630
12,2,360
Event size: 4


And then for each Event and each sample that i want i have its correct value.
Last edited on
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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

const int NrSamples = 4;


struct Event{ int data[NrSamples]; };

istream & operator >> ( istream &str, Event &event )
{
   for ( auto &e : event.data ) str >> e;
   return str;
}


int main()
{
   string filename = "Data_Test.txt";
   vector<Event> all;
   ifstream input( filename );
   for ( Event e; input >> e; ) all.push_back( e );

   for ( int n = 0, counter = 0; n < all.size(); n++ )
   {
      cout << "Event: " << n << '\n';
      for ( int j = 0; j < NrSamples; j++ ) cout << ++counter << "," << n << "," << all[n].data[j] << '\n';
   }
}


Event: 0
1,0,122
2,0,135
3,0,148
4,0,156
Event: 1
5,1,178
6,1,189
7,1,256
8,1,114
Event: 2
9,2,125
10,2,136
11,2,630
12,2,360
closed account (y3091hU5)
May explain that what do you do? and can you tell me what is the problem of my code that do not work correctly?
@badoom,
You haven't given complete code, so it is difficult to test. However,...

Look at the order you are writing things in.
Line 23 you write the data:
cout << counter << ", " << index << ", " <<str << ", "<< endl;
THEN on line 31 you write the header:
cout<<"Event: "<<index<<endl;
Obviously that is the wrong way round.


On line 23 you also write a spurious extra comma at the end.


You can test the stream at the same time as inputting.
1
2
3
4
5
while(input) {

        input.getline(str, 255); 

           if(input) {

could be condensed to
while( input.getline(str, 255) ) {
thus removing two unnecessary levels of nesting (and being more idiomatic).


If your data is anything numerical then you would be far better using int[] or double[] rather than storing it in a string[].


You seem to be confused over what is an "Event" and what is a "Sample". That is a data-understanding issue, not a C++ one.
Last edited on
closed account (y3091hU5)
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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

const int NrSamples = 4;


struct Event{ int data[NrSamples]; };

istream & operator >> ( istream &str, Event &event )
{
   for ( auto &e : event.data ) str >> e;
   return str;
}


int main()
{
   string filename = "Data_Test.txt";
   vector<Event> all;
   ifstream input( filename);
   for ( Event e; input >> e; ) all.push_back( e );

   for ( int n = 0, counter = 0; n < all.size(); n++ )
   {
      cout << "Event: " << n << '\n';
      for ( int j = 0; j < NrSamples; j++ ) cout << ++counter << "," << n << "," << all[n].data[j] << '\n';



   }
   cout<<all[1].data[5]<<endl;
}




Event: 0
1,0,122
2,0,135
3,0,148
4,0,156
Event: 1
5,1,178
6,1,189
7,1,256
8,1,114
Event: 2
9,2,125
10,2,136
11,2,630
12,2,360
 all is : 178


for n=0 and j=4 the code show us 178 whereas value of 178 is for n=1 not n=0 and so on.
cout<<all[1].data[5]<<endl;
That is looking at n=1 and j=5 (WELL AND TRULY OUT OF BOUNDS: goes from j=0 to 3 here)



for n=0 and j=4 the code show us 178 whereas value of 178 is for n=1 not n=0 and so on. 

No idea what you are doing here (j=4 is impossible; j ranges from 0 to 3) : look back at my output:
.....
Event: 1
5,1,178
.....

That quite clearly shows 178 for n=1.
closed account (y3091hU5)
Yes, j=0 is out of range. j can be 0,1,2,3. But when you cout all[1].data[5] the result is 178. Test what i say.
Last edited on
closed account (y3091hU5)
@lastchance the type of my data must be string.about
cout << counter << ", " << index << ", " <<str << ", "<< endl;
I use comma because of the output form be for example 1,0,122.
unfortunately i cant understand why my code do not work correctly yet!
The event is a vector of an array that its name is sample.actually each member of vector is an array. My vector name is event and my array name is sample.
Last edited on
@badoom,
When you use all[1].data[5] then you are using j=5. This is OUT OF BOUNDS!!! If you attempt to use this then you could be accessing memory from just about anywhere. DON'T GO BEYOND THE BOUNDS OF CONTAINERS.

Your output requires commas in the MIDDLE of the line. It does not need the last comma at the END.

I'm afraid that I can't understand what you are trying to say after that. In this and your other thread people are not able to understand you. Please try to translate into better English, and think carefully how you ask questions.
Last edited on
closed account (y3091hU5)
@lastchance i know that j=5 is out of rang but my code will be use by another people in future that he or she don't know that j=5 is out of rang. The person that in future use this code have not any information about detail.he or she just take the outputs.
because of it i emphasize that when n=0 and j=5 we must have not output. Then the person realized that each event just have 4 samples.
I find the answer. This is a undefined behavior in c++.
Topic archived. No new replies allowed.