例如:
int input;
cout << "Please enter how many burgers you'd like" << endl;
cin >> input;
缩短“输入”并只接受前两个 bits 数字的最简单方法是什么。
继续这个例子:
用户输入:432534。 输入值:43。
用户输入:12342342341234123412341235450596459863045896045。 输入值:12。
(编辑:说“数字”而不是“位”)
最佳答案
我认为 std::string
操作可以让你回家。
std::string inputstr;
cout << "Please enter how many burgers you'd like" << endl;
cin >> inputstr;
inputstr = inputstr.substr(0, 2);
int input
input = std::stoi(inputstr); // C++11
input = atoi(inputstr.cstr()); // pre-C++11
文档:
http://en.cppreference.com/w/cpp/string/basic_string/stol
http://en.cppreference.com/w/cpp/string/byte/atoi
关于c++ - 截断除前两位以外的整数的所有数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19385761/