473,569 Members | 2,617 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

non-repeating numbs

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
}
--------------------------------------------------
*** E-mail is shut off ***
--------------------------------------------------
Jul 19 '05 #1
1 2128

"Developwebsite s" <de************ *@aol.com> wrote in message
news:20******** *************** ****@mb-m12.aol.com...
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
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3731
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence their dtor is called immediately and not at the end of the function. to be able to use return objects (to avoid copying) i often assign them to a const...
3
12204
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in nonblocking mode in c++? I did something ugly like --- c/c++ mixture --- mkfifo( "testpipe", 777);
25
7569
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
4484
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
8
3494
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I cannot load/unload/reload extensions into my large and slow-to-load application during development without restarting the process then the...
14
8371
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
11
3403
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the vtable pointer is made use of.. eg. class one {
2
6097
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my pointers, but I'm not sure. Please help. The code: void equate(matrix *A, matrix *B) { int i, j; assert(A.row_dim == B.col_dim && A.col_dim ==...
399
12705
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or to python-3000@python.org In summary, this PEP proposes to allow non-ASCII letters as identifiers in Python. If the PEP is accepted, the...
12
29833
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
7701
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8130
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.