-2

As the title states, Im writing a program to find the occurances of a digit in a number. both are entered by the user.

this is what i got so far:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int userNum;
    int digCount;
    int digSum;

    cout << "Please enter your number:" << endl;
    cin >> userNum;

    cout << "Enter the digit you want to count:" << endl;
    cin >> digCount;

    if (digCount <= 0 || digCount > 9)
    {
        cout << "You entered an invalid counter!" << endl;
    }

    cout << "Number of occurances for your digit is:" << digSum << endl;
}
Jean-Emmanuel
  • 688
  • 8
  • 18
Eddy
  • 11
  • 1
    Welcome to Stack Overflow! Please elaborate further on your problem. Presently, it is unclear what specific issues you are encountering, and thus we cannot help you. You might wish to read up on [ask] good questions, to get a better idea of what kinds of questions we expect here on Stack Overflow. Additionally, you may find the page on creating an [mcve] helpful. – jaggedSpire Dec 09 '16 at 17:30
  • Your code [doesn't compile](http://coliru.stacked-crooked.com/a/667ffb1c28abb01c). As mentioned provide a [MCVE] please. – πάντα ῥεῖ Dec 09 '16 at 17:31
  • Read about the _modulo_ operation to check for certain digits in a number. – πάντα ῥεῖ Dec 09 '16 at 17:33
  • You could also read a string and interate it. How would you use the modulo operator to find the number of occurances in say 101028 counting the zeros @πάνταῥεῖ ? – Seth Dec 09 '16 at 17:38
  • 2
    @Seth if you take `num % base` the result will be the least significant digit of its representation in the given base. – jaggedSpire Dec 09 '16 at 17:41
  • Im looking for a currcet way to write the program using only loops. – Eddy Dec 09 '16 at 17:50
  • Think about how you can extract each digit out of a number.How can you get `3`, then `2` and finally `1` out of `123`.Once you find this out your problem reduces to counting how many times the extracted digit matches with the input digit. – Gaurav Sehgal Dec 09 '16 at 17:56

2 Answers2

2

You have to (modulo and devide) slice the last digit from userNum and compare it to your digitCount variable. if your digCount variable is equal to the last digit in userNum then you have to increment your digSum variable. if all digits are sliced from userNum the while loop will stop.

int userNum = 0;
int digCount = 0;
int digSum = 0;

cout << "Please enter your number:" << endl;
cin >> userNum;

do
{
    cout << "Enter the digit you want to count:" << endl;
    cin >> digCount;

    if (digCount <= 0 || digCount > 9)
        cout << "You entered an invalid counter!" << endl;
    else
        break;
} while (true);

do
{
    int j = userNum % 10; // get the last digit
    if (j == digCount) // if the last digit is equal to digCount increment digSum
        digSum++;
    userNum = (userNum - userNum % 10) / 10; // slice last digit
} while (userNum != 0); // until all digits are sliced

cout << "Number of occurances for your digit is:" << digSum << endl;

This is what you are searching for.

code1x1.de
  • 304
  • 2
  • 13
1

Transform both userNum and digCount in std::string, and then you can use std::count

#include <algorithm>

digSum = std::count(StringUserNum.begin(), StringUserNum.end(), StringDigCount);
Jean-Emmanuel
  • 688
  • 8
  • 18
  • still dont get it. Im a begginer in c++, I need some help understanting what each line means. I need to write it the like the way I wrote it. – Eddy Dec 09 '16 at 18:00
  • sure, convert `userNum` and `digCount` to `string`, this [link](http://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c) can help you. Name them `StringUserNum` and `StringDigCount` respectively and just copy paste the code above just before the `cout` of your function. – Jean-Emmanuel Dec 09 '16 at 18:05