首页 > 解决方案 > 我如何使用十一位数字

问题描述

我想在我的变量-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];
    }
}

标签: c++memorynumbers

解决方案


您当前正在使用有符号整数来保存您的值。

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;

推荐阅读