Heres the code, im pretty sure i missed something really stupid,
No matter what number i type i get "The number oyu type must be a positive number" so i guess the problem is either in my if statements in main or in the checkuserinput function, most likely in my if statements however, i cant find it. I'ts really annoying because it should be quite easy. I would really appreciate any help given.

EDIT: it worked before i added the if statements and the checkuserinput fucntion (which is lacking some functions i know, if you have some good advice please share it :) )

#include <iostream>

using namespace std;
int facultize (int user_input);
int checkuserinput (int user_input);
int main()
{
    int n;
    int inpchk;
    cout<< "Please type a number to find it's factorial; ";
    cin>> n;
    inpchk = checkuserinput (n);
    if (inpchk = 0)
        cout<< facultize (n);

    else if (inpchk = 1)
        cout<< "The number you type must be a positive number";

    else
        cout<< "Error, program terminated";

    return 0;
}
int facultize (int user_input) {
    int result = user_input;
    for (int i=user_input - 1;i>1;i--) {
        result = result * i;
    }
    return (result);
}
int checkuserinput (int user_input) {
    int res;
    if (user_input > 0) {
        res = 0;
    }
    else if (user_input <= 0) {
        res = 1;
    }
return (res);
}

Dani AI

Generated

Short version: the bug was using assignment instead of comparison in the condition — already pointed that out. A single = stores a value and then the condition tests that stored value; that makes the first if evaluate unexpectedly and the flow fall into the wrong branch. Beyond that immediate fix, there are a few practical improvements worth making so the program is robust and clearer.

Use direct input validation (avoid magic numeric return codes) and handle non-integer input from cin. For example, loop until cin yields an integer:

int n;
while (!(std::cin >> n)) {
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cout << "Please enter an integer: ";
}

Compute factorial with a stable type and guard against overflow. Prefer unsigned long long for larger results and check before multiplying:

unsigned long long factorial(unsigned int n) {
    unsigned long long res = 1;
    for (unsigned int i = 2; i <= n; ++i) {
        if (res > std::numeric_limits<unsigned long long>::max() / i)
            throw std::overflow_error("factorial overflow");
        res *= i;
    }
    return res;
}

Quick notes tied back to the thread: treat 0 as valid input (0! == 1), prefer boolean-returning checks (or test n < 0 directly) instead of return codes, and use descriptive names (e.g. factorial) so intent is clear. Enable compiler warnings (g++ -Wall -Wextra) — they typically flag accidental assignments in if conditions and will save time debugging.

Recommended Answers

All 2 Replies

In the if clause u need to use "==" and not = , because its defines a new value (with = ).
So change
if (inpchk = 0)
cout<< facultize (n);
else if (inpchk = 1)
To
if (inpchk == 0)
cout<< facultize (n);
else if (inpchk == 1)

Holy fuck im stupid:) i knew it was something really stupid, well thanks jen140:)

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.