(Help)Counting numbers typed

Im making a program tha needs to type series of numbers until i enter any negative number or exeed 100 the loop stops and counts and displays the number of times i typed a specific number and displays the most number of times i entered the specific number (I am not allowed to use array, just loops and if..else)

Heres a code but was unfinished because my teacher said if i typed it one by one it would be wrong cuz there is a shorter code for that. But i want to learn what is this shorter code is...



#include <iostream>
using namespace std;
main ()
{
int x=0,count0=0,count1=0,count2=0;

cout<<"Enter a series of numbers : ";
for (x; x>=0;)
{

cin>>x;
if (x==0)
{
count0++;
}
else if (x==1)
{
count1++;
}
else if (x==2)
{
count2++;
}
}
cout<<"Number\t"<<"Times"<<endl;
if (count0>0)
{
cout<<"0\t"<<count0<<endl;
}
if (count1>0)
{
cout<<"1\t"<<count1<<endl;
}
if (count2>0)
{
cout<<"2\t"<<count2<<endl;
}



return 0;
}
Last edited on
I think you're expected to use an array to hold the counts. That way you don't have 100 different named counters.
This is why i am so confused
If anyone has a good idea pls help me.
Unless you first ask for the specific number, there is no way to do this without some form of array. (Sure, you could do all kinds of fancy tricks with the stack or something, but as you don't know what I mean I doubt it would be a correct answer.)

Perhaps you (or we) are misunderstanding something about your assignment?
Do you have the exact words to your assignment?

Hope this realy help and give you a idea!!!
int main () {
int x,counter=0;
int tabl[101]={0};

cout << "Enter a series of numbers : ";
do {
cin >> x;
if (x>=0 && x<=100) {
tabl[x]++;
}
counter++;
} while (x>=0 && counter<100 ) ;
int i;
for (i=0,cout << "Number\t" << "Time" << endl;i<=100;i++) {
if (tabl[i] > 0 ) {
cout << i << "\t" << tabl[i] << endl;
}
}
return 0;
}
I wanna add and display the most number of times the specific number is entered
cout<<"The numbers occurred most is ";

But i dont know how to do this pls help
Last edited on
The problem is, you're not being clear about your problem.

We can't talk about what you're doing and stumble on your problem and provide help. You have to be clear about what it is you have seen that you don't understand. A comments like "This is why i am so confused" doesn't help anyone.
closed account (48T7M4Gy)
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
#include <iostream>

using namespace std;

int main()
{
	const int limit = 100;
	int x = 0, counter = 0;
	int tab[limit] = { 0 };
	
	do {
		cout << "Enter a series of numbers: ";
		cin >> x;
		if (x >= 0 && x < 100)
		{
			tab[x]++;
			counter++;
		}
	} while (x >= 0 && counter < 100);

	for (int i = 0; i < limit; i++)
	{
		if (tab[i] > 0)
			cout << "Number: " << i << " occurred " << tab[i] << " times" << endl;
	}

	return 0;
}
Topic archived. No new replies allowed.