new c++ user array printing

hi i am brand new to c++ so please bear with me
i am trying to make a program that reads in an array of numbers between 1 and 100, as they are read in it will print them unless they are a duplicate number of one already in the array... this is the code i have so far and nothing is working
if someone could show me the code that would do this i would be so grateful, thanks so much!

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
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{ 
	const int SIZE=100;
    int array[SIZE];
    
    cout << "Enter a list of numbers :" ;
    for (int i=0; i<SIZE; i++)
    {   
		cin >> array[i];
		 while (array[i] >=1 && array[i] <= 100);

			 if (array[i] != array[i])
			 {
				 cout << "Numbers not duplicates: " << array[i];
			 }
			 else
			 {
		 
			 }
				 getch();
				 return 0;
		 }
	}
Last edited on
Your if statement will alway be false, since you're checking if a number IS NOT equal to itself, which is never true.

What you can do is fill the array, then print the first number. Then, for each element after that, check to see if it has already been printed. If it has been printed, skip it and move to the next one.
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
const int SIZE = 100;

int main( )
{
    int array[SIZE];
    
    cout << "Enter numbers:" << endl;
    for (int i = 0; i < SIZE; i ++)
    {
        cin >> array[i];
    }
    //This first loop will simply fill the array
    
    cout << array[0] << " "; // this will print the first element
    
    for (int i = 1; i < SIZE; i++)
    {
        bool repeated(false);
        
        for (int j = 0; j < i; j++)
        {
            if (array[i] == array[j])
            {
                repeated = true;
            }
        }
        
        if (repeated == false)
        {
            cout << array[i] << " ";
        }
    }
    
    return 0;
}


I hate to give out code, but this problem could be tricky. Use a boolean to test for repetition (you can either break out or not). And only print the array element if the boolean is false.
Last edited on
this doesn't seem to be working, as i type the numbers e.g. 1111 all of those numbers are appearing? this should only show up as 1?
That is strange.
When I set MAX = 5, I get this:

Enter numbers:
1
1
1
1
1
1 Program ended with exit code: 0

What compiler are you using?
what is MAX = 5 sorry?

visual studio 2012 on windows
this may work :

a little edit to Jayhawk's code :

from line 20:
1
2
3
4
5
6
7
8
9
10
11
12
13
 for (int j = 0; j < i; j++)
        {
 if (array[i] == array[j] && array[i]!=NULL)
            {
                repeated = true;
                array[j]=NULL;
            }
         }
if (repeated == false && array[i]!=NULL)
        {
            cout << array[i] << " "; 
            array[i]=NULL;
        }


Last edited on
got Jayhawks code to work, i'd accidentally deleted some of the lines. Thanks so much for the help!
Visual studio should work fine. Many of us on here use Code::Blocks.

I set MAX = 5 so that I didn't have to type in 100 numbers. If it works for 10, it should also be bale to work for larger numbers of elements.

And what ALOK R said would also work well, but I left out any NULL in case you didn't know about them yet.
Good luck programming!
I used "NULL" for the prevention of repetition, and I couldn't think of any other thing to differentiate integers of the array, so, I used NULL there. But I think NULL's aren't meant for int's.
Topic archived. No new replies allowed.