int input;
cout << "Please enter how many burgers you'd like" << endl;
cin >> input;
什么是缩短“输入”和只接受前两个
位
数字的最简单的方法。 继续该示例:
用户输入:432534. 值输入的:43.
用户输入:12342342341234123412341235450596459863045896045. 值输入的:12.
(编辑:说 '数字'代替“位”)
int input;
cout << "Please enter how many burgers you'd like" << endl;
cin >> input;
什么是缩短“输入”和只接受前两个
位
数字的最简单的方法。 继续该示例:
用户输入:432534. 值输入的:43.
用户输入:12342342341234123412341235450596459863045896045. 值输入的:12.
(编辑:说 '数字'代替“位”)
阅读前两个数字,并形成一个整数:
int digit1 = std::cin.get() - '0';
int digit2 = std::cin.get() - '0';
int input = digit1 * 10 + digit2;
然后丢弃输入的休息:如果用户输入任何
int input = 1;
int digit1 = std::cin.get();
if (digit1 == '-') {
input = -1;
digit1 = std::cin.get();
}
digit1 -= '0';
int digit2 = std::cin.get() - '0';
input *= (digit1 * 10) + digit2;
如下评论说,这不起作用:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
要处理一个负号,你可以做这样的事情除了两个数字作为前两个字符。这很容易通过阅读和使用std::isdigit
进行检查来检查。这取决于你继续或抛出某种错误。
如果用户只输入一个数字,这也不起作用。如果您还需要这样做,则可以读取整个字符串并使用其大小或检查EOF。
对输入操作本身也没有错误检查,但应该有实际的代码。
以input
作为串并转换一日2个字符作为int
#include <sstream>
#include <string>
#include<iostream>
int main()
{
std::string input ;
std::cin>>input;
std::stringstream iss(input.substr(0,2));
int num;
iss >> num;
std::cout<<num;
}
istringstream iss(input.substr(0,2))''有什么问题? – IInspectable
@IInspectable是啊它没有出现在我的脑海,因为第一次想到,谢谢,更新 – P0W
我觉得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
我推荐使用'std :: strtol' pre-C++ 11。它具有与'std :: stoi'相同的错误检查功能。 – chris
int i;
cin >> i;
while (i >= 100 || i <= -100) {
i = i/10; // remove and ignore the last digit
}
这不是真正的大量工作,由于整数溢出。我只是把它作为一个非常简单的算法。
让我重写它为你回答这个问题:
如何从标准输入读取一个字符串,前两个字符写到标准输出?
这是如何回答这个问题的? –
@David它将一个非平凡的数字问题转化为一个字符串操作问题,因此任何人都可以解决这个问题:'string input; cin >>输入; istringstream iss(input.substr(0,2)); int result(0); iss >>结果;' – IInspectable
而且...... *这是应该进入答案的。 –
阅读的字符串,并提取前2个字符:
std::string input;
cout << "Please enter how many burgers you'd like" << endl;
cin >> input;
int first2;
if (input.size()>=1)
{
if (input[0] == '-')
std::cerr << "Negative num of burgers";
else
first2 = atoi(input.substr(0,2).c_str());
}
else
std::cout << "Null number";
我觉得这个方法很容易理解。
int input;
cout << "Please enter how many burgers you'd like" << endl;
cin >> input;
char cTemp[50];
itoa(input, cTemp, 10);
char cResult[3] = {cTemp[0], cTemp[1], '\0'};
int output = atoi(cResult);
除了我的口味太C,不可读,它有一个很好的缓冲区溢出。 -1 – IInspectable
@IInspectable,除非sizeof(int)很大,否则至少不会有缓冲区溢出。 – chris
@chris如果你不想'atoi'访问'cResult [2]'(第三元素),你将不得不阻止这种情况的发生。就目前而言,这不是代码的作用。这是未定义的行为。 – IInspectable
我很惊讶没有人提到fscanf
。虽然C++纯粹主义者可能会反对,但在这种情况下,这需要比cin
少得多的代码(并且实际上更好的错误检查)。
int res = 0;
std::cout << "Please enter how many burgers you'd like" << std::endl;
if (fscanf(stdin, "%02d", &res) != 1) {
std::cerr << "Invalid Input" << std::endl;
}
int c;
do {
c = fgetc(stdin);
} while (c != '\n' && c != EOF);
std::cout << "Got " << res << std::endl;
请注意,这需要'cin'与'stdin'同步,而stdin是默认的默认值 –
那些不是位,它们是数字。 – chris
你想要发生什么与剩余的数字?他们应该被扔掉吗?如果用户输入“-123”怎么办?应该是'-1'还是'-12'? – BoBTFish
我刚编辑过这个问题。对不起,@chris,您的评论现在看起来毫不相关! –