#include <iostream>
#include <cmath>
#include <string>
usingnamespace std;
int main()
{
string name;
cout << "Please enter your username: " << endl;
cin >> name;
do
{
char choice;
cout << "Welcome to the Prime Number Program" << endl;
cout << "-----------------------------------" << endl;
cout << "Please enter the following options: " << endl;
cout << "A) Would you like to print all of the prime numbers from N (desired integer)?" << endl;
cout <<" B) Quit the program" << endl;
cin >> choice;
bool prime;
if (choice == 'a' || choice == 'A')
{
double number;
number = 0;
cout <<"Please enter the desired value to print all of the prime numbers: " << endl;
cin >> number;
for (int i = 3; i <= number; i++)
{
prime = true;
for (int n = 2; n <= i; n++)
{
if (i%n==0)
{
prime = false;
}
}
if(prime)
{
cout << i << " is prime" << endl;
}
}
}
else
{
cout << "You have entered an invalid option" << endl;
}
while (choice != 'a' || choice != 'A')
{
cout <<"Exiting program" << endl;
}
}
return 0;
}
Your prime generator is extremely slow. Here is an semi-optimized prime sieve but still much faster than what you are doing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
constint largestPrime = 10000;
std::vector<bool> sieve(largestPrime+1, true);
sieve.at(0) = false;
sieve.at(1) = false;
constint sqrt = 101;
for(int i = 2; i < sqrt; ++i) //this can be optimized
//if you create the sieve for odds only and remember 2 is a prime
{
if(sieve.at(i)
{
for(int j = i*i; j <= largestPrime; j += i)
{
sieve.at(j) = false;
}
}
}
//now if it is true it is prime.
I have finished my code with a improved prime number finder from a max range.
One last problem, I cannot compile as I am receiving two errors:
error C2181: illegal else without matching if
error c1075: end of file found before the left brace
{
at 'd:\visual studio 2010/projects/project3/project3/primenumb.cpp(16)' was matched
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
int main()
{
string name;
char choice;
int number, i, count, n;
cout << "Please enter your username: " << endl;
cin >> name;
do
{
cout << "Welcome to the Prime Number Program" << endl;
cout << "-----------------------------------" << endl;
cout << "Please enter the following options: " << endl;
cout << "A) Would you like to print all of the prime numbers from N (desired integer)?" << endl;
cout <<" B) Quit the program" << endl;
cin >> choice;
if (choice == 'a' || choice == 'A')
{
cout <<"Find all the primes below which number:" << endl;
cin >> n;
cout << name << " has chosen to select all of the prime numbers below" << number << endl;
for (number=1; number <= n; number++)
{
count =0;
for (i=2; i<= number/2; i++)
{
if (number%i==0)
{
count ++;
break;
}
}
if(count == 0 && number!= 1)
{
cout << number << setw(3);
}
}
else
cout << "You have entered an invalid option" << endl;
while (choice == 'b'|| choice == 'B');
{
cout <<"Exiting program" << endl;
}
system("PAUSE");
return 0;
}
Ah! i cannot believe I missed such a simple error, thank you!
One last question I promise, thank you guys for pointing out all the logical errors!
The program runs as intended, however, when I press B which the program should exit, the else statement "you have entered an invalid option" executes and re loops the menu, but when I press B or b the program should exit
while (choice == 'b'|| choice == 'B'); this is the condition you use inside your do/while loop so it only executes if choice is b or B maybe you meant != b && != 'B' or == 'A' || == 'a'
I see, so I managed to fix the while loop where I enter B and the program executes.
HOWEVER haha another issue has come up.
When I enter a digit over 100, the prime numbers output correctly up until the program has to deal with high numbers (over 100) and the program starts executing random numbers
Also, the do while loop does not execute correctly, after inputting a digit to find the prime number, the program will execute the else statement and terminate.
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
int main()
{
string name;
char choice;
int number, i, count, n;
cout << "Please enter your username: " << endl;
cin >> name;
do
{
cout << "Welcome to the Prime Number Program" << endl;
cout << "-----------------------------------" << endl;
cout << "Please enter the following options: " << endl;
cout << "A) Would you like to print all of the prime numbers from N (desired integer)?" << endl;
cout <<" B) Quit the program" << endl;
cin >> choice;
if (choice == 'a' || choice == 'A')
{
cout <<"Find all the primes below which number:" << endl;
cin >> n;
cout << name << " has chosen to select all of the prime numbers below " << n << endl;
for (number=1; number <= n; number++)
{
count =0;
for (i=2; i<= number/2; i++)
{
if (number%i==0)
{
count ++;
break;
}
}
if(count == 0 && number!= 1)
{
cout << number << setw(3);
}
}
}
else
{
}cout << "You have entered an invalid option" << endl;
}while ((choice !='A')&&(choice !='B')&&(choice !='a')&&(choice !='b'));
{
cout <<"Exiting program" << endl;
}
system("PAUSE");
return 0;
}
while ((choice !='A')&&(choice !='B')&&(choice !='a')&&(choice !='b')); again this isn't right. You either want to loop 1) when it is 'a' or 'A', 2)when it is not 'b' && not 'B', 3) when it is case 1 or case 2. (I would suggest case 1 and output an error message for case 3). As far as your prime finder again I would suggest a sieve you can find all the primes less than 5 billion in the matter of a few minutes(given enough ram and decent computer). As for your error maybe setw(3) has something to do with it. I would honestly just put a space (' ') or a newline ('\n') instead of using std::setw unless you want it to look like a table then pick a largest width.
What about the else statement? It is not executing properly
When I press B to quit the program, the else statement executes along with the proper statement, why?
I want it to execute properly where it only shows the statement "you have entered an invalid option" when the user enters anything other than A or B or a or b
Look at it closely. I didn't notice earlier but the output statement is outisde of the braces. Anyways it shouldn't be an else for that. It should be an else-if something like:
1 2 3 4
elseif(choice != 'b' && choice != 'B') //the first if will have already checked if it is a or A so you don't need to check again
{
//error message
}