例如:
int input;
cout << "Please enter how many burgers you'd like" << endl;
cin >> input;
Run Code Online (Sandbox Code Playgroud)
什么是缩短“输入”和只接受前两个最简单的方法位数字。继续这个例子:
用户输入: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
Run Code Online (Sandbox Code Playgroud)
文档:
http : //en.cppreference.com/w/cpp/string/basic_string/stol
http://en.cppreference.com/w/cpp/string/byte/atoi
| 归档时间: |
|
| 查看次数: |
1324 次 |
| 最近记录: |