-2
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;
bool isPrime(int num);

int main()
{
   int num;
   ofstream outputFile;

   outputFile.open("PrimeOut.txt");

// Checks to see if the file opens if (outputFile) { cout << "file opened" << endl; } else { cout << "The file cannot be opened" << endl; } // Input validation of user input number cout << "Enter a number between 1 and 100" << endl; cin >> num;

   while (num < 1 && num > 100)
   {
      cout << "The number must be between 1 and 100" << endl;
   }


   if(isPrime(num) == true)
   {
      cout << num << endl;
   }
    return 0;
}
// Function for finding prime numbers
bool isPrime(int num)
{
   bool valPrime;
   for (int i = 2; i < num; i++)
   {
      valPrime = true;
   }
   return valPrime;
}
Sion Lee
  • 1
  • 1
  • 3
    I'm voting to close this question as off-topic because the OP has provided a basic code structure for an obvious homework exercise, but has applied no effort toward addressing the substance of that homework (in this case, the logic for detecting if an integral value is prime) in the hope of getting the homework done by others. – Peter Nov 08 '19 at 06:31
  • so the point of homework is that im running a program where I can print out prime number within the range 1 to input number. im just currently trying to figure out how to get the code to read prime numbers – Sion Lee Nov 08 '19 at 22:41

1 Answers1

0

I am not sure about your homework.

If you look for code snippets on primes, then you will find here on SO. For example you can look here

bool isPrime( int number )
{
    if ( ( (!(number & 1)) && number != 2 ) || (number < 2) || (number % 3 == 0 && number != 3) )
        return (false);

    for( int k = 1; 36*k*k-12*k < number;++k)
        if ( (number % (6*k+1) == 0) || (number % (6*k-1) == 0) )
            return (false);
    return true;
}

With that (brute force algorithm, you can find out, if a number is prime (for small numbers).

Your program is extremely buggy and basically nonesense. You should by several C++ books and study carefully. Then you need to read your own code and see the problems.

Your post is not suited for SO.

A M
  • 14,694
  • 5
  • 19
  • 44