lundi 13 juillet 2020

what is different with bool and if condition? [closed]

i wrote two codes of the program for identifying which vector is the prefix of another one. This is first one that:

int main() {
    vector<int> ivec1, ivec2;
    int ival;
    cout << "Enter elements for first vector:(100 to end)" << endl;
    cin >> ival;
    while (ival != 100){
        ivec1.push_back(ival);
        cin >> ival;
    }
    cout<< "Enter elements for second vector:(100 to end)" << endl;
    cin >> ival;
    while (ival != 100) {
        ivec2.push_back(ival);
        cin >> ival;
    }
    vector<int>::size_type size1, size2;
    size1 = ivec1.size();()
    size2 = ivec2.size();
    
    bool result = true;
    for (vector<int>::size_type ix = 0; ix != (size1 > size2 ? size2 : size1); ++ix) {
        if (ivec1[ix] != ivec2[ix]) {
            result = false;
            break;
        }
    }
    if (result) {
        if (size1 < size2)
            cout << "first vector is prefix of second";
        else if (size1 = size2)
            cout << "two vectors is equal";
        else
            cout << "second is prefix of first";

    }
    else
        cout << "no vector is prefix of another";
        
    return 0;

}

this is second one:

int main() {
    vector<int> ivec1, ivec2;
    int ival;
    cout << "Enter elements for first vector:(100 to end)" << endl;
    cin >> ival;
    while (ival != 100){
        ivec1.push_back(ival);
        cin >> ival;
    }
    cout<< "Enter elements for second vector:(100 to end)" << endl;
    cin >> ival;
    while (ival != 100) {
        ivec2.push_back(ival);
        cin >> ival;
    }
    vector<int>::size_type size1, size2;
    size1 = ivec1.size();()
    size2 = ivec2.size();
    
    
    for (vector<int>::size_type ix = 0; ix != (size1 > size2 ? size2 : size1); ++ix) {
        if (ivec1[ix] = ivec2[ix]) {
            if (size1 < size2)
                cout << "first vector is prefix of second";
            else if (size1 = size2)
                cout << "second is prefix of first";
            else
                cout << "two vectors is equal";

        }
        else
            cout << "no vector is prefix of another";
    }
    
    
        
    return 0;

}

the first Code works very well. when I was running the second one, I entered 123 for ivec1 and 9 for ivec2. the result should be "no vector is the prefix of another" But, the result shows "second is the prefix of first". is there anyone who can help me check what's the problem with my second Code?

Aucun commentaire:

Enregistrer un commentaire