1

So i am really new at programming, i was messing around and decided to try coding a Prime generator. The idea is the user inputs the maximum number the computer should check for a prime, and it outputs a text file with all the primes till there. So i've written this piece of code.

#include <iostream>
#include <string>
#include <Windows.h>
#include <fstream>

using namespace std;
int isPrime(int num) {
    for (int a = 1; a <= num/2; a++) 
        if (num % a == 0)
            return 0;
    return 1;
}


int main() {
    ofstream out_data("primes.txt");
    std::string name;
    int quantity;
    int maximum = 1000000;
    std::cout << "What is your name ?\n";
    getline(std::cin, name);
    std::cout << "What is the biggest number you want to get as a prime " << name << "? Please note that the maximum is " << maximum <<" \n";
    std::cin >> quantity;
    if (quantity <= maximum) {
        for (int b = 1; b < quantity; b++) {
            if (isPrime(b) == 1) {
                std::cout << b << "\n";
                out_data << b << "\n";
            }
        }
        std::cout << "The computer has finished calculating primes. Please check your folder for a .txt file.";
        Sleep(60000);
    }
    if (quantity > maximum) {
        std::cout << "Oh, i'm sorry. The computer can not calculate till " << quantity << ".";
        Sleep(15000);
    }
    return 0;
}

The file and the console shows just the number 1. I spent some time trying to find out what's wrong with the code and got nowhere. In my mind the for loop would repeat itself and the if statement until b

LRD27
  • 98
  • 1
  • 8
  • 1
    Its funny that you set your `maximum` to 1 million. Assuming a 32-bit Integer, your maximum value should be around 2.1 billion, and you should use `INT_MAX`. – abelenky Jun 07 '17 at 19:34
  • Also try declaring the function `isPrime` as a `bool`. Then you'd write `return false; //instead of return 0` and `return true; //instead of return 1`. In your if statement you can then write `if(isPrime(b))`. Note that you could write the if statement like that without the change too, because in C++ every integer value apart from 0 evaluates to true. – Botimoo Jun 07 '17 at 19:34
  • Run this function on every iteration to see if the current iteration's number is a prime or not: [**Determining if a number is prime**](https://stackoverflow.com/a/38646385/5890227) – Khalil Khalaf Jun 07 '17 at 19:37

2 Answers2

2

You should start your loop in isPrime from 2 instead of 1. Every integer equals 0 mod 1.

styrofoam fly
  • 578
  • 2
  • 9
  • 25
1

I believe your issue lies within your isPrime funcion. The first iteration of the for loop when a = 1 will always catch the if statement due to any num % 1 = 0. Start your for loop with a = 2.

   int isPrime(int num) {
        for (int a = 2; a <= num/2; a++) 
            if (num % a == 0)
                return 0;
        return 1;
    }

Since num / 2 = 0, the for loop is never entered when num = 1. Therefore, your isPrime() funtion will return 1 and it will let it output. All other nums will enter the for loop and return 0.

Easton Bornemeier
  • 1,918
  • 8
  • 22
  • OMG ! I can't believe i didn't see it. But why was the output 1 and not all the numbers from 0 to x ? – LRD27 Jun 07 '17 at 19:37
  • Because your function returned 0 and you wrote to output only if it deemed a number a prime. Which it didn't in this form. – Botimoo Jun 07 '17 at 19:39
  • Since num / 2 = 0, the for loop is never entered when num = 1. Therefore, your isPrime() funtion will return 1 and it will let it output. All other nums will enter the for loop and return 0. (Edited original answer to include this) – Easton Bornemeier Jun 07 '17 at 19:40