#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <sstream> using namespace std;
void printer(int i) {
cout << setw(4) << i << ", "; }
int main() {
string s;
getline(cin, s);
stringstream input(s); //LINE I
vector<int> v1;
int i;
do {
input >> hex >> i;
v1.push_back(i); //LINE II
} while (!input.fail());
for_each(v1.begin(), v1.end(), printer);
return 0; }
Similarly this program outputs t, r, e, e,
for file content t r e
. I believe the reason is very similar to above question.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <fstream>
using namespace std;
void printer(char c) {
cout << setw(2) << c << ", ";
}
int main ()
{
ifstream inputfile("input.txt");
vector<char> v1;
char c;
do
{
inputfile>>c;//LINE I
v1.push_back(c);
}
while (inputfile.good());//LINE II
inputfile.close();
for_each(v1.begin(), v1.end(), printer);
return 0;
}
These are questions from an assessment. I need to understand the why. Of course, knowing the correction would improve my skills too. But I need to explain why it does not work that way.