问题描述
- 为何char【17】无法输入字符
-
#include
#include
using namespace std;
class Date{
public:
Date(int y, int m, int d);
~Date(){}
Date(){}
int getyear()const{ return year; }
int getmonth()const{ return month; }
int getday()const{ return day; }
Date(Date &birthday);
void enteringbirthday();
void showbirthday();
private:
int year;
int month;
int day;
};
Date::Date(int y, int m, int d){
year = y;
month = m;
day = d;
}
Date::Date(Date &birthday){
year = birthday.year;
month = birthday.month;
day = birthday.day;
}
void Date::enteringbirthday(){
cout << "年:";
cin >> year;
cout << "月:";
cin >> month;
cout << "日:";
cin >> day;
}
void Date::showbirthday(){
cout << "出身日期:" << year << "年" << month << "月" << day << "日" << endl;
}
class People{
public:
People(){}
People(string n, string s, char id[17]) :birthday(){
number = n;
sex = s;
strcpy_s(ID, id);
}
void entering();
void show();
~People(){}
private:
string number;
string sex;
char ID[17];
Date birthday;
};
void People::entering(){
cout << "录入编号:";
cin >> number;
cout << "录入性别(female,male):";
cin >> sex;
cout << "录入身份证:";
cin >> ID[17];
ID[17] = '';
cout << "录入出生日期:";
birthday.enteringbirthday();
}
void People::show(){
cout << "编号:" << number;
cout << "性别:" << sex;
cout << "身份证:" << ID;
birthday.showbirthday();
}
int main(){
People x;
x.entering();
x.show();
return 0;
}
解决方案
身份证18位加上需要19才够。
解决方案二:
身份证数组应该19位,输入时需要数组名即可,不需要单独加字符串结束标志。
char ID[19];
cin >> ID;
解决方案三:
不是id无法输入数值
时间: 2024-11-25 13:04:27