c++ - 如何从C++中的字符串ID中提取int日、月和年

标签 c++ class

我是在大学里开始做这个程序的,但我还有一些功能需要在家里做:getBirthdayage。如何从字符串 ID 中获取日、月、年作为整数?

我尝试只使用字符串,但它不起作用。

#include <iostream>
#include <string>
using namespace std;

enum Sex {male,female,unknown};  //const
class Person   //class
{
private:
    string name;
    string ID;   //this is the problem
    Sex pol;
....
void getBirthday(int &day,int&month,int&year)  //even if i use string for day...it is not working
{}
...
int main()
{Person p1("ALEX","9510167954")...;}   //95-year,10-month,16-day*this is what my teacher wrote

我期望输出:16/10/95

最佳答案

尝试初始化 getBirthday( ) 参数如下:

day = stoi(ID.substr(0,2));
month = stoi(ID.substr(2, 2));
year = stoi(ID.substr(4, 2));

关于c++ - 如何从C++中的字符串ID中提取int日、月和年,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58258687/

相关文章:

C++货币输出

c++ - 尝试通过构造函数初始化 String 类。我得到奇怪的输出

oop - 除了经典的基于类的方法之外,您能想象任何其他实现 OO 的方法吗?

class - 类内常数

C++: "this"指针没用吗?

android - 使用另一个类中的编辑文本中的文本设置 TextView 中的文本

C++ 可能的内存泄漏读取文件

c++ - 为什么libcxx的condition_variable是constexpr和noexcept,但在标准中不是?

带有正文的 Java "abstract like"方法

c++ - 调试断言失败消息