#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; }
这是输出:
567Error!Error!678fghError!567fgh678Error!fhg687its characters!Press any key to continue . . .
由于字符串中的数字,fhg687应该输出错误.
接受的输出应仅包含字母,例如ghggjh.
解决方法 更新:显示更全面的解决方案.最简单的方法可能是迭代输入中的每个字符,并检查该字符是否在English-letter ranges in ascii(较低的下限)内:
char c;while (std::getline(std::cin,line)){ // Iterate through the string one letter at a time. for (int i = 0; i < line.length(); i++) { c = line.at(i); // Get a char from string // if it's NOT within these bounds,then it's not a character if (! ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) ) ) { std::cout << "Error!" << std::endl; // you can probably just return here as soon as you // find a non-letter char,but it's up to you to // decIDe how you want to handle it exactly return 1; } } }总结
以上是内存溢出为你收集整理的c – 只接受信件全部内容,希望文章能够帮你解决c – 只接受信件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)