dimanche 24 juillet 2016

Why is if statement not working

I'm new to programming and can't work this out and have looked everywhere i can think for an answer. The if statement in function1 "if (m != 0 || 1)" doesn't seem to be read when "cin >> 1" is passed from function2. Here is my code, any help would be appreciated.

#include <iostream>

void function1(int i);

int main() {
    using namespace std;

    int i;

    function1(i);

return 0;
}
----------------------------------------------------------------------------
#include <iostream>

void function2();

void function1(int i) {
    using namespace std;
    if (i != 0 || 1 ) /* not working when the variable 'i' is passed from function2 */ {     
    cout << endl << "i != 0 || 1" << endl;
    function2();
    }
    else if (i == 0 || 1) {
        if (i == 0) {
            cout << endl << "m == 0" << endl;
        }
        else if (i == 1) {
            cout << endl << "m == 1" << endl;
        }
    }
}
----------------------------------------------------------------------------
#include <iostream>

void function1(int i);

void function2() {
    using namespace std;

    int i;

    cout << endl << "type 0 or 1" << endl;
    cin >> i;    /* type 1 or 0 in here */
    function1(i);
}

Aucun commentaire:

Enregistrer un commentaire