Code:
#include <iostream>
#include <string>
#include <sstream>
using std::string;
using std::stringstream;
using std::cout;
using std::endl;
int string2int(const string& text, bool& ok)
{
stringstream stream;
stream << text;
int number = 0;
stream >> number;
ok = !stream.fail() && stream.eof();
return number;
}
int main(int argc, char** argv)
{
for (int i = 1; i < argc; ++i)
{
bool ok;
cout << "input: '" << argv[i] << "' -> number: "
<< string2int(string(argv[i]), ok);
cout << " (" << (ok ? "ok" : "not ok") << ")" << endl;
}
return 0;
}
Ciao,
_
Lesezeichen