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