c++ - 我如何使用十一位数字
问题描述
我想在我的变量-number 中输入一个十一位数的数字,但我认为没有那么多内存。我尝试使用 *number 和 int *number = new int[100],但它不起作用。
我还想在我的变量名称中添加姓名和姓氏,但每次我使用空间时,它也停止工作。
我该如何解决这些问题?
#include <iostream>
#include <string>
using namespace std;
struct NOTE {
string name;
int number;
int birthday[3];
};
int main()
{
//int *tel = new int[100];
//int *ptr = new int;
NOTE arr[3];
cout << "Please enter quality names and numbers or program stop working!";
for (int i = 0; i < 3; i++) {
cout << "Man #" << i + 1 << "\n";
cout << "Name: ";
cin >> arr[i].name;
cout << "Number: ";
//*tel = arr[i].number;
//cin >> *tel;
cin >> arr[i].number;
cout << "Year: ";
cin >> arr[i].birthday[0];
cout << "Month: ";
cin >> arr[i].birthday[1];
cout << "Day: ";
cin >> arr[i].birthday[2];
}
}
解决方案
您当前正在使用有符号整数来保存您的值。
int number;
带符号的 int 可以保存最大值 2^31 (2,147,483,648),它只有 10 位长。
unsigned int number;
一个无符号整数可以容纳 2^32,即 4,294,967,296(仍然是 10 位),这仍然不够。
您可以使用有符号长整数,其大小为 64 位,最多可容纳 2^63 (9,223,372,036,854,775,808),即 19 位长。这应该足够了。
long number;
推荐阅读
- python - Python 鼻子处理参数和动态生成的测试
- css - 有条件地改变垫形式波纹的颜色
- java - Spark Streaming 转换数据集
到数据集
在java中 - kframework - K 中是否有用于注释的解析器标记?
- markdown - 通过 pandoc 转换 markdown 并解析嵌套的 .md 文件
- scala - 在 Spark scala 中提取到 DataFrame 的 JSON 键的选定值
- r - 如何让 selectInput 中的项目使用通用的 numericInput?
- mule - 使用 MuleSoft 作为 ETL 处理从数据库到 SFDC 的 5M 记录
- html - 更改蓝色按钮中文本的颜色
- wordpress - 在 woocommerce REST API 中添加自定义分类术语