Problem in searching an array

Hey guys, i am a noob in programing,i would apreciate some help in this issue.

i have to create a program that reads inputes values into an array, and print them out as they are inputed, but if a value is repeated, it does not print it out.

i got this so far:

# include <stdlib.h>
# include <iostream>

using namespace std;

int main()
{
while (1)
{
const int SIZE = 4;
int read,i,fid[SIZE],counter=0;

for (i=0;i<=SIZE;i++)
{
cout<< "\n"<<"Please input value number"<< i+1<<":"<<"\n";
cin>> read;

for (int t=0;t<=SIZE;t++)
if (read != fid[t])
{fid[i]=read;
}
for (int j=0;j<=i;j++) cout<< fid[j]<<";";
}


}
}
can anyone explain how do I check the hole array for an equal value... all i am printing is the exact values the user is inputing...
ps: the array is meant to be the smalest possible, i am just using a [4] to try and solve my problem...
Thanks alot
Firstly you should loop only till fid[SIZE-1]. For example if SIZE = 4, if you do:

for (i=0;i<=SIZE;i++)

you will be reading 5 numbers instead of 4. so you should make it:

for (i=0;i<SIZE;i++)

Also, you will need a counter to keep track of the number of elements filled up in the array. The second loop (the 't' loop) should only check till counter and not SIZE.

You do not need the third 'j' loop. If you find that the inputted element is not present in your array, then you can just print it out.

Hope the above gives you some hints.
Last edited on
Thank you for the reply, i have tryied to follow your tips, but i get the same output... should i be using two arrays for this, it seams such an easy question...

for (i=0;i<SIZE;i++)
{
cout<< "\n"<<"Please input value number"<< i+1<<":"<<"\n";
cin>> fid[i];


for ( t=0;t!=counter;t++)
if (fid[i] != fid[t])
{fid[t]=fid[i];

}
//cout<<fid[t];
counter++;
for (int j=0;j<=i;j++) cout<< fid[j]<<";";
}

}
}


i know its this for loop thats completely wrong... it keeps on just showing the input... the last for is to make sure i know the input is being saved into the array...

Basicaly the program should be like:

input value:1
output:1

input value:2
output:1,2

input value:1
output:1,2

input value:3
output:1,2,3

input value:6
output:1,2,3,6

Sorry for the stupid question
I hope you are initializing counter = 0; before the for loops. Also, instead of

1
2
3
4
5
6
cout<< "\n"<<"Please input value number"<< i+1<<":"<<"\n";
 cin>> fid[i];
for ( t=0;t!=counter;t++) 
if (fid[i] != fid[t])
 {fid[t]=fid[i];
 }


try something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
cout<< "\n"<<"Please input value number"<< i+1<<":"<<"\n";
 cin>> read;
int found = 0;
for ( t=0;t!=counter;t++) 
if (read == fid[t]) {
   found = 1;
   break;  
}
if (!found) {
  fid[i] = read;
  counter++;
}

//also, the j loop should also loop till counter-1 and not i; 
Thank you so much, just changed the last fid[i] = read; to fid[counter]=read; and the print came out exactly as i wanted, thank you... and thanks for getting me thinking ;)
Topic archived. No new replies allowed.