If-Else and Loops:

Write your question here.
Hi Guys, i need help with a particular problem the assignment wants me to write a program that prompts for a long integer. Displays counts of 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 if existed and in ascending order.
example:
Enter an integer: 123234345130
digit count
0 1
1 2
2 2
3 4
4 2
5 1
Another question is how do I get the count display to count the numbers?
1
2
3
4
5
6
7
8
9
10
11
12
  	long int lInteger;

	cout << "Enter an integer: ";
	cin >> lInteger;
	cout << "digit\tcount\n";
	while (lInteger <= 0)
	{
		if (lInteger % 0 == 0)
		{
			cout << lInteger << endl;
		}
	}


Thanks Guys
Well, you'll have to look at each digit and see how many times it showed up, right?
I think there are a few ways to approach this. But you should devise a solution that utilizes what you've learned in class.

Can you think of a way to look at each digit in a number?
Could I make all of the numbers to zero
like how I have like this?
[code]
long int lZero= 0;
long int lOne = 0;
long int lTwo = 0;
long int lThree = 0;
long int lFour = 0;
long int lFive = 0;
long int lSix = 0;
long int lSeven = 0;
long int lEight = 0;
long int lNine = 0;
[code/]
So your integers you enter should be in an array, and it doesn't matter if this array is in order or sorted.

You would need another array of the same data type, which has the same size but this one needs to be ordered from 0-9

you then need to think of a way which will do this if (x == y) x++; for every number in your array

hint:
int checkArray[9];
int countArray[size];

for (int i = 0; i < 9; i++)
{
for (int j = 0; j < size; j++)
{
if (..... == .....)
checkArray[i] ++;
I've done this a couple times before. Can you use strings in class?
You could enter a long integer and use std::to_string to convert it to a string. Then you can index through the count how many of each digit show up.

But remember that if you convert it to a string, then the digits are no longer numbers. They are characters now. doing (str[i] == 9) would not work. You would have to do (str[i] == '9') to see if it was a 9.

If you can't use a string, then you'll need to devise another method. You could enter each digit one-by-one, but that kind of seems like it goes against the description of entering a long integer all at once. Let's not make this more complicated than it needs to be.
Would this work?
string digitstr = "";
cout << "Enter an integer: " << endl;
cin >> digitstr;
for(int n = 0; n == 9; n++)
{
if (digitstr[n] == '0') &&
(digitstr[n] == '1') &&
(digitstr[n] == '2') &&
(digitstr[n] == '3') &&
(digitstr[n] == '4') &&
(digitstr[n] == '5') &&
(digitstr[n] == '6') &&
(digitstr[n] == '7') &&
(digitstr[n] == '8') &&
(digitstr[n] == '9')
Well, that will always return false. It can't be equal to every single one of those at once. You have to check each one individually.

Your for loop is a little funny.

1
2
3
4
5
6
7
8
9
string digitstr = "";
cout << "Enter an integer: " << endl;
cin >> digitstr;

for (int i = 0; i < digitstr.length(); i++)
{
    if (digitstr[i] == '0')
    //What to do ?
}
I did some by myself this is what I have so far
string digitstr = "";
cout << "Enter an integer: ";
cin >> digitstr;
int countZero = 0;
int countOne = 0;
int countTwo = 0;
int countThree = 0;
int countFour = 0;
int countFive = 0;
int countSix = 0;
int countSeven = 0;
int countEight = 0;
int countNine = 0;
for(int n = 0; n == 9; n++)
{
if (digitstr[n] == '0')
countZero + 1;
if (digitstr[n] == '1')
countOne + 1;
if (digitstr[n] == '2')
countTwo + 1;
if (digitstr[n] == '3')
countThree + 1;
if (digitstr[n] == '4')
countFour + 1;
if (digitstr[n] == '5')
countFive + 1;
if (digitstr[n] == '6')
countSix + 1;
if (digitstr[n] == '7')
countSeven + 1;
if (digitstr[n] == '8')
countEight + 1;
if (digitstr[n] == '9')
countNine + 1;
}

cout << "digit\tcount\n";
for (int digitstr = 0; digitstr == 9; digitstr++)
{
if (digitstr % 0 == 0)
cout << digitstr << "\t" << countZero << countOne << countTwo << countThree<<
countFour << countFive << countSix << countSeven << countEight << countNine << endl;
}
Instead of countZero + 1;, I would do countZero++ or countZero += 1. If you just do countZero + 1;, it won't actually increase the value because you haven't assigned the increased value to countZero.

if (digitstr % 0 == 0)
Get rid of that.

You can just print them like this:

cout << "0: " << countZero << endl;
Just do that for every one of them.

That should make it work. I didn't pop it into my compiler. If you want to make your program a little faster, you can change all of those 'if' statement AFTER (if digitstr[n] == '0') to else if(...), like this:
1
2
3
4
5
if (digitstr[n] == '0')

else if (digitstr[n] == '1')

else if (digitstr[n] == '9')
its printing, but i keep getting numbers that is not in the input and its not counting the numbers in the input
Hmm. Can you show me your changed code?
if (digitstr[n] == '0')
countZero += 1;
else if (digitstr[n] == '1')
countOne += 1;
else if (digitstr[n] == '2')
countTwo += 1;
else if (digitstr[n] == '3')
countThree += 1;
else if (digitstr[n] == '4')
countFour += 1;
else if (digitstr[n] == '5')
countFive += 1;
else if (digitstr[n] == '6')
countSix += 1;
else if (digitstr[n] == '7')
countSeven += 1;
else if (digitstr[n] == '8')
countEight += 1;
else if (digitstr[n] == '9')
countNine += 1;
cout << "digit\tcount\n";
cout << "0\t" << countZero << endl;
cout << "1\t" << countOne << endl;
cout << "2\t" << countTwo << endl;
cout << "3\t" << countThree << endl;
cout << "4\t" << countFour << endl;
cout << "5\t" << countFive << endl;
cout << "6\t" << countSix << endl;
cout << "7\t" << countSeven << endl;
cout << "8\t" << countEight << endl;
cout << "9\t" << countNine << endl;
wasn't sure if i was to keep the for statement between the digit and the output
That's strange. That seems to work for me.

But you need all those if statements to be in the for loop. The 'cout' statements need to be after the loop. Loop through all of them, count them, and then print once they've all been counted.
Topic archived. No new replies allowed.