Your biggest issues were:
- The letter you entered just before pressing enter is still in the stream. So your std::cin.get() check won't identify the Enter key, but will instead identify the letter.
- You pushed back to your vector before checking if you should have. Those letter 'g's are going into your vector.
This code is more complicated due to what appears a desire to enter the data all at once on one line and process it after the fact. So I simply read the line in using std::getline() and process that total input in the function break_up(). At this point I have a vector of strings containing all the separate pieces that were entered, valid or not.
Finally, the chunks are checked for validity. As soon as a chunk that cannot be fully converted to an integer is found, myv stops receiving data. Then it's printed. I replaced your while loop with a shorter for loop.
If each piece of data was entered individually and checked as you went, then this program becomes far easier. The toughest bit of breaking up the line goes away, and you can verify as you go and stop exactly when bad data is encountered, as opposed to entering a ton of data just to find that 80% of it didn't get added.
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string> break_up(std::string line) {
std::vector<std::string> chunks;
// Emtpy line check
if (line.length()) {
std::string::size_type loc = 0;
std::string::size_type subStart = 0;
while (loc != std::string::npos) {
loc = line.find_first_of(" \t\r\n", loc + 1);
loc != std::string::npos
? chunks.push_back(line.substr(subStart, loc - subStart))
: chunks.push_back(line.substr(subStart)); // catches last item
subStart = loc + 1;
}
}
return chunks;
}
int main() {
std::vector<int> myv;
std::string values; // Changed type
std::getline(std::cin, values);
std::vector<std::string> chunks = break_up(values);
for (auto i : chunks) {
// std::size_t pos;
try {
int num = std::stoi(i); // , &pos);
// if (pos == i.length()) {
myv.push_back(num);
// } else {
// break; // Remove this else block to add all valid numbers that
// } // were entered
} catch (...) {
} // std::stoi throws two exceptions, don't need to
// distinguish or take any action. Just don't want the program ending
}
// Replaced unnecessarily complicated while loop
for (unsigned int i = 0; i < myv.size(); ++i) {
std::cout << myv[i];
if (i < myv.size() - 1) {
std::cout << ", ";
}
}
std::cout << '\n';
}
EDIT: I made some slight changes to the code to clear up the misunderstanding of what the desired output is. I left the older code commented out.
!cin.fail(), but the same problem persists. Any ideas? - Arîston Giltedgedstd::cinto me. I'm just trying to get clarification on your actual requirements. - sweenish