2

I have recently started to learn about C++ and so far I started on data validation anyways I want this program to reject the integer should the user enter said integer containing a 1 or a 0 e.g 1234, 0222 so I thought about it and came up with the code below. However I now face a problem in this particular line

while (temp > 0 )

It would work if i was rejecting any other number say 2,3,4 but since I want to reject 0 this becomes illogical. How should i change the condition so that 0 will reject and ask the user to input again?

Edit: I managed to reject 0 or 1 in the middle of the integer e.g 234152, 234013

However I have no way to reject if the user enters 0234

#include <iostream>


using namespace std;

int main ()
{

int n, temp, temp2 ,counter;
bool check = false;
counter = 0;
do
{
    check = false;                                          

    cout << "Enter a positive integer: ";
    cin >> n;

    temp = n;                                               
    temp2= n;

    while (!check && n > 0 )                                 
    {
        if( n % 10 ==1 || n % 10 == 0)
        {
            check = true;
        }

        else
            n = n/ 10;

    }


}

while(check == true || n < 0);
3
  • If you're trying to filter out certain characters from the input (why you want to is unclear) you should read the user input into a string and check that there are none of the characters that you want to reject. If you explain you're reason for wanting to discard the input it would be easier to guide you. Commented Oct 15, 2017 at 16:43
  • How about this: while(n == 0 || check == 1); Commented Oct 15, 2017 at 16:44
  • Ah right, this is for an class assignment the professor who teaches us wants us to use integer instead of a string as he have not taught us about strings yet. and while(n == 0 || check == 1) gave me an infinite loop. Commented Oct 15, 2017 at 16:46

2 Answers 2

1

I don't think this is the best way of doing what you want to do. That being said, here is a quick way to fix your problem. After the while loop add an if statement.

if(temp==0)

that deals with the zero case separately.

Sign up to request clarification or add additional context in comments.

Comments

1

1) change while (temp %10 == 1 || temp % 10 == 0) to if (temp %10 == 1 || temp % 10 == 0)

2) check have not to be 1 in do while loop, or change its type to bool.

here is code:

int main()
{
    int n, temp;
    bool check = false;
    do
    {
        cout << "Enter a positive integer: ";
        cin >> n;
        temp = n;
        check = false;
        while (temp >= 0) // changed here
        {
            if (temp % 10 == 1 || temp % 10 == 0)
            {
                check = true;
                break;
            }
            temp = temp / 10;
        }
    }
    while (check);
    return 0;
}

3 Comments

Thanks for the help, however entering 0 still breaks out of the loop and does not ask the user for input again
@eatmybananapls to fix it just change while(temp > 0) to while(temp >= 0)
Thank you for the fast reply however once entering certain values e.g 0222 the output now just hangs there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.