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
#include <iostream>
#include <cmath>
usingnamespace 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
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;
}
}
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');