keep loops asking

Oct 17, 2013 at 3:47am
how do you make a loop ask for another number after it runs through and does its math with the first number you give it? The code runs perfectly but I want it so I can put a new number in it for it to run through again without having to exit the program.

1
2
3
4
5
6
7
8
9
10
11
12
   int num;
   int answer = 1;

   cout << "Enter a Number: " << endl;
   cin >> num;

  for (int multi = 1; num >= multi; multi++){
      
      answer = answer * multi;
      cout << answer << endl;
   }
Oct 17, 2013 at 4:04am
Put the code (lines 2-12 inclusive) inside of a do while loop and make it exit when num is something like 0 or -1 (you will also want to change the prompt to reflect the number to enter to exit).
Oct 17, 2013 at 4:10am
I also have this loop going too

1
2
3
4
5
6
7
8
9
10
11
12
13
while (num < 0){
      
      cout << "Enter a Number: " << endl;
      cin >> num;
      
      if(num == -1){
         exit(1);
      }
      else{
         cout << "Invalide Number" << endl <<
         "The number must be a whole number" << endl;
      }
   }
Oct 17, 2013 at 4:32am
I put it all in the for loop instead of a do while loop. I'm not sure how to do a dowhile loop. Is it the same as a do loop? I do have a new problem. If I type in 0 I get this: (lldb) No clue what it means. If 0 is typed in the answer should be one, not sure what it's doing.
Oct 17, 2013 at 5:04am
I change the loop to a do loop but I still have one problem. When I enter in 0 the code just exits. I'm not sure why.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

int num;             //Variables
   int multi = 0;
   int answer = 1;
   
   cout << "Enter -1 to exit" << endl;
   cout << "Enter a Number: ";
   cin >> num;
   
   do{
      multi++;
      answer = answer * multi;
      cout << answer << endl;
      
      //To start over the loop and enter in a new number
      
      if (num == multi){
         
         cout << "Enter a Number: ";
         cin >> num;
         multi = 0;
         answer = 1;
      }

      //For negative numbers and how to exit (-1)
      
      while (num < 0){
         
         if(num == -1){
            exit(1);
         }
         else{
            cout << "Invalide Number" << endl <<
            "The number must be a whole number" << endl;
            cout << "Enter a Number: ";
            cin >> num;
         }
      }
      
   }while (num > multi);
}
Oct 17, 2013 at 5:37am
toast9 wrote:

I change the loop to a do loop but I still have one problem. When I enter in 0 the code just exits. I'm not sure why.


You have initialized multi as 0. And in your do-while loop, you check whether num is greater than multi. So, if you input num as 0, the while test condition evalutates as false, hence exiting your loop.
Oct 17, 2013 at 5:53am
i changed it to while num is greater than equal to and it still exits.
Oct 17, 2013 at 8:06am
Show the exact and full code you are using right now.

EDIT:

Anyways, your while test condition is still incorrect because,

if num = 0, and multi = 0,

while (num > multi) and while (num < multi) both evaluate as false ;)
Last edited on Oct 17, 2013 at 8:12am
Topic archived. No new replies allowed.