code crashes at run time but compiling fine

ok so this code crashes when i run, but it compiles fine..can anyone look throught and tell me why? thanks in advance

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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
 #include <iostream>
#include <vector>
#include <fstream>

 /* given an array of 50 numbers, dived the positions by five..
 so we assume a position one or zero after every five numbers, the
 aim here is to take the first two numbers after every five count, but on the condition
 that the first two numbers of every five position count taken must not have values which repeat..
 if there is a value repeating, then we go and  look at the the third position of every five count,
 making sure the value there is not in the group of first two of every five count, we replace a repetive number
 with one of the values of the group of third positon of every five count...all in order!
 Ist group: vector<int>cont == first two values of every five count
 2nd group: vector<int>cont1 == third values of every five count
 3rd group: vector<int>cont3 == Ist group with repetitive numbers replaced by values of 2nd group

don't look at the struct much, i had wanted to load some values in a linked list but i changed idea..
 */
 const int arr [50]={
 2,34,32,34,56,
 76,87,98,3,4,
 3,4,3,4,5,6,
 7,55,67,65,67,
 6,57,12,45,34,
 59,67,57,4,34,
 33,45,67,87,98
 ,38,45,65,23,12,
 12,32,35,76,87,
 45,82,98,78,};

struct Nodo
 { int valore;
   Nodo *nextPtr;
 };
Nodo * testa = 0;

int vll, y = 0;
void putinorder ( const int * pt1,  const int * pt2);
void replace ();
void add_value();
void print_on_screen(std::vector<int>);
void write_to_file (std::vector<int>);
void inserimento(Nodo * &testa, int numero);
std:: vector <int> cont;
std:: vector <int> cont2;
std:: vector <int> cont3;
std:: vector<int>::iterator chk1 ;
 std::vector<int>::iterator chk2;
std::vector<int>::iterator pt3 = cont2.begin();


int main()
{

putinorder(&arr[0], &arr[49]);
print_on_screen(cont3);
 write_to_file (cont3);
return 0;
}

void putinorder ( const int * pt1,  const int * pt2)
{


 int y = 0, v0 = 0,  v1=1, v2=2, v5=5, v6=6, v7=7, v10=10, v11=11, v12=12, v15=15, v16=16, v17=17;
 int v20= 20, v21=21, v22=22, v25=25, v26=26, v27=27, v30=30, v31=31, v32=32;
   int v35=35, v36=36, v37=37, v40=40, v41=41, v42=42, v45=45, v46=46, v47=47;

   cont.push_back(arr[v0]); cont.push_back(arr[v1]);  cont.push_back(arr[v5]);  cont.push_back(arr[v6]);  cont.push_back(arr[v10]);
   cont.push_back(arr[v11]);  cont.push_back(arr[v15]);  cont.push_back(arr[v16]);  cont.push_back(arr[v20]);  cont.push_back(arr[v21]);
   cont.push_back(arr[v25]);  cont.push_back(arr[v26]);  cont.push_back(arr[v30]);  cont.push_back(arr[v31]);  cont.push_back(arr[v35]);
   cont.push_back(arr[v36]);  cont.push_back(arr[v40]);  cont.push_back(arr[v41]);  cont.push_back(arr[v45]);  cont.push_back(arr[v46]);

   cont2.push_back(arr[v2]);    cont2.push_back(arr[v7]);     cont2.push_back(arr[v12]);     cont2.push_back(arr[v17]);     cont2.push_back(arr[v22]);
   cont2.push_back(arr[v27]);     cont2.push_back(arr[v32]);     cont2.push_back(arr[v37]);     cont2.push_back(arr[v42]);     cont2.push_back(arr[v47]);

   std::vector<int>::iterator pt3 = cont2.begin();
   std::vector<int>::iterator Ist_item = cont.begin();
   std::vector<int>::iterator lst_item = cont.end();

   for ( chk1 = Ist_item; chk1 != lst_item; chk1 ++)
   { for (chk2 = Ist_item ; chk2 != lst_item; chk2 ++)
         {
             if (chk2 != chk1 && *chk2 == *chk1)
             {   //std::cout<<"there is a repeat and am gonna replace!";
                add_value();

             }

             else
                 vll = *chk1;
                 cont3.push_back(vll);
              //  inserimento (testa, vll);
         }
   }

 //print_on_screen(testa);
cont.empty();
cont2.empty();
}


   void add_value()
   {
       int num = *pt3;
    for (std::vector<int>::iterator pt  = cont.begin();  pt != cont.end(); pt++)
      {
           if (num == * pt)
             {
            pt3++;

      /*


         y++;
        if ( y >= cont2.size())
           {
            std::cout<<" we exceeded the replacing numbers, please insert number to use! waiting...";
            int k;
           std::cin>> k ;
           cont3.push_back(k);
           inserimento(testa, k); */

           }

    else
        cont3.push_back(num);
       // inserimento(testa, num);
     }
   }



   void inserimento(Nodo * &testa, int numero)

{
    Nodo *temp = new Nodo;

    temp->valore = numero;

    temp->nextPtr = 0;

    if (testa==0) testa=temp;
    else
    {
    Nodo *curr=testa;
    while (curr->nextPtr!=0) curr=curr->nextPtr;
    curr->nextPtr = temp;
    }
}


void print_on_screen(std::vector<int>)
{
    std::cout << std::endl << std::endl;

   for(std::vector<int>::iterator ptr = cont3.begin(); ptr != cont3.end(); ptr ++)

   std::cout << " , " << *ptr;// << std::endl;

}

