Chambers
-- -- --

creating a loop inside a loop using while loops

Anonymous in /c/coding_help

921
Hi guys! I need help trying to figure this problem out. I need to write a while loop that will ask the user to input a number. This number will be used as the number of times a loop will iterate. Inside this loop, the code will ask the user to input another number. This number will be used to control the number of times another loop will iterate. For example, if the user first enters 3, the loop will iterate 3 times. Each time the code will ask for a new number. Let's say the user enters 2, then 1, then 4. The code will iterate 2 times, then 1 time, then 4 times. During each iteration, I need to keep track of a counter. The counter should reset to 0 inside each loop. It's driving me nuts! I've spent hours on this so far and I'm just not sure I can do it with while loops. Am I just doing it wrong, or is it just not possible with while loops? Thanks for any help you can give me. <br><br>What I need to do: <br><br>1. outer loop will iterate the number of times the user inputs.<br>2. Inside the outer loop, the user will input how many times the inner loop will iterate<br>3. Inside the inner loop, a counter will increment each time, and reset to 0 in each iteration of the inner loop<br><br>For example, if the user enters the following: <br><br>Enter number of iterations: 5<br>Enter number of inner iterations: 2<br>Counter 0<br>Counter 1<br>Enter number of inner iterations: 1<br>Counter 0<br>Enter number of inner iterations: 4<br>Counter 0<br>Counter 1<br>Counter 2<br>Counter 3<br>Enter number of inner iterations: 1<br>Counter 0<br>Enter number of inner iterations: 1 <br>Counter 0<br>Enter number of inner iterations: <br><br>at this point, the code would go back to the first prompt ("Enter number of iterations: 5")<br><br>Basically, I need to write this in a while loop, but somehow I'm just not getting it.

Comments (16) 26429 👁️