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?
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/]
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.
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;
}
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:
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.