我有一堂課,學生。 它包含一個私人會員'dob' 我想將字符串dob設置爲適當的日期。 我應該驗證輸入,並且我試圖使用scanf來做這件事,但我不知道爲什麼我的輸出關閉。問題存儲日期C++
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 . . .
編輯:
終於得到它的工作。謝謝你的幫助。我選擇了這個答案,因爲它幫助我轉換,但每個人都幫助我理解我的連接問題。
對於2015年9月28日,'mm +'/'' + dd +'/'+ yyyy'是2146.你可能想要在你的數字經過驗證後轉換爲字符串。 – molbdnilo
@molbdnilo我很困惑,對不起。我需要做些什麼才能將日期存儲爲mm/dd/yyyy? – aaaa
與問題無關,但請考慮閱讀https://en.wikipedia.org/wiki/ISO_8601並以國際標準格式(yyyy-mm-dd)存儲/提示日期,而不是混淆的mm/dd/yyyy (甚至是dd/mm/yyyy)格式。 – Andrew