[Solved] C++ how to input certain things in vector [closed]


Your vector has zero length at start

vector<int> binV;  //zero length

In this line of code

for (int i = 0; i < binV.size(); i++){//some code}

Will not run because binV is empty

This expression is not correct

if (input == 1 && input == 0)    //not valid

This && is logical and operator, if one of the conditions is false this expression will return false
You need to use logical or || operator

if (input == 1 || input == 0)

If you want to break your for loop if the value is different from 1 or 0

for (int i = 0; i < binV.size(); i++) {
cout << "Enter binary number: " << " ";
if (input == 1 || input == 0) {
    cin >> input;
    binV.push_back(input);
}
else {
    cout << "Wrong input, the size of vector is complete";
    break;    //here
}

the break; command will end the loop

break statement causes the enclosing for, range-for, while or do-while loop or switch statement to terminate.

0

solved C++ how to input certain things in vector [closed]