'I'm having issues with my for_each function and my finding prime function right now

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


template <typename t>
void find_prime(const vector<t>& V)
{
    int num;
    bool is_prime = true;
    
    for (int i = 2; i <= num / 2; i++) {
        if (num % i == 0) {
            is_prime = false;
            break;
        }
    }
    
    if (is_prime) {
        cout << "This is the prime number: " << num << endl;
    }
    else {
        cout << "This number is not a prime number: " << num << endl;
    }
}

int main() {
    int input;
    vector<int> num;
    cout << "please enter a number larger than 1: ";
    cin >> input;
    for (int i = 0; i < input; i++) {
        cout << i << endl;
    }

    num.push_back(input);

    for_each(num.begin(), num.end(), find_prime<int>);
}

Right now I'm writing currently doing a project that has a user populate the arry with a singular number, and then display all the numbers from 2 up to the number the user entered. After doing so it should then step through the vector and pass each element of the array through the find_prime function. Though I do not know why my program is deciding it wants to start crashing.



Solution 1:[1]

This is a simpler implementation of your code:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

std::string find_prime(int num) {

    for (int i = 2; i <= num / 2; i++) {
        if (num % i == 0) {
            return "This number is not a prime number: ";
        }
    }
    return "This is a prime number: ";
}

int main() {

    int input;

    cout << "please enter a number larger than 1: ";
    cin >> input;

    for (int i = 2; i <= input; i++) {
        cout << find_prime(i) << i << endl;
    }
}

The above code takes in a number, and then for every number starting from 2 up to the number entered, the computer prints "This is a prime number: " <num> for primes and "This number is not a prime number: " <num> for non-primes.

Here, I have removed the usage of templates because that's completely unnecessary. For the same, I have also removed the vector you created. But in case you need all of that, you can use the below code, but I strongly advise the code provided earlier:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

template <typename t>
std::string find_prime(t num) {

    for (int i = 2; i <= num / 2; i++) {
        if (num % i == 0) {
            return "This number is not a prime number: ";
        }
    }
    return "This is the prime number: ";
}

int main() {

    int input;

    vector<int> nums;

    cout << "please enter a number larger than 1: ";
    cin >> input;


    for (int i = 2; i <= input; i++) {
        nums.push_back(i);
    }

    for (auto& i : nums) {
        cout << find_prime(i) << i << endl;
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Solved Games