> Yours looks lovely, but I'd prefer to use mine just because I need to understand as well haha.
I'll wait until you are ready :)
> Is my loop correct, however?
Your loop wasn't correct, that's why I fixed it for you just now. If you are still in doubt, just update and show us your loop.
#include <iostream>
#include <string>
usingnamespace std;
//module prototype
bool isPrime (int number);
constint ZERO_FOR_CAL=0;
int main()
{
int number;
string keepGoing;
bool trueOrFalse;
cout << "We are going to see if the positive integer you enter is a prime number or not." << endl;
do
{
cout << "Enter a positive number." << endl;
cin >> number;
trueOrFalse=isPrime(number);
if (trueOrFalse==true);
{
cout << "The number is prime." << endl;
}
if (trueOrFalse==false);
{
cout << "The number is not prime." << endl;
}
cout << "Would you like to see if another number is prime? Type yes if so." << endl;
}
while (keepGoing=="yes");
}
bool isPrime(int number)
{
int i=2;
for(i=2; i<number; number++)
{
if(number%i==ZERO_FOR_CAL) returnfalse;
}
returntrue;
}
#include <iostream>
#include <string>
usingnamespace std;
//module prototype
bool isPrime (int number);
constint ZERO_FOR_CAL=0;
int main()
{
int number;
string keepGoing;
bool trueOrFalse;
cout << "We are going to see if the positive integer you enter is a prime number or not." << endl;
do
{
cout << "Enter a positive number." << endl;
cin >> number;
trueOrFalse=isPrime(number);
if (trueOrFalse==true);
{
cout << "The number is prime." << endl;
}
if (trueOrFalse==false);
{
cout << "The number is not prime." << endl;
}
cout << "Would you like to see if another number is prime? Type yes if so." << endl;
}
while (keepGoing=="yes");
}
bool isPrime(int number)
{
int i=2;
for(i=2; i<number; number++)
{
if(number%i==ZERO_FOR_CAL) returnfalse;
}
returntrue;
}
#include <iostream>
#include <string>
usingnamespace std;
// module prototype
bool isPrime (int number);
constint ZERO_FOR_CAL=0;
int main()
{
int number;
string keepGoing;
bool trueOrFalse;
cout << "We are going to see if the positive integer you enter is a prime number or not." << endl;
do
{
cout << "Enter a positive number." << endl;
cin >> number;
trueOrFalse=isPrime(number);
if (trueOrFalse==true) // (1)
{
cout << "The number is prime." << endl;
}
if (trueOrFalse==false) // (2)
{
cout << "The number is not prime." << endl;
}
cout << "Would you like to see if another number is prime? Type yes if so." << endl;
}
while (keepGoing=="yes");
}
bool isPrime(int number)
{
int i=2;
for(i=2; i<number; i++) // (3)
{
if(number%i==ZERO_FOR_CAL) returnfalse;
}
returntrue;
}
Alright, the two isses, 1) I wanted the user to press yes at the end if they wanted to enter another prime number, but when I run it and try to press yes, it exits me out of the program due to the (press any key to exit).
If what I just said confused you, perhaps run it yourself and you'll see what I mean.
I wanted to make it so that if they press yet they repeat the process.
2) It's giving me weird answers for the prime is not prime.
For example when I put 3 or 5 it says it's not prime when they are.
#include <iostream>
#include <string>
#include <stdlib.h>
usingnamespace std;
//module prototype
bool isPrime (int number);
constint ZERO_FOR_CAL=0;
int main()
{
int number;
string keepGoing;
bool trueOrFalse;
cout << "We are going to see if the positive integer you enter is a prime number or not." << endl;
do
{
cout << "Enter a positive number." << endl;
cin >> number;
trueOrFalse=isPrime(number);
if (trueOrFalse==true)
{
cout << "The number is prime." << endl;
}
if (trueOrFalse==false)
{
cout << "The number is not prime." << endl;
}
cout << "Would you like to see if another number is prime? Type yes if so." << endl;
}
while (keepGoing=="yes");
system("Pause");
}
bool isPrime(int number)
{
int i=2;
for(i=2; i<number; number++)
{
if(number%i==ZERO_FOR_CAL) returnfalse;
}
returntrue;
}
You just didn't notice the third logic error. for(i=2; i<number; number++ i++) // (3)
Declare an additional string and tell cin to input it :
1 2 3 4 5 6 7 8 9
do
{
char YesNo[100];
cout << "Would you like to see if another number is prime? Type yes if so." << endl;
cin >> YesNo;
keepGoing=YesNo;
}
while (keepGoing=="yes");
//Ashton Dreiling
//Prime number exercise
#include <iostream>
#include <string>
#include <stdlib.h>
usingnamespace std;
//module prototype
bool isPrime (int number);
//global constant
constint ZERO_FOR_CAL=0;
int main()
{
//some variables
int number;
bool trueOrFalse;
string keepGoing;
cout << "We are going to see if the positive integer you enter is a prime number or not." << endl;
do
{
cout << "Enter a positive number." << endl;
cin >> number;
trueOrFalse=isPrime(number);
//testing trueOrFalse value
if (trueOrFalse==true)
{
cout << "The number is prime." << endl;
}//end if statement
if (trueOrFalse==false)
{
cout << "The number is not prime." << endl;
}//end if statement
cout << "Would you like to see if another number is prime? Type yes if so." << endl;
cin >> keepGoing;
}//ed while loop
while (keepGoing=="yes");
system("Pause");
}//end main
//bool function to put in prime loop
bool isPrime(int number)
{
int i=2;
//for loop for prime calculation
for(i=2; i<number; i++)
{
if(number%i==ZERO_FOR_CAL) returnfalse;
}//end for loop
returntrue;
}//end bool function