I am trying to make a program for which I need to store different values in two vectors. To read the values I use two equal WHILE loops, but the one from the second vector is skipped. What happens?
The final goal is to see if one vector is a prefix of the other, example: {1,3,4,5,7} and {1,3,4} would come true.
vector<int> vecA, vecB, small, large;
int elem;
int elem2, in = 0;
string prefix = "true";
std::cout << "Enter numbers for the first vector: " << std::endl;
while (std::cin >> elem) // read until end-of-file or cin read invalid data input
vecA.push_back(elem);
std::cout << "Enter numbers for the second vector: " << std::endl;
while (std::cin >> elem2) // read until end-of-file or cin read invalid data input
vecB.push_back(elem2);
if (vecA.size() < vecB.size()) {
small = vecA;
large = vecB;
}
else {
large = vecA;
small = vecB;
}
for (auto &r : small) {
if (r != large[in]) { prefix = "false"; break; }
in++;
}
std::cout << "Prefix is " << prefix << std::endl;