gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:43:28 26 4
gpt4 key购买 nike

我是在大学里开始做这个程序的,但我还有一些功能需要在家里做: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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com