How to ask user if they want to run program again?

I have to have this program loop if the user is asked if they want to run it again, I know i might have to use a while loop but I don't know where to put it :S

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  #include <iostream>
#include <cmath>
using namespace std;

//Creating new methods
void getOutput();
void isPrime(int);

//Main method 
int main()
{   
    //calling class into main
    getOutput();

    return 0;
}//END Main

//Method that prints to the console
void getOutput()
{
    //Declaring variables
    int input;

    //Asking for user input
    cout << "Enter a number to see if it prime: ";
    //Storing the input
    cin >> input;
    //Spacing
    cout << endl;
    //Displaying all other prime numbers
    cout << "Here are all the numbers that are prime up to the number you entered: " << endl;
    isPrime(input);
    //Spacing
    cout << endl;

}//END getOutput

//is Prime method/mathematics
void isPrime(int Size)
{
    //Declare/initiate variables
    int Array[Size];
    int x = 0;
    int endline = 0;

    //BEGIN for loop to make array
    for (int fillArray=0; fillArray<Size; fillArray++)
    {
        Array[fillArray] = fillArray+1;
    }//END for loop

//For loops and if statements to check if number that was entered is prime or not
    //BEGIN for loop
    for (int i=1; i<(Size-1); i++)
    {
        //BEGIN for loop
        for (int j=2; j<=sqrt(i); j++)
        {
            //BEGIN if statement
            if (Array[i]%j == 0)
            {
                x = 1;
                break;

            }//END if

        }//END for loop

        //BEGIN if statement for fill array
        if (Array[i] == 4)
        {
            x =1;
        }//END if

        //BEGIN if for array
        if (x == 0)
        {
            cout << Array[i] << " ";
            endline = endline++;
        }//END if

        //initialize variable
        x = 0;
        //BEGIN if statement
        if (endline != 0)
        {
            //BEGIN nested if 
            if (endline%10 == 0)
            {
                cout << endl;
                endline = 0;
            }//END nested if

        }//END if
    }//END for loop

}//END isPrime 


Thank you!
Seems like you'd want to loop in main around the getOutput function.
Some run again code would be:

1
2
3
4
5
6
7
8
9
10
11
12
void run_again()
{
   string s;
   cout<<"Do you want to run again (yes or no):"<<endl;
   cin >> s;
   if(s == "yes") {
      getOutput();
   }
   else {
      return 0;
   }
}


You'd put the run again function right after the getOutput function in main. It would look like:

1
2
3
4
5
int main()
{
   getOutput();
   run_again();
}
For a loop it would look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
void run_again()
{
   string s;
   bool r = true;
   cout<<"Do you want to run again (yes or no):"<<endl;
   cin >> s;
   if(s == "yes") {
      r = true;
   }
   else {
      r = false;
   }
}


The main loop would look like:

1
2
3
4
5
6
7
8
9
int main()
{
   while(r == true)
   {
      getOutput();
      run_again();
   }
   return 0;
}
Thank you so much guys! This really helped me :) Mush appreciated
Try a do-while loop. It will run through once before checking the condition so it would look something like this:

1
2
3
4
5
6
7
8
9
char userChoice = '\0';

do
{
getOutput();

cout << "Would you like to run this program again? (y/n): ";
cin >> userChoice;
}while(userChoice == 'y' || userChoice == 'Y');
Topic archived. No new replies allowed.