#include <iostream>
#include <vector>
#include <list>
usingnamespace std;
int main()
{
vector<int> ivec;
list<int> ilst;
int ival;
cout << "Enter several numbers for vector<int>:" << endl;
while (cin >> ival)
{
ivec.push_back(ival);
}
cin.clear();
cin.sync();
cout << "Enter several numbers for list<int>:" << endl;
while (cin >> ival)
{
ilst.push_back(ival);
}
if (ivec.size() != ilst.size())
{
cout << "ivec != ilst" << endl;
return -1;
}
vector<int>::iterator vit = ivec.begin();
list<int>::iterator lit = ilst.begin();
while (vit != ivec.end())
{
if (*vit != *lit)
{
cout << "ivec != ilst" << endl;
return -1;
}
else
{
++vit;
++lit;
}
}
cout << "ivec == ilst" << endl;
return 0;
}
on mac ox x lion, I use the gcc compiler and the output:
Enter several numbers for vector<int>:
1
2
3
// I entered control + D here
Enter several numbers for list<int>:
ivec != ilst
I have no opportunity to input list<int> container!
but this program runs well on windows using MinGW compiler.
how to solve this problem? thx in advance!
#include <iostream>
#include <sstream>
#include <string>
int main()
{
std::string line;
std::cout << "Please enter numbers for vector on the same line" << std::endl;
std::getline(std::cin, line);
{
std::istringstream is(line);
int value;
while (is >> value)
// append value to the vector
;
}
std::cout << "Please enter numbers for list on the same line" << std::endl;
std::getline(std::cin, line);
{
std::istringstream is(line);
int value;
while (is >> value)
// append value to the list
;
}
return 0;
}
if you can put all the numbers on the same line, you can read the whole line and process that. For example:
#include <iostream>
#include <sstream>
#include <string>
int main()
{
std::string line;
std::cout << "Please enter numbers for vector on the same line" << std::endl;
std::getline(std::cin, line);
{
std::istringstream is(line);
int value;
while (is >> value)
// append value to the vector
;
}
std::cout << "Please enter numbers for list on the same line" << std::endl;
std::getline(std::cin, line);
{
std::istringstream is(line);
int value;
while (is >> value)
// append value to the list
;
}
return 0;
}
I know its an alternative method, but I still want to know what the problem with my program?
If you end the stream with control + D I'm not sure if you can enter more input later. Note that cin.sync(); doesn't do anything with gcc so you could try to use something else like cin.ignore(numeric_limits<streamsize>::max(), '\n');
Actually, even I use
cin.ignore(numeric_limits<streamsize>::max(), '\n');
instead of
cin.sync();
the problem is still the same...
It's driving me crazy
Like Peter said, you've ended the input stream. The system has no reason to expect further input to appear. Imagine you were redirecting a file into the standard input of the program -- there's no way to redirect *two* files.
On the other hand, Mac appears to be alone in this: I just ran your program on Linux (gcc), Solaris (CC), AIX (xlc), and HP-UX (acc), and they all accept further input after cin.clear(). I don't have a Mac system, but give cin.sync_with_stdio(false); a try.