I'm trying to write this little program in wich I have to enter by keyboard a certain amount of numbers (int) not floating points, and the program must count how many different numbers I have entered, when the amount is equal to ten, it must say that the amount of different numbers has reached it's limit!

If the limit is not reached I must enter a word or letter like STOP or stop or s or whatever and the program should show me how many different numbers there actually are, wich of course is lower then 10.

I have written this piece of code, but it's not doing what it should do :)

``````int main()
{
int x, y=0, z=0, MAX= 10;

for (int i=0;y<MAX;i++)
{
if (y==MAX)
cout<< "The amount of different numbers has reached its limit!"<<endl;
else
{
cin>>x;
for (int i=0; i<MAX; i++)
if (x!=z)
{
y++;
z=x;
}
}
}
cout<< "The amount of different numbers is: " << y <<endl;``````

I also tried it out with an array because I was not and still am not certain wether this could be done without an array!
I think the control should be done by going threw the array each time a number is entered and that this should determine wether the number allready exists or not, if it exists, the amount should be added by 1, if not, I should be able to continue adding numbers until I decide to STOP!

``````const int amount=10;
int s[amount], x=0,y=0, z=0;

for (int i=0;i<10; i++)
{
cin>>s[i];
++y;
for (int i=0;i<y;i++)
{
if (s[i]!=s[i-1])
x++;
if(x==10)break;
cout<<"The amount of different numbers has reached its limit!";
}
}
cout<<"The amount of different numbers is: "<< x <<endl;

cout<<endl;

return 0;
}``````

## All 24 Replies

Hi all,

Ive changed it abit, but still not working as it should :-|

``````int main()
{
const int amount=10;
int s[amount],n=0, x=0,y=0, z=0;

for (;;)
{
if (x==10)cout<< "The amount of different numbers has reached it's limit!"<<endl;
else
cin>>n;
++y;
for (int i=0;i<y;i++)
{
if (n==s[i])break;

}
x++;
s[i]=n;

}
cout<<"The amount of different numbers is: "<< x <<endl;

return 0;
}``````

You don't say what isn't working, but offhand it looks like 'y' is just getting in the way. There are only x values in s[], so you could use x in your loop and remove y altogether.

I don't see a way to exit if you enter 'stop'; don't you get some sort of exception because you are trying to put a string into an integer?

A third issue is that your loop is constantly setting every entry in s[] to be the current number. It also 'break's if you find the value, which is sorta along the right track, but maybe try something like this:

bool found = false;
for y iterations, if n == s then set found to true and break

after the loop, if found == false, THEN set s[x] to n and increment x.

The break; will only break the innermost loop.

``````#include <iostream>
#include <cstddef>
using namespace std;

int main ( void )
{
int array [ 10 ];
size_t i, j;
for ( i = 0; i < sizeof array / sizeof *array; ++i )
{
cout << "? ";
cin  >> array[i];
for ( j = 0; j < i; ++j )
{
if ( array[i] == array[j] )
{
goto end;
}
}
}
end:
cout << "You entered " << i << " different numbers:\n";
for ( j = 0; j < i; ++j )
{
cout << array[j] << endl;
}
return 0;
}``````

You've encountered one of the few potential uses for a goto. (And no doubt someone will be along soon to assail this.)

In the latest version, you should keep a closer watch of which counter - x or y - is controlling the outer loop.

Damn pokey fingers!

Thanks for the help guys,

@ Chainsaw,
I'm using the variable y as an indicator of how many numbers there are in the array. Replacing this by x isn't possible in my opinion because x is the indicator how many different numbers there are!

The stop is effectively used to end the entering of numbers, because I'm using an integer, when entered a char it will stop.

@ Dave,

Could you explain why you use "i < sizeof array / sizeof *array " as an selection in your loop?

I allways heard that using goto is to be avoided at ALL COSTS :!:

>Could you explain why you use "i < sizeof array / sizeof *array " as an selection in your loop?

The ratio of the total size of the array to its initial member is the number of elements of the array. This is directly tied to the array, not indirectly through the use of a (hopefully) common value used both to declare the array and control the loop.

>I allways heard that using goto is to be avoided at ALL COSTS :!:

That's the kids' version; something like, "because I said so."

Thanks for the info, tried out your version but it doesn't work as It should,
from the moment you enter a number that allready exists, the program stops and says how many different numbers there are, that's not the idea, it should keep on going aslong as the amount of different numbers isn't more then ten.

