0

我的任务是编写一种 ID 检查器。用户输入他的 ID 号,它应该检查它是否有效并评估其他一些东西(性别、年龄等)。

ID 号由用户作为字符串给出,如下所示:123456/7890

我的任务是删除“/”,然后将字符串转换为 int 并用它进行一些基本计算,例如if(%11==0){...}. 我被困在字符串到 int 的转换上。

这是我的代码:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
    string id;
    int result;
    cout << "Enter an identification number:" << endl;
    cin >> id;

    //Check if the input has "slash" on the 6th position. If so, consider the input valid and remove the slash.
    if (id.at(6) == '/')
    {
        id.erase(id.begin() +6);
    }else cout << "Invalid input." << endl;

    cout << "\n" << id << endl; //cout the ID number without the slash

    stringstream ss(id); //convert the string to int, so we can use it for calculations
    ss >> result;

    cout << "\n" <<result << endl;

    return 0;
}

这似乎只在数字达到某个阈值之前有效。

正如你在这里看到的:
在此处输入图像描述

所以看起来 int 太小了。但是 long int 或 unsigned long int 也是如此......请问该怎么办?

提前致谢。

4

3 回答 3

1

long long用作__int64. _ 也看这个。

于 2013-11-06T15:03:10.793 回答
0

如果您使用 unsigned long long int,则最多可以有 18446744073709551615。

检查您可以在您的平台上使用 .

于 2013-11-06T15:02:40.150 回答
0

使用long long数据类型 asint并且long大小相同

于 2013-11-06T14:58:34.847 回答