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;

      void primenum(long double);     
      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";
      primenum(x);                
     
return 0;
}

           void primenum(long double x)
      {
      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;
    }
}

Dani AI

Generated

Short version: the program will run but the prime test has logic/type bugs and the console-closing is a separate issue. was right that you should use integer types, and / were right that the console needs a pause when you run the EXE by double-click, but the real correctness fixes are below.

Key correctness fixes to apply (why the original prints nothing or the wrong results)

  • Use an integer type for the limit (e.g. long long) and validate input.
  • Start testing at 2 (1 is not prime).
  • For each candidate i test divisors up to sqrt(i) (not sqrt of the overall input). A safe integer test is for (j = 2; j*j <= i; ++j).
  • Initialize the isPrime flag at the start of the outer loop (do not reuse a single flag across different i values).
  • Avoid globals for counting; use a local counter if you need one.

A minimal, corrected example you can drop in and try:

#include <iostream>
#include <limits>

int main() {
    long long n;
    if (!(std::cin >> n) || n < 2) return 0;

    for (long long i = 2; i <= n; ++i) {
        bool isPrime = true;
        for (long long j = 2; j * j <= i; ++j) {
            if (i % j == 0) { isPrime = false; break; }
        }
        if (isPrime) std::cout << i << ' ';
    }
    std::cout << '\n' << "Press Enter to exit...";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cin.get();
    return 0;
}

Performance note and testing

  • For large upper bounds (tens or hundreds of thousands), use a Sieve of Eratosthenes instead of trial division for much better speed and simpler code.
  • Test with small inputs first: input 10 => "2 3 5 7", input 1 or 0 => no primes.
  • To avoid the console closing while debugging, run the program from a terminal/command prompt or use the ignore/get pause shown above rather than relying on double cin.get().

This addresses the logical errors in the loop and gives a simple, robust replacement to try.

Recommended Answers

All 4 Replies

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.

When you repost your formatted code, add comments to explain what you are doing and why. That helps us understand your thought processes.

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";
primenum(x);
    //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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.