Hello, I`m just starting to get into the basics of C++ and I have to create a following program - basically the user has to input an integer and a digit of their choice and the program has to check whether the square of that integer contains that specific digit or not, and if it does then how many of them are in that square. The result has to be outputted on the screen and the number has to be split into digits with the modulus operation.
This is what I have done so far

#include <iostream>
using namespace std;
int main()
{
    int number,digit,square,remainder,ok;
    do
        {
        start: {}
        cout << "Input an integer!" << endl;
        cin >> number;
        if (number <= 0)
            {
            cout << "You entered incorrect data, input an integer!" << endl;
            goto start;
            }
        else
        cout << "Input a digit!" << endl;
        cin >> digit;
        square = number*number;
        do
            {
                remainder = number %10;
                number = number / 10;
            } while (square>1);
        if (remainder == digit)
        cout << "Square of "<< number <<" contains "<< digit << endl;
        else
        cout << "Square of "<< number <<" does not contain "<< digit << endl;
        cout << "Repeat the program (1) or end it (0)?" << endl;
        cin >> ok;
        } while (ok == 1);
    return 0;
}

It also has to be repeatable, which I have kinda figured out, also if the input is wrong, like a negative number, the program has to let you know, is goto the right thing for that?
I don`t know how to count the specific digits in the given number and I think my code is off, too. So any help or directions on what I should be working with will be appreciated. Thanks in advance!

Whoops, lines 22. and 23. are supposed to be

remainder = square %10;
square = square / 10;

Edited 5 Years Ago by DoubleZ: n/a

do
            {
                remainder = square %10;
                square = square / 10;
            } while (square>1);

Input --> number = 13
square = 169
digit = 6

This should pass? Yes? 6 is a digit in 169.

Think about how you would do this on paper.

169
Number of 6's = 0

169 % 10 = 9
Is 9 6? No.
169 / 10 = 16


16 % 10 = 6
Is 6 6? Yes.
Add 1 to the Number of 6's. # of 6's is 1
16 / 10 = 1

1 % 10 = 1
Is 1 6? No
1 / 10 = 0


There are 1 6's.


Now look at your loop. You need to go through it 3 times. Do you? You need to do all of these things every trip through the loop. Do you? Turn your mathematical process into code. Adjust what is inside the loop and outside to match.

Edited 5 Years Ago by VernonDozier: Fixing things up.

Okay, so I kinda figured it out and it looks a bit like this:

do
            {
                remainder = square % 10;
                if (remainder==digit)
                counter++;
                square = square / 10;
            } while (square>0);
        if (counter>0)
        cout << "Your digit " << digit << " appeared " << counter << " times " << endl;
        else
        cout << "Your digit was not found" << endl;
        out << "Repeat the program (1) or end it (0)?" << endl;
        cin >> ok;
        counter = 0;

everything seems ok right? dont really know how that counter++ thingy works but its doing fine.

>> dont really know how that counter++ thingy works but its doing fine.

The counter counts the number of occurrences under certain occurrences (in this case, remainder equals digit), and as with all counting, you add one every time you increment...

counter++;

is the same as

counter = counter + 1;

It all looks good to me secept that you need to move line 14 above line 1. What about the FIRST time you go through that loop? Sometimes variables get initialized to 0 automatically if you don't initialize them to something else, but you can never assume that.

Yeah I know, I added it on the top too, the line 14 one is to reset the counter, because if you repeat the program rather than terminate it and start again it does not reset and gives false values. The whole program looks like this:

