HELP! While loop?

Hello everybody, this is my first post of probably many to come.

I'm currently taking a basic C++ course and am stuck on one of my assignments, and was seeking some help, advice, anything; I am at my wit's end with this seemingly simple problem.

The question I am stuck on requires having the user input a "base 5 number"
(a number containing only 0s, 1s, 2s, 3s, or/and 4s)

I must test the number to see whether it is in fact a base 5 number, if so, I have to output the number of occurences of each number (number of 0s, number of 1s, etc.)

I have looked online already for advice but most of what I've found uses things that haven't been covered in the course material, and it's very important to me that I understand and grasp the course-related material.

For example, I know most people would use "arrays" for this, but I can't do that in this case.

So to clarify exactly what I am looking for then, is to be able to perform what is needed in this question, using WHILE loop.

Here's one of my attempts:
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
using namespace std;

int main()
{
	int zeros = 0;
	int ones = 0;
	int twos = 0;
	int threes = 0;
	int fours = 0;

	int number;


	cout << "Please enter a base 5 number\n";
	cin >> number;
	cout << endl;

	while (cin.get() == '0')
	{
		zeros++;
	}

	while (cin.get() == '1')
	{
		ones++;
	}

	while (cin.get() == '2')
	{
		twos++;
	}

	while (cin.get() == '3')
	{
		threes++;
	}

	while (cin.get() == '4')
	{
		fours++;
	}

	cout << "Number of Zeros: " << zeros;
	cout << endl;
	cout << "Number of Ones: " << ones;
	cout << endl;
	cout << "Number of Twos: " << twos;
	cout << endl;
	cout << "Number of Threes: " << threes;
	cout << endl;
	cout << "Number of Fours: " << fours;
	cout << endl;
	

	return 0;
}

I've tried with and without the single quotations around the numbers, and many other minor variations of the code to no avail.
Also I believe I'm missing an "if" statement to test if it is in fact a base 5 number before using the "whiles" but I just can't think of a way to test that

Perhaps something like:
1
2
while (cin.get() != '0' || '1' || '2' || '3' || '4')
......

?

and either way, the "whiles" aren't even counting in this case and I can't figure out why!



Also, the reason why I chose to try this method (aside from requiring the use of a while loop and the other associated course-related topics), I used the same method in a previous program that worked perfectly. The previous one was essentially a user inputting a string and I had to count and output the number of characters within it (including spaces, symbols, etc.) until the user hits "return". Here's part of that old code:
1
2
3
4
5
6
7
char input;
int counter = 0;

while (cin.get() != '\n')
	{
		counter++;        
	}



Apologies for the long post, I'm just very discouraged with this as it's been many an hour with no progress.
Thank you in advance to anybody who takes the time to read this and provide a response.




tl;dr input base 5 number, count+output number of occurences of each number, must use while loop
Last edited on
I have a solution without the while loop or cin.get(). I know in your question you said you needed those, but it might help.
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
using namespace std;

int main()
{
	int zeros = 0;
	int ones = 0;
	int twos = 0;
	int threes = 0;
	int fours = 0;

	int number;


	cout << "Please enter a base 5 number\n";
	for(int a =0; a < 4;a++)
        {
	cin >> number;
	cout << endl;
    
	if (number == 0)
	{
		zeros++;
	}

	if (number == 1)
	{
		ones++;
	}

	if (number == 2)
	{
		twos++;
	}

	if (number == 3)
	{
		threes++;
	}

	if (number == 4)
	{
		fours++;
	}
}
	cout << "Number of Zeros: " << zeros;
	cout << endl;
	cout << "Number of Ones: " << ones;
	cout << endl;
	cout << "Number of Twos: " << twos;
	cout << endl;
	cout << "Number of Threes: " << threes;
	cout << endl;
	cout << "Number of Fours: " << fours;
	cout << endl;
	

	return 0;
}
Last edited on
The cin.get() is not necessary but the code MUST incorporate a while loop.

This is an interesting response, I was trying to think of a way to use nested if/else or for loops but still have to use that while loop.

Also, this code is only useful assuming the user inputs a 1 digit base 5 number if I'm not mistaken? If that is the case, perhaps I should clarify that the user's input could be any length (and it could also not be a base 5 number at all, in which case I am going to 'cout' or 'cerr' a little "invalid entry" message)

Thank you for taking the time to try and help, especially with such a rapid response.

Last edited on
Topic archived. No new replies allowed.