While loop

Good Morning Coders,
I need a quick help with my while loop, I have a dynamically allocated array that increases with each input of an integer value, I wish to exit the loop once the user enters -1 and not have that value in the array. I've tried a combination of ideas but I'm stumped. Here is what i have so far
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
int main(int argc, char const *argv[])
{
	int index = 1;
	int* iptr = new int[index];
	int count = 0;



	do //input will continue till -1 is entered
	{
		cout << "Enter Integer Value: ";
		cin >> iptr[index];
		cout << "1" << endl;
		if(iptr[index] != -1)
		{
			cout << "2" << endl;
			count++;
			if(count >= index)
			{
				cout << "3" << endl;
				index++; //increase by 1
				int* temp = new int[index];//create a new bigger array
				for(int i = 0; i < index; i++)//copy old array to new array
				{
					temp[i] = iptr[i];
					cout << "4" << endl;
				}
				delete [] iptr;//free up old array memory
				iptr = temp;//points to new array
				cout << "5" << endl;
			}
		}	
		cout << "6" << endl;
	}while(iptr[index] != -1);
Last edited on
1
2
3
4
5
int x;
while((std::cin >> x) && x != -1)
{
    //deal with x
}
tried that the other day, for some reason it hangs before entering the loop
If that were true, your code would never reach line 13 where it outputs "1".
i got it, sorry you were right my cout was in the wrong place :).

although now i am thoroughly confused. I believe i have a memory leak somewhere as it runs sometimes and other times crashes. here is the full code
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 <iostream>
using namespace std;

void SelectSort(int [], int);
float MeanCalc(int [], int);
float MedianCalc(int* [], int);

int main(int argc, char const *argv[])
{
	int index = 0;
	int input;
	int* iptr = new int[index];
	int count = 0;


	cout << "Enter Integer Value: ";
	while((cin >> input) && input != -1) //input will continue till -1 is entered
	{
		if(input != -1)
		{
			iptr[index] = input;
			cout << "1" << endl;
			cout << "2" << endl;
			count++;
			if(count >= index)
			{
				cout << "3" << endl;
				index++; //increase by 1
				int* temp = new int[index];//create a new bigger array
				for(int i = 0; i < index; i++)//copy old array to new array
				{
					temp[i] = iptr[i];
					cout << "4" << endl;
				}
				delete [] iptr;//free up old array memory
				iptr = temp;//points to new array
				cout << "5" << endl;
			}
		}	
		cout << "6" << endl;
		cout << "Enter next integer: ";
	}

	SelectSort(iptr, index); //sorts the input into ascending order for MedianCalc

	for(int i = 0; i < index; i++)//error checking the sort algorithm works
	{
		cout << "X" << iptr[i] << endl;
	}

	cout << "The Mean of the Numbers Entered is : " << MeanCalc(iptr, index) << endl; //calls the MeanCalc and outputs mean

	//MedianCalc(ptrarr, index);



	return 0;
}

void SelectSort(int sort[], int size) // select sorting algorithm
{
	int min_i;
	int min_e;

	for(int i = 0; i < (size - 1); i++)
	{
		min_i = i;
		min_e = sort[i];
		for(int j = i + 1; j < size; j++)
		{
			if(sort[j] < min_e)
			{
				min_e = sort[j];
				min_i = j;
			}
		}

		sort[min_i] = sort[i];
		sort[i] = min_e;
	}

}

float MeanCalc(int input[], int index)// calculates the mean
{
	int SumTotal;
	int* ptr;
	float Mean;

	ptr = input; //allocates pointer to first element in array

	for(int i = 0; i < index; i++)
	{
		SumTotal += (*(ptr + i));//adds the elements together.. is this pointer arithimetic though?
	}

	Mean = SumTotal / index;

	return Mean;
}

/*float MedianCalc(int* ptrarr[], int index)
{
	float Median;
	float middle;

	if(index % 2 == 0)
	{
		middle = (index / 2) - 1;
		cout << *ptrarr[middle] + *ptrarr[middle + 1] << endl;
		cout << *ptrarr[middle + 1] << endl;
		Median = (*ptrarr[middle] + *ptrarr[middle + 1]) / 2;
		cout << Median << endl;
	}
}*/


any help would be great
On line 12 you dynamically create an array of size 0. Line 21 is then guaranteed undefined behavior.
sorry but what do you mean?
weve only just started learning this
Line 10 index has a value of 0 then on line 12 when you create your dynamic array of size index you are creating an empty array.
oh ok I see what you mean, if i change the value to 1 will that solve the issue?

when i cout my sort as well im getting some random values. what is the cause of that?

this is with the input 15 -9

1
2
3
4
5
6
7
8
9
10
11
Enter next integer: -1
X9
X11
X12
X13
X14
X15
X10098048
X117242368
The Mean of the Numbers Entered is : 1.59176e+00
Last edited on
oh ok I see what you mean, if i change the value to 1 will that solve the issue?


No. You will continue to access outside the bounds of the array. See lines 29 and 21.
ok im going to have go back over pointers
Topic archived. No new replies allowed.