If you enter a word after a certain amount of numbers it should stop aswell, your program does but adds the word to the amount of different numbers and gives a ZERO in your array and gives a random negative numberafter that aswell in your array.

>tried out your version but it doesn't work as It should

Ah. Sometimes I don't read as closely as I should. It behaved exactly as I wanted, not they way you wanted. Minor changes:

``````#include <iostream>
#include <cstddef>
using namespace std;

int main ( void )
{
int array [ 10 ];
size_t i, j, k = 0;
for ( i = 0; i < sizeof array / sizeof *array; ++i )
{
cout << "? ";
cin  >> array[i];
for ( j = 0; j < i; ++j )
{
if ( array[i] == array[j] )
{
goto end;
}
}
++k;
end:
;
}
cout << "You entered " << k << " different numbers:\n";
for ( j = 0; j < i; ++j )
{
cout << array[j] << endl;
}
return 0;
}

/* my output
? 1
? 2
? 3
? 1
? 2
? 5
? 6
? 7
? 8
? 9
You entered 8 different numbers:
1
2
3
1
2
5
6
7
8
9
*/``````

>If you enter a word after a certain amount of numbers it should stop aswell

Well that increases complexity. You'll have to input text instead of an int (as Chainsaw mentioned already). You'll have to compare this text with the keyword. Then if it's not the keyword, you'll have to convert the text to a number. And then you can enter it into the array.

I think I'm having an off day. Please disregard my replies.

That still doesn't change the fact that when you enter twice the same number, it still stops the count and that's not the idea ;)

Like I said earlier, I'm not certain wether I need to do this with an array, the idea is solely to enter numbers and aslong as the amount of different numbers doesn't equal ten, I could enter one thousand numbers by manner of speaking and it would still run.

It ONLY stops for two reasons,:
1) when after a certain amount of numbers I type in a word or letter.
2) the amount of DIFFERENT numbers is equal to TEN!

``````int main()
{
const int amount=10;
int s[amount],n=0, x=0,y=0;

for (;;)
{
if (x==10) cout<< "The amount of different numbers has reached it's limit!"<<endl;
else
cin>>n;
++y;
for (int i=0;i<y;i++)
{
if (n==s[i])break;
if(s[i]>0 && n!=s[i])x++;
}
s[i]=n;
}
cout<<"The amount of different numbers is: "<< x <<endl;

return 0;
}``````

The problem with my code is that it allways increases x with one even if it allready has done this the previous time for the number.

I'll try and explain a bit better, if I have following numbers entered, 1, 45, 43, 54, 34, 63.

My code will compare them with eachother and increase x everytime it runs trough the array. So, it's like it compare's 1 with the others and this puts x equal to 5 and then it'll compare 45 with the others, meaning 43, 54, 34, 63 and this will put x equal to 9!

Of course, this is wrong because x should be equal to SIX and not NINE :D

DARN DARN, I tried several things, don't know what else I can try.

I also thought that I should fill an array first, but that's just it, the exercise states that the amount of numbers is random, so, you could type in one time five numbers and the next time fifthy, it should still work. Aslong as a) there aren't any more then TEN different numbers b) I don't type in a word or character!

Hello,

I would code this using a bubble-sort (or other sort) that after the input is completed, the numbers in the array are sorted. Then, you can do a compare, and discard the duplicate elements. Then, you can run through the array and count the elements.

Christian

Since I've given such bad advice already, here is some code that works for me. Perhaps you'll hate my style and redo it a bit.

``````#include <iostream>
#include <string>
#include <sstream>
#include <cstddef>
using namespace std;

int main ( void )
{
int array [ 10 ];
size_t i, j;
for ( i = 0; i < sizeof array / sizeof *array; )
{
string line;
cout << "? ";
if ( getline(cin, line) )            // get response as text
{
if ( line == "STOP" )
{
break;
}
istringstream ss(line);
int value;
if ( ss >> value )                // convert to an int
{
for ( j = 0; j < i; ++j )      // look for duplicates
{
if ( array [ j ] == value )
{
goto duplicate;
}
}
array [ i++ ] = value;         // no duplicates -- add to array
duplicate:
;
}
}
}
cout << "You entered " << i << " different numbers:\n";
for ( j = 0; j < i; ++j )
{
cout << array[j] << endl;
}
return 0;
}``````

