0

I think my code has an infinite loop. Can someone tell me where I went wrong? The code is supposed to find the number of valid numbers, with a valid number being a number without a digit repeating. For example, 1212 would be a non-valid number because 1 and 2 repeated.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
int a; int b; int count_validNums = 1; int digit; int last_digit; bool is_valid = true;
vector <int> num_list; 
cout << "Enter numbers 0 < a <= b < = 10000: ";
cin >> a >> b;

// Checks for invalid input
if (a < 0 || b < 0 || a > 10000 || b > 10000) {
    cout << "Invalid input";
    return 1;
}

// Checks every number from the range [a,b]
for (int i = a; i <= b; i++){
    last_digit = i % 10;
    num_list.push_back(last_digit); 
    i = i / 10;
    while (i != 0){
        digit = i % 10;
        if (find(num_list.begin(), num_list.end(), digit) != num_list.end()){
            is_valid = false;
            }
        num_list.push_back(digit);
        i = i / 10;
    }
    if (is_valid) count_validNums++;
}

cout << "They are " << count_validNums << " valid numbers between" << a << " and " << b << endl;
}
2
  • 1
    And when you used your debugger to run your program, what did you see? This is what a debugger is for. If you don't know how to use a debugger this is a good opportunity to learn how to use it to run your program one line at a time, monitor all variables and their values as they change, and analyse your program's logical execution flow. Knowing how to use a debugger is a required skill for every C++ developer, no exceptions. With your debugger's help you should be able to quickly find all bugs in this and all future programs you write, without having to ask anyone for help. Commented Jan 26, 2020 at 3:19
  • 1
    Your for loop always ends with i equal to 0 and b always stays the same, thus i will never be greater than b, and your loop never ends. Commented Jan 26, 2020 at 3:30

2 Answers 2

1

The inner while loop terminates when i == 0. Then the outer for loop increments it (so i == 1), then the inner loop reduces it to zero again. Then the other loop increments it, then ...

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

Comments

1

What is happening to cause the infinite loop is that you are constantly reducing the int i back down to 0. Consider these highlights:

`for(int i = a; i <= b; i++){
    //stuff
    while(i != 0){ //<--this forces i down to 0
        //more stuff
        i = i / 10;
    }
    //final stuff
}`

i here is all one variable, so any changes you make to it anywhere will affect it everywhere else it exists! Instead, you can try saying something like int temp = i; and then perform your operations on temp so that i remains independent, but because your for-loop terminates when i <= b and you are constantly resetting i to 0, it will never reach b.

Also, I noticed that in your check for valid numbers you verify that 0 < a,b < 10000, but later in your for-loop you seem to make the assumption that a <= b will be true. Unfortunately, your test does not ensure this, so the for-loop will immediately terminate for inputs where b < a is true (which your program currently allows) and your program will report answers that are likely incorrect. The same is true when I enter letters as input instead of numbers. You might want to revisit that portion of code.

Comments

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.