截断除前两个数字之外的整数的所有数字
Truncate all digits of an integer except the first two
例如:
int input;
cout << "Please enter how many burgers you'd like" << endl;
cin >> input;
缩短"输入"并仅接受前两位数字的最简单方法是什么。要继续该示例,请执行以下操作:
用户输入: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/stolhttp://en.cppreference.com/w/cpp/string/byte/atoi
读取前两位数字并形成一个整数:
int digit1 = std::cin.get() - '0';
int digit2 = std::cin.get() - '0';
int input = digit1 * 10 + digit2;
然后丢弃其余的输入:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
要处理负号,您可以执行以下操作:
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::isdigit
进行测试来检查。由您决定继续前进或抛出某种错误。
如果用户只输入一个数字,这也不起作用。如果你也需要它工作,你可以读取整个字符串并使用它的大小或检查 EOF。
输入操作本身也没有错误检查,但应该在实际代码中存在。
我很
惊讶没有人提到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;
int i;
cin >> i;
while (i >= 100 || i <= -100) {
i = i / 10; // remove and ignore the last digit
}
由于整数中溢出,这不适用于非常大的数字。 我只是把它作为一个非常简单的算法。
读取一个字符串并提取前 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";
将input
作为字符串,并将第一个两个字符转换为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;
}
我认为这种方法很容易理解。
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], ' '};
int output = atoi(cResult);
让我通过为您重写来回答这个问题:
如何从标准输入中读取字符串并将前 2 个字符写入标准输出?
相关文章:
- 检查输入是否不是整数或数字
- 如何找到大于整数的最小数字
- 在数字之间插入 + 或 - 符号以使其等于整数
- 为什么有时我输入一个整数,程序将第一个输入的数字打印成十进制数?
- 整数区间(或 int 数组)中每个数字的出现次数
- 当我输入字符类型的数字时,为什么我无法获得整数?
- 必须首先打印向量 v1 中最接近整数 x 的数字<int>
- 分隔整数 (C++) 的数字
- 如何优化代码以返回最接近给定整数的数字,但给定列表中不存在?
- C++:从文件中读取字符串和整数,并获得最大数字
- 如何在C ++中替换整数的前两位数字?
- 如何计算整数的十进制数字?
- 如何计算整数链中使用了多少次数字?
- 为什么这个程序接受整数和字母数字输入并打印它们? C++ 中的 std::string 也采用整数值吗?
- R 中的算术在数字上比整数更快。这是怎么回事?
- 我想使用 std::stoi 函数在 cpp 中将字符串转换为整数,因为我想在字符串中找到不同数字的总和(在下级酶中)
- 将大数字(10-12 位数字)存储在无序映射中<字符串,整数>
- 在c++中读取文件时,它如何自动将字符放入字符数组,将数字放入整数变量
- 如何从向量转换为<char>数字整数
- 如何将.txt文件中的未知数字整数保存到数组中