Hello. I am trying to figure out why my program has an infinite loop :/ The program asks a user how many digits they want their numbers to be and then the program needs to print out all binary numbers for the user inputted digits.

For example: User inputs 3

Program needs to output:
000
001
010
011
100
101
111

On paper when I work it out my calc functions work, but it does not when I run it. I appreciate the help. Thank you.

{
if(b[index] == 0)
   b[index] = 1;
else
   {
   b[index] = 0;
   index -= 1;
   calc(b,index);
   }
}
int main()
{
   string input;                        // Holds the A-Z values
   vector<int> intVec;  
   int *bin,                            // Array of binary values (0 and 1's)
       index,
	   choice,
       sz = 0,                   		// Size used for in graph size
       i;                        		// Used in loops

   cout << "Please enter in the number of digits" << endl;
   cin >> input;
   choice = atoi(input.c_str());
   bin = new int[choice];
   for(i=0; i < choice; i++)
      intVec.push_back(0);	  
   sz = pow(2,choice);
   sz -= 1;
   index = choice - 1;
   for(i=0;i<intVec.size(); i++)
      cout << intVec[i];
   cout << " " << endl;
   cout << sz << endl;
   
   for(i=0; i < sz; i++)
      {
	  index = 2;
      calc(intVec, index);
	  for(i=0;i<intVec.size(); i++)
         cout << intVec[i]; 
	  cout << " " << endl;

	  }
   intVec.clear();
   exit(0);
   for(i=0; i < choice; i++)
      intVec.push_back(1);
   for(i=0;i<intVec.size(); i++)
      cout << intVec[i]; 
   return 0;
}

I suggest creating a function called OutputBinary(int lengthOfNumbers) and testing it by hardcoding a call to OutputBinary(3). This way you will be able to tell if the problem is with your output of if it is with your user input.

I suggest creating a function called OutputBinary(int lengthOfNumbers) and testing it by hardcoding a call to OutputBinary(3). This way you will be able to tell if the problem is with your output of if it is with your user input.

Thank you for your reply, but I have tested this. I outputted sz after its calculations and its correct.

You should use more descriptive names. What is 'sz'? What does 'calc' do?

You should also post the shortest compilable example you can make that demonstrates the problem.

sz is the number of times the loop should run to get the correct number of numbers to output.

calc is a function that brings takes in an integer vector(size is determined by input) and index. The index is the point where calc should look at and determine if it is a 1 or a 0. If intVec[index] is a 0 it needs to be converted to a 1, and if intVec[index] is a 1 it needs to be a 0.

I found a problem. When I go into my function, the values inside of my b vector change correctly, but they do not also change the values in intVec. Aren't they supposed to do so or am I thinking wrong? Thanks.

It looks like you missed copying/pasting the critical line to answer this question! (The function declaration is missing!)

Well I figured it out and it works now! My biggest problem was on line 39. I am using i in two different places so I switched the i in the for loop on line 35 to a j, changed some stuff and now it works perfectly. Thanks for your time and help.

This question has already been answered. Start a new discussion instead.