void write_to_file (std::vector<int>)
 {
     std::fstream file;
     file.open("organizeddddd.txt",std::ios::app);
     if (file.is_open())
     {    file<<"{";
         for(std::vector<int>::iterator ptr = cont3.begin(); ptr != cont3.end(); ptr ++)
             {
         file << "," << *ptr ;
             }
             file<<"},"<<std::endl;
             std::cout<<"writing to file was successful...";
     }

     else
        std::cout<<"Could not open file, corrupt or does not exist in location...";

 }




int num = *pt3;
dereferencing a garbage pointer.

You shouldn't be doing stuff like this:

 
std::vector<int>::iterator pt3 = cont2.begin();

outside of "everything" i.e. at a global level.


Edit: this is massively complicated for what you're trying to achieve. For example:

1
2
3
4
5
6
7
cont.push_back(arr[v0]); cont.push_back(arr[v1]);  cont.push_back(arr[v5]);  cont.push_back(arr[v6]);  cont.push_back(arr[v10]);
	cont.push_back(arr[v11]);  cont.push_back(arr[v15]);  cont.push_back(arr[v16]);  cont.push_back(arr[v20]);  cont.push_back(arr[v21]);
	cont.push_back(arr[v25]);  cont.push_back(arr[v26]);  cont.push_back(arr[v30]);  cont.push_back(arr[v31]);  cont.push_back(arr[v35]);
	cont.push_back(arr[v36]);  cont.push_back(arr[v40]);  cont.push_back(arr[v41]);  cont.push_back(arr[v45]);  cont.push_back(arr[v46]);

	cont2.push_back(arr[v2]);    cont2.push_back(arr[v7]);     cont2.push_back(arr[v12]);     cont2.push_back(arr[v17]);     cont2.push_back(arr[v22]);
	cont2.push_back(arr[v27]);     cont2.push_back(arr[v32]);     cont2.push_back(arr[v37]);     cont2.push_back(arr[v42]);     cont2.push_back(arr[v47]);


could be massively simplified using loops.
Last edited on
sorry i couldn't figure out a loop that could be jumping from one place to another on an array..can you please give an example, mutexe? thank you..

i will try to do std::vector<int>::iterator pt3 = cont2.begin(); locally, but what do you mean by dereferencing a garbage pointer? i mean what do you mean by "garbage"???
thanks..am glad you correct me but i will appreciate petty examples to proof your point..thanks
Last edited on
http://www.cplusplus.com/reference/vector/vector/begin/
"Returns an iterator pointing to the first element in the vector."

At this point:
std:: vector <int> cont2;
what do you think the state of your vector is? You are saying "get me a pointer to the first element in my collection, but your collection is empty.

I'm not really sure what you're trying to achieve to be honest.
Last edited on
ok am gonna change that..infact to make helpers understand what am doing..i gave a long introduction on what the code should do...i.e :
1
2
3
4
5
6
7
8
9
10
11
12
13
  /* given an array of 50 numbers, dived the positions by five..
 so we assume a position one or zero after every five numbers, the
 aim here is to take the first two numbers after every five count, but on the condition
 that the first two numbers of every five position count taken must not have values which repeat..
 if there is a value repeating, then we go and  look at the the third position of every five count,
 making sure the value there is not in the group of first two of every five count, we replace a repetive number
 with one of the values of the group of third positon of every five count...all in order!
 Ist group: vector<int>cont == first two values of every five count
 2nd group: vector<int>cont1 == third values of every five count
 3rd group: vector<int>cont3 == Ist group with repetitive numbers replaced by values of 2nd group

don't look at the struct much, i had wanted to load some values in a linked list but i changed idea..
 */

can you suggest a loop which will simplify the part you talk about???
yea i read that and i'm none the wiser sorry.
ok thank you anyways..
given an array of 50 numbers, dived the positions by five


does that mean divide every number in your array by 5?
where are you doing this in your code?

ok i guess my explanation was crap... here we have an array of 50 numbers...am interested in analyzing positions 0 to 4, 5-9,10-14,15-19....45-49... thats what i meant... because the arrays come with 50 numbers in them...and we have to analyze them this way...
so..now lets take the first five..0-4..i take 0and 1 and place them in group 1...5-10, i take 5 and 6 and place them in group 1...10-14..i take 10 and 11 and place them in group 1..and so on till i arrive at 45 and 46 and place them in group 3... now...
in group 2 i put the third value of every 5 of my array...so that is, 2,12,17...till 47...
but group 3 is group 1 with no number which repeats...so if i find a number which repeats in group 3,,,i go and check the first value in group 2...makes sure is not already present in group 3...if present then i move on to the next value..if not present then the repeating number of group 3 is substituted by the current value of group 2...

this explanation should make it a bit clearer...although is not the bests of explanation..lol sorry...
although not exactly what you're looking for, you rule out every 5 element like this:

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
#include<iostream>
#include<vector>


int main()
{
	std::vector<int> arr{
		2, 34, 32, 34, 56,
		76, 87, 98, 3, 4,
		3, 4, 3, 4, 5, 6,
		7, 55, 67, 65, 67,
		6, 57, 12, 45, 34,
		59, 67, 57, 4, 34,
		33, 45, 67, 87, 98
		, 38, 45, 65, 23, 12,
		12, 32, 35, 76, 87,
		45, 82, 98, 78, };

	for (size_t i = 0; i < arr.size(); i++)
	{
		if (i % 5 != 0)
		{
			std::cout << i << std::endl;
		}
	}

	return 0;
}


then where i've outputted to screen you could add these numbers to another vector or something like that.

The logic is not exactly the same, but i've just had 4 beers in the pub for lunch, and i now i need to go home and carry on getting drunk, but hopefully you get the idea.
lol thanks...when you're done with the beer..just add another bottle again on my behalf ;D..thanks...
Topic archived. No new replies allowed.