#include <iostream>
using namespace std;
int main()
{
    int number,digit,square,remainder,ok,counter;
    counter = 0;
    do
        {
        start: {}
        cout << "Input an integer!" << endl;
        cin >> number;
        if (number <= 0)
            {
            cout << "You entered incorrect data, input an integer!" << endl;
            goto start;
            }
        else
        start2: {}
        cout << "Input a digit!" << endl;
        cin >> digit;
        if (digit < 0)
            {
            cout << "You entered incorrect data, input a digit!" << endl;
            goto start2;
            }
        if (digit > 9)
            {
            cout << "You entered incorrect data, input a digit!" << endl;
            goto start2;
            }
        square = number*number;
        do
            {
                remainder = square % 10;
                if (remainder==digit)
                counter++;
                square = square / 10;
            } while (square>0);
        if (counter>0)
        cout << "Your digit " << digit << " appeared " << counter << " times " << endl;
        else
        cout << "Your digit was not found" << endl;
        cout << "Repeat the program (1) or end it (0)?" << endl;
        cin >> ok;
        counter = 0;
        } while (ok == 1);
    return 0;
}

Thanks for the help :)

It does not reset because your using "Goto"... goto will just skip over everything and jump over code.. all the input is still in the input buffer..

I fixed the goto's below.. u can figure it out easily.. as for the "your number 0" appeared blah blah blah amount of times.. u can fix that.. because I did not enter 0..

always says your number 0 appeared...

#include <iostream>

using namespace std;

int digit,square,remainder,ok;
int counter = 0;
int number = 0;

void Start()
{
     while(number <= 0)
     {
        cout << "Input an integer!" << endl;
        cin >> number;
        if (number <= 0)
        {
            cout << "You entered incorrect data, input an integer!" << endl;
            continue;
        }
        else
            break;
     }
}

void Start2()
{
     while((digit < 0) || (digit > 9))
     {
        cout << "Input a digit!" << endl;
        cin >> digit;
        if (digit < 0)
        {
             cout << "You entered incorrect data, input a digit!" << endl;
             continue;
        }
        if (digit > 9)
        {
             cout << "You entered incorrect data, input a digit!" << endl;
             continue;
        }
        else
            break;
     }
}
    
int main()
{
    do
    {
            if(number  <= 0)
            {
              Start();
            }
            else
                Start2();
            
            square = number*number;
            while(square > 0)
            {
                remainder = square % 10;
                if (remainder==digit)
                {
                   counter++;
                }
                square = square / 10;
            }
            
            if (counter>0)
               cout << "Your digit " << digit << " appeared " << counter << " times " << endl;
            else
                cout << "Your digit was not found" << endl;
                cout << "Repeat the program (1) or end it (0)?" << endl;
                cin >> ok;
                number = 0;
                counter = 0;
        } while (ok == 1);
    return 0;
}

I appreciate the effort, but I cant get this to work, it always takes 0 as the digit, also in my code I used goto just to jump back to the value input (I don`t think it affects the counter or does it?), line 44 is for resetting the counter and you used the same thing for that. So can you please explain what exactly void pointers do and how to get your code to work properly. Thanks!

Edited 5 Years Ago by DoubleZ: n/a

Okay, guys, here`s the final version of this program, I`m marking this as solved, cause I think it pretty much is, thanks again everyone!

#include <iostream>
using namespace std;
int main()
{
    int number,digit,square,remainder,ok,counter;
    do
        {
        counter = 0;    
        do
        {
            cout << "Input an integer!" << endl;
            cin >> number;
            if (number <= 0)
            {
                cout << "You entered incorrect data, input an integer!" << endl;
            }
        } while (number <= 0);
        do
        {
            cout << "Input a digit!" << endl;
            cin >> digit;
            if ((digit < 0) || (digit > 9))
            {
                cout << "You entered incorrect data, input a digit!" << endl;
            }
        } while ((digit < 0) || (digit > 9));
        square = number*number;
        do
            {
                remainder = square % 10;
                if (remainder==digit)
                {
                    counter++;
                }
                square = square / 10;
            } while (square>0);
        if (counter>0)
        {
            cout << "Your digit " << digit << " appeared " << counter << " times " << endl;
        }
        else
        {
            cout << "Your digit was not found" << endl;

        }
        cout << "Repeat the program (1) or end it (0)?" << endl;
        cin >> ok;
        } while (ok == 1);
    return 0;
}
This question has already been answered. Start a new discussion instead.