Hello all, I have a assignment for school that I am having trouble with. I have to write a program that will list all primes numbers of an inputted number. I have the code and it compiles with no errors, but when I run it after i input the number, the prompt goes away and nothing shows up after. Is there a problem with my code?

``````# include <cmath>
# include <iostream>
using namespace std;

int c = 0;

int main()
{
long double x = 0;
cout<<"\n This program will generate all prime numbers up to the"
<<"\n number you have entered below...\n";
cout<<"\n Please enter a number: ";
cin>> x;

cout<<"\n Here are all the prime numbers up to "<<x<<".\n";

return 0;
}

{
bool prime = true;
int number2;
number2 =(int) floor (sqrt (x));

for (int i = 1; i <= x; i++){
for ( int j = 2; j <= number2; j++){
if ( i!=j && i % j == 0 ){
prime = false;
break;
}
}
if (prime){
cout <<"  "<<i<<" ";
c += 1;
}
prime = true;
}
}``````

I see two things immediately.
1) Prime numbers are integers, not doubles. 24.5 does not have prime values. This will make your code easier to deal with.
2) Your formatting makes the code very difficult to follow. If you want others to read your code, make it easy for them and format it.

Lol the reason why the screen goes away is because right after doing primenum(x).. you need the cin.get(); but instead u have:

``````cout<<"\n Here are all the prime numbers up to "<<x<<".\n";
//Put a cin.get(); here to pause the program instead of the console just disappearing..
return 0;``````

so do i just put cin.get();?

the promt still goes away

Use cin.get() twice..

``````primenum(x);

cin.get();
cin.get();

return 0;``````

There are other ways, but this one's the easiest.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.