これは文字のみを受け入れる必要がありますが、まだ正しくありません。
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
std::string line;
double d;
while (std::getline(std::cin, line))
{
std::stringstream ss(line);
if (ss >> d == false && line != "") //false because can convert to double
{
std::cout << "its characters!" << std::endl;
break;
}
std::cout << "Error!" << std::endl;
}
return 0;
}
出力は次のとおりです。
567
Error!
Error!
678fgh
Error!
567fgh678
Error!
fhg687
its characters!
Press any key to continue . . .
fhg687
文字列内の数字が原因でエラーが出力されるはずです。
受け入れられる出力には、などの文字のみを含める必要がありますghggjh
。