我是在大学里开始做这个程序的,但我还有一些功能需要在家里做:getBirthday
和 age
。如何从字符串 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/