Can someone tell me how to fix my code?

I am supposed to write a program that will read 25 positive numbers from the user and stores them in an array. The user can stop entering numbers by entering -1. The program must search print only the unique numbers the user has entered. It should also print out the total number of numbers entered along with the number of unique numbers entered. Any help you can give would be greatly appreciated! I'm very confused. It would be very helpful if you could point out exactly what I'm doing wrong. The program builds but the output doesn't look right. Thanks!
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
  #include <iostream>
using namespace std;

int main(){
	const int MAX = 25;
	int input[MAX];
	int count = 0;

	do{
		cout << "Enter a number (-1 to stop): ";
		cin >> input[MAX];
		if (input[MAX] != -1 && input[MAX] >= 0){
			input[count++] = input[MAX];
		}
		if (input[MAX] < -1){
			cout << "Invalid input. This will not be included in your values." << endl;
		}
	} while (input[MAX] != -1 && count < MAX);

	for (int i = 0; i < count; i++){
		cout << input[i] << ', ';
	}
	cout << endl;

	int unique[25];
	int count2 = 1;
	int duplicate = 0;

	unique[0] = input[0];

	for (int i = 0; i<count; i++){
		duplicate = 0;

		for (int j = 0; j<count2; j++){

			if (input[i] == unique[j]){
				duplicate = 1;
			}
		}
		if (duplicate == 1){
			continue;
		}
		unique[count2] = input[i];
		count2++;
	}

	for (int i = 0; i < count2; i++){
		cout << "The unique numbers you entered were: " << unique[i] << ' ';
	}


	cout << "You entered " << count << " numbers, " << count2 << " unique." << endl;
	cout << "Have a nice day!" << endl;

	system("pause");
	return 0;
}
Last edited on
First thing I notice is that you have an array called input that holds 25 values, but then you try to write to input[25].

input[25] is out of bounds of the array, and writing to that location will result in undefined behavior (which is bad).
arrays start at index 0, so if 25 is the size of the array, indices go from [0] to [24].
Thanks for answering. :) I changed that. But my results still aren't coming out right. I have no idea what else is wrong. Do you see anything else?
Last edited on
> But my results still aren't coming out right.
Provide an example input.
The output that you should get with that.
The output that you are getting.


Also, show updated code.
Last edited on
Line 21: single quotes enclose individual characters. Double quotes enclose multi-character strings. You want a string here so it should be ", ".

Line 48: Your code will print "The unique numbers you entered were: " for each unique number. You want to print this outside the loop. The loop should only print the numbers and the string to separate them.
Thanks. The problem I'm having is that the code can't figure out which numbers are unique...it lists them all. For example when I enter 3, 4, 5, 5, 6, 7, 8, 8 it should tell me 4 are unique and they are 3, 4, 6, 7
Last edited on
Hmm. Post your current code. I was able to get it working and I think the only changes I made were the ones I described.
Topic archived. No new replies allowed.