non-repeating numbs

  • Thread starter Developwebsites
  • Start date
D

Developwebsites

user enters non-repeating 100 numbers.
how do i determine that numbs do not repeat?
is this code correct?
also, once a repeated number has been found, lets say
I enter 345 twice, how do i break out of loop?
could someone test this with random(1-1000).

const int MAX=100;
int Numb[MAX];
bool exists;
cout<<"Enter "<<MAX<<" numbers";
for(int t=0;t<MAX;t++)
{
cout<<'\n';
cin>>Numb[t];
if (t>0) {
do {
exists = false;
for(int x=0;x<t;x++)//should this be a while instead?
{
if (Numb[x]==Numb[t]) {
exists = true;
cout<<"\nNumber "<<Numb[t]<<" already exists.\n";
cin>>Numb[t];
//break goes here?
}//if
}//for
}//do
while(exists == true)
}//if
}
 
M

Mike Wahler

Developwebsites said:
user enters non-repeating 100 numbers.
how do i determine that numbs do not repeat?
is this code correct?
also, once a repeated number has been found, lets say
I enter 345 twice, how do i break out of loop?
could someone test this with random(1-1000).

const int MAX=100;
int Numb[MAX];
bool exists;
cout<<"Enter "<<MAX<<" numbers";
for(int t=0;t<MAX;t++)
{
cout<<'\n';
cin>>Numb[t];
if (t>0) {
do {
exists = false;
for(int x=0;x<t;x++)//should this be a while instead?
{
if (Numb[x]==Numb[t]) {
exists = true;
cout<<"\nNumber "<<Numb[t]<<" already exists.\n";
cin>>Numb[t];
//break goes here?
}//if
}//for
}//do
while(exists == true)
}//if
}

#include <cstdlib>
#include <iostream>
#include <map>

int main()
{
std::map<int, unsigned int> numbers;
int number(0);

while(std::cin >> number)
if(++(numbers[number]) > 1)
{
std::cerr << "The number "
<< number
<< " is a repeat, input terminated.\n";

break;
}

if(!std::cin && !std::cin.eof())
{
std::cerr << "Error reading input\n";
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top