You may want to take advantage of STL set.

``````// the STL set is a sorted unique element container
// Dev-C++

#include <cstdlib>
#include <iostream>
#include <iterator>   // ostream_iterator

using namespace std;

int main(int argc, char *argv[])
{
set<int> iST;
int num;

cout << "Enter an integer number:\n";
// loop will break after 10 unique integers have been entered
// or a non-numeric value has been entered
while(cin >> num)
{
iST.insert(num);
if (iST.size() >= 10)
break;
}

copy(iST.begin(),iST.end(),ostream_iterator<int>(cout," "));
cout << endl;

system("PAUSE");
return EXIT_SUCCESS;
}``````

Hi guys,

Thanks for the help and examples you have given, tough, there's one thing that is important, that is, this exercise is stated just when you learned about iterations, selections, array's.

I'm sure as you have shown that there are several solutions to this problem, but I would like to stick with the book I'm reading and with the chapter just read, using pointers, and stuff that I haven't even seen or read about yet, it would be much to confusing :o

Vegaseat, thanks for the example, but as said before, you are using code that I haven't even learned or know about ;)

There has to be an easier way, right :?:

Thanks for the help and examples you have given, tough, there's one thing that is important, that is, this exercise is stated just when you learned about iterations, selections, array's.

I'm sure as you have shown that there are several solutions to this problem, but I would like to stick with the book I'm reading and with the chapter just read, using pointers, and stuff that I haven't even seen or read about yet, it would be much to confusing

These are parts you need to focus on developing using what you know:

``````// get response as text
// convert to an int``````

JoBe,

so you are still in the muckamuck of C++ learning. You are asking the right questions. What book are you using?

@ Dave, thanks for the tip.

@ Vegaseat, yes, I'm a very big NOOB still :mrgreen:

I have been following lessons in evening school and have seen Structured Programming and Object Oriented programming.

Problem is, this course was given in a modular system within a time period of FOUR months wich was just to fast for me, that's why I decided to do this course again starting in September but in the mean while I wanted to learn as much as possible on my own.

Therefore, coming to your question, it's a Dutch book from a professor called Leen Ammeraal, he's a Mathematical Engineer and teaches or teached (don't know exactly) at the High School of Utrecht.

That's why you'll see text in my code that's written in Dutch ;)

Hi guys,

Ive been working on this abit more and decided to try out to find the amount of different numbers first and then, when this works, add the stopping part!

What I got untill now is this:

``````#define AMOUNT 20

int main()
{
int s[AMOUNT],n, x=0;

for (int i=0;i<AMOUNT;i++)
{
cin>>n;cin.get();
s[i]=n;
}

for (i=0;i<AMOUNT;i++)
{
for (int j=0;j<i;j++)
if (s[j]==s[i])
x++;
}

i-=x;

cout<<"The amount of different numbers is: "<< i <<endl;

for (i=0; i<AMOUNT;i++)
{
cout<<setw(4)<<s[i];
if (i%AMOUNT==9)cout<<endl;
}

return 0;
}``````

Problem is, the amount of different numbers isn't correct :-|

Can anyone please explain what I'm doing wrong, thank you ;)

Also, is it better or wiser to enter the random numbers directly into the array cin>>s; or is it better to use another variable :?:

Found the cause of the problem, seems everytime I enter more then twice the same number it counts this of course as also the same number and increments x by one!

If I put in twenty random numbers from wich there are never more then two the same, there's no problem, but then again, that's not the idea of this exercise :cheesy:

Hi guys,

Sorry to bother you again with this stupid exercise, but I can't get it to work properly :o

``````#define AMOUNT 5

int main()
{
int s[AMOUNT], x=0;

for (int i=0;i<sizeof s / sizeof *s;i++)
cin>>s[i];cin.get();

for (i=0;i<sizeof s / sizeof *s;i++)
{
for (int j=0;j<i;j++)
if (s[i]==s[j])
{
x++;
break;
}
}

i-=x;

cout<<"The amount of different numbers is: "<< i <<endl;

for (i=0;i<sizeof s /sizeof *s;i++)
{
cout<<setw(4)<<s[i];
if (i%AMOUNT==9)cout<<endl;
}

cout<<endl;

return 0;
}``````

The problem is that when I enter a word like STOP, the program does stop but it does show the amount of numbers that have to go into the array.

I'll try to explain this abit better, when the array is equal to five as in the example, and I enter three numbers and then enter 'STOP', the other two places in the array get filled up aswell, how can I avoid that?

Thanks for the help guys and sorry to keep bringing this up, but I'm not going to start another exercise before I get this one to work properly :!:

The problem is that when I enter a word like STOP, the program does stop but it does show the amount of numbers that have to go into the array.

I'll try to explain this abit better, when the array is equal to five as in the example, and I enter three numbers and then enter 'STOP', the other two places in the array get filled up aswell, how can I avoid that?

[post]81443[/post]
Look at the loop control for obtaining data. Then look at the loop control for printing the data. They are not identical.

But the solution has to be made without you having to convert a string into an integer.

Converting is something that is not shown before this exercise, so, actually, it doesn't exist yet :-|

[post]81443[/post]
Look at the loop control for obtaining data. Then look at the loop control for printing the data. They are not identical.

But the solution has to be made without you having to convert a string into an integer.

Converting is something that is not shown before this exercise, so, actually, it doesn't exist yet :-|

I was trying not to spoonfeed you the difference between this

``````int i;
for (i=0;i<sizeof s / sizeof *s;i++)
{
// obtain data
}
//...
for (i=0;i<sizeof s / sizeof *s;i++)
{
// print data
}``````

and this

``````int i;
for (i=0;i<sizeof s / sizeof *s;i++)
{
// obtain data
}
//...
int j = i;
for (i=0;i<j;i++)
{
// print data
}``````

as it pertains to this

I'll try to explain this abit better, when the array is equal to five as in the example, and I enter three numbers and then enter 'STOP', the other two places in the array get filled up aswell, how can I avoid that?

So why are you mentioning conversion?

Note here:

``````for (int i=0;i<sizeof s / sizeof *s;i++)
cin>>s[i];cin.get();

for (i=0;i<sizeof s / sizeof *s;i++)``````

This is an MSVC mistake (Catch-22).

DOH :o

Now I understand it :D I tought you where referring to this part in the loop:

``````string line;
cout << "? ";
if ( getline(cin, line) )            // get response as text
{
if ( line == "STOP" )
{
break;
}``````

Here's a nifty little code
Be wary though. You can only enter 100 numbers at a time. If you want to add more, either increase your array size, or try dynamic initialization. You should be knowing that if you know pointers. :?:

``````#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main()
{
int flag=0;		//Flag variable
int count=1;	//Counting variable
char ans;		//Contains your answer whether you want to exit the loop or not
int x;
int i;
int j;
for(i=0; i<100; i++)
{
flag=0;
cout<<"\n Enter number "<<(i+1)<<": ";	//(i+1) because C++ is zero-based counting
cin>>x[i];
cout<<"\n Do you want to stop? Y/y/N/n: ";
cin>>ans;
if(ans=='Y' || ans=='y')
break;
for(j=0; j<i; j++)
{
if(x[i]==x[j])
flag=0;
else
flag=1;
}
if(flag==1)
++count;
//Please note that flag keeps oscillating from 0 to 1 as the array is searched
//which means you will have to reinitialize the flag variable everytime
if(count==10)
break;
}
cout<<"\n You have entered "<<(count)<<" distinct numbers";
getch();
}``````

It worked for me! Please add to my reputation if you like what you see. :cheesy:
Me :D ;) :cheesy: :rolleyes:

Hi jeanatkin,

Thanks for the offered help, but, I think I got it working now :D

``````#define AMOUNT 30
#define HOWMANY 10
#define WIDTHARR 9

int main()
{
int s[AMOUNT], i=0, x=0;

while (i<AMOUNT)
{
cin>> s[i];
if (cin.fail())
{
cin.clear();
break;
}
i++;
}

for (int u=0;u<i;++u)
{
for (int j=0;j<u;j++)
if (s[u]==s[j])
{
x++;
break;
}
}

i-=x;

if (i>=10)
cout<<"The amount of different numbers is bigger then TEN ! "<<endl;
else
cout<<"The amount of different numbers is: "<< i <<endl;

int j=i;

for (i=0;i<j;i++)
{
cout<<setw(4)<<s[i];
if (i%HOWMANY==WIDTHARR)cout<<end
}

cout<<endl;

return 0;

}``````

If you think I could change something wich would improve the code, then by all means, tell me :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.