Not getting same result

I have problem with my code.When i step into functions I get one result(no repetition),but when i debbug in my Watch table(first number is repeating in all other elements of array) .I am using some random functions to generate random name and some random attributes to the object.I have one class that has array of pointers type Step and to classes derived from Step.
Source.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include<iostream>
#include"Workflow.h"
#include"Activity.h"
#include"State.h"
void main()
{
	
	int i;
	Step **n=new Step*[1009];
	Step **k = new Step*[1009];
	Workflow *W1 = new Workflow(2018);
	W1->Definisi("Activity", 1009);
	W1->Definisi("State", 1009);
	n = W1->GetSpecificSteps("Activity");
	k = W1->GetSpecificSteps("State");
	W1->InsertAt(234,"Activity");
	W1->RemoveAt(723);
delete W1;
	
 	system("pause");
	}

Workflow.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once

#include"State.h"
#include"Step.h"
#include"Activity.h"

class Workflow
{
	int duz;
	Step **p;
	int provera=0;
public:
	Workflow(int a);
	Workflow(const Workflow &w);
	~Workflow();
	void InsertAt(int k,const char* tip);
	void RemoveAt(int k);
	Step** GetSpecificSteps(const char* tip1);
	void Definisi(const char* tip,int broj);
	friend ostream& operator<<(ostream &out, const Workflow& w);
};


Workflow.cpp
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
#include "Workflow.h"

Workflow::Workflow(int a)
{
	duz = a;
	p = new Step*[duz];
}

Workflow::Workflow(const Workflow & w)
{
	duz = w.duz;
}

Workflow::~Workflow()
{
	delete[] p;
}

void Workflow::InsertAt(int k,const char* tip)
{ 
	int i;
	if (tip == "Activity")
	{
		p = new Step*[duz + 1];
		for (i = 0; i < duz; i++)
			p[i] = p[i + 1];
		p[k] = new Activity;
		duz++;
	}
	 else if (tip == "State")
	{
		p = new Step*[duz + 1];
		
		for (i = 0; i < duz; i++)
			p[i] = p[i + 1];
		p[k] = new State;
		duz++;
		
	}
	
}

void Workflow::RemoveAt(int k)
{
	int i;
	p = new Step*[duz];
	for (i =k+1; i<duz; i++)
		p[i] = p[i-1];
	duz--;
	
}

Step ** Workflow::GetSpecificSteps(const char * tip1)
{
	Step **f = new Step*[1009];
		if (tip1 == "Activity")
		{
			
			int brkor = 0;
			for (int i = 0; i < this->duz; i++)
				if (p[i]->GetSpecAttribute() == 0 || p[i]->GetSpecAttribute() == 1)
				{
					f[brkor] = p[i];
					brkor++;
				}
		}
		else if (tip1 == "State")
		{
			
			int brkor =0;
			for (int i = 0; i < this->duz; i++)
				if (p[i]->GetSpecAttribute() != 0 && p[i]->GetSpecAttribute() != 1)
				{
					f[brkor] = p[i];
					brkor++;
				}
		}
		else
			exit(1);
	
	return f;
}

void Workflow::Definisi(const char * tip,int broj)
{
	int s=0;
	int i;
	int f=0;
	if (tip == "Activity")
		s = 1;
	if (tip == "State")
		s = 2;
	while ((s == 1) && (provera < duz) && (f<broj))
	{ 
		p[provera] = new Activity;
		provera++;
		f++;
	}
	while ((s == 2)&&( provera < duz) && (f<broj))
	{
		p[provera] = new State;
		provera++;
		f++;
	}


}

ostream & operator<<(ostream & out, const Workflow & w)
{
	for (int i = 0; i < w.duz; i++)
		out << w.p[i] << ',';
		return out;
}

Step.h
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
#pragma once
#include<iostream>
#include<ctime>
#include<string>


using namespace std;
class Step
{
protected:
	const char* naziv;
	const char* tip;
public:
	int Rand();
	int Rand1();
	int Rand7();
	string Randchar();
	const char* Tip() { return tip; }
	Step() :naziv(NULL),tip("Bez Imena"){}
	Step(const char * naziv,const char* tip);
virtual int GetSpecAttribute()=0;
virtual void Prikaz();
virtual ~Step();

};

Step.cpp
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
#include "Step.h"

int Step::Rand()
{
	int a;
	srand(time(NULL));
	a = rand() % 2;
	return a;
}

int Step::Rand1()
{
	int a;
	srand(time(NULL));
	a = rand() % 1000 + 2;
	return a;
}

int Step::Rand7()
{
	int a;
	srand(time(NULL));
	a = rand() % 10+4;
	return a;
	
}

string Step::Randchar()
{
	srand(time(NULL));
	 char alphabet[26] = {'a','b','c', 'd', 'e', 'f', 'g',
						  'h', 'i', 'j', 'k', 'l', 'm', 'n',
						  'o', 'p', 'q', 'r', 's', 't', 'u',
						  'v', 'w', 'x', 'y', 'z' };
	 int a = Rand7();
	string s = "";
	for (int i = 0; i < a; i++)
		s = s + alphabet[rand() % 26];
	
	return s;
}

Step::Step(const char * naziv,const  char * tip)
{
	this->naziv = naziv;
	this->tip = tip;
}

void Step::Prikaz()
{
	cout << naziv << ',' << tip << ',' << GetSpecAttribute();
}

Step::~Step()
{
	delete[] naziv;
	delete[] tip;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once
#include"Step.h"
class Activity:public Step
{
	int obavezna;
public:
	Activity() :Step(Randchar().c_str(),"Activity"), obavezna(Rand()){}
	Activity(const char * a,int d):Step(a,"Activity"),obavezna(d){}
	   friend istream& operator >>(istream &in, Activity &activity);
	   int GetSpecAttribute();

};
istream & operator>>(istream & in, Activity & activity)
{
	in >>activity.obavezna;
	return in;
}

int Activity::GetSpecAttribute()
{
	return obavezna;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#pragma once
#include"Step.h"
class State:public Step
{
	int prioritet;
public:
	State():Step(Randchar().c_str(),"State"),prioritet(Rand1()){}
	State(const char * n, int d) :Step(n, "State"), prioritet(d){}
	int GetSpecAttribute();
	friend istream& operator >> (istream &in, State &state);
};


int State::GetSpecAttribute()
{
	return prioritet;
}

istream & operator>>(istream & in, State & state)
{
	in >> state.prioritet;
	return in;
}

Some words are in my language ,Serbian.
naziv-name,tip-type,prioritet-oriority(can have length greater than two)
obavezna-obligatory(canhave valuse only one or zero)


Last edited on
Multiple glaring things to fix.

1. Call srand() exactly ONCE at the start of main.
In a short-lived program, time(NULL) returns a constant, which in turn means srand(time(NULL)) resets the prng to the same state, thus ensuring the next call to rand() will return exactly the same number as before.

2. Change all your const char* to std::string.
Because this does NOT work.
> if (tip == "Activity")

3. duz = w.duz;
Look up shallow copy vs deep copy.
You just copy your pointer p (not fully clone everything it points to).
Which means if you eventually delete your w object, then this object's copy of p is now pointing at garbage.

Replace all your roll-your-own dynamic arrays with std::vector.

4. delete[] p;
What about all the p[i] things you also created?
Thank you very much for your answer.Is there away to make naziv(name) and tip(type) somehow char* because that is how is said in my assignment.I did now know how to make all functions with attributes being only char*.We did not do std::vector ,we all supposed to do all our assingnments with array of pointers,or matrix of pointers.Thanks for your advices.
If you're using C-style strings, then you need to learn to use the C standard library string functions. In particular, you need to look at:

http://www.cplusplus.com/reference/cstring/strcmp/
Topic archived. No new replies allowed.