我被赋予编写某种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 ...请问该怎么办?
提前感谢。
将long long
等同于__int64
使用。Also watch this.
将long long
数据类型用作int
和long
的大小相同
如果使用unsigned long long int,则最多可以有18446744073709551615。
检查您在ULLONG_MAX中可以在平台上获得的最大数量。