gpt4 book ai didi

c++ - 问题存储日期 C++

转载 作者:行者123 更新时间:2023-11-30 03:49:01 26 4
gpt4 key购买 nike

我有一个类(class),学生。它包含一个私有(private)成员“dob”我想将字符串 dob 设置为正确的日期。我应该验证输入,并且我尝试为此使用 scanf,但我不确定为什么我的输出关闭。

void Student::getBday(){
int mm, dd, yyyy;
printf("Date of Birth?\n");
scanf("%d/%d/%d", &mm, &dd, &yyyy);
dob = mm + '/' + dd + '/' + yyyy;
}

这是我的打印功能:

void Student::printStudent(){
cout.flush();
cout << endl;
cout << "Student: " << lastName << ", " << firstName << endl;
cout << "Student ID: " << ID << endl;
cout << "Gender: " << gender << endl;
cout << "Status: " << status << endl;
cout << "Date of Birth: " << dob << endl;
cout << "GPA: " << gpa << endl;
cout << endl;
}

我的输出:

LastName?
Bar
FirstName?
Foo
Gender?
M
Status?
Freshman
Date of Birth?
12/21/2012
GPA?
3.5
ID?
12345678
12345678

Student: Bar, Foo
Student ID: 12345678
Gender: M
Status: Freshman
Date of Birth: [
GPA: 3.5

Press any key to continue . . .

编辑:

终于成功了。谢谢大家的帮助。我选择那个答案是因为它帮助我转换,但每个人都帮助我理解了我的串联问题。

最佳答案

这是导致问题的原因是您使用 scanf 的方式。

应该更像

...
printf ("Date of Birth?");
scanf("%d/%d/%d", &mm, &dd, &yyyy);
...

编辑

这是一个 sprinf( ... ) 的例子

...
int mm,dd,yyyy;
char dob [50];
printf ("Date of Birth?");
scanf("%d/%d/%d", &mm, &dd, &yyyy);
sprintf (dob, "%d/%d/%d", mm, dd, yyyy);
...

关于c++ - 问题存储日期 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32817102/

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