未初始化的局部变量tc使用(错误) [英] Uninitialized local variable tc used ( error)
问题描述
struct dat
{
int 天;
int 月;
int 年;
int cday;
int cmonth;
int cyear;
};
void dob()
{
dat d;
cout<< 输入出生日期;
cout<< ENDL;
cout<< 输入日期;
cin>> d.day;
cout<< ENDL;
cout<< month;
cin>> d.month;
cout<< ENDL;
cout<< year;
cin>> d.year;
cout<< ENDL;
cout<< d。一天<< - << d.month<< - << d.year;
cout<< ENDL; cout<< ENDL;
}
void cdob()
{
dat cd;
cout<< 输入当前日期;
cout<< ENDL;
cout<< 输入日期;
cin>> cd.day;
cout<< ENDL;
cout<< month;
cin>> cd.month;
cout<< ENDL;
cout<< year;
cin>> cd.year;
cout<< ENDL;
cout<< cd.day<< - << cd.month<< - << cd.year;
cout<< ENDL; cout<< ENDL;
}
void tdob()
{
dat tc;
int tyear;
tyear = tc.cyear - tc.year- 1 ;
int 天;
days = tc.day - tc.cday;
int tmonth;
tmonth = tc.month + tc.cmonth;
cout<< year =<< tyear;
cout<< ENDL;
cout<< 总天数<<天;
cout<< ENDL;
cout<< 总月份<< tmonth;
cout<< ENDL;
}
void main()
{
dob();
cdob();
tdob();
}
我的尝试:
void tdob()
{
dat tc;
int tyear;
tyear = tc.cyear - tc.year- 1 ;
int 天;
days = tc.day - tc.cday;
int tmonth;
tmonth = tc.month + tc.cmonth;
cout<< year =<< tyear;
cout<< ENDL;
cout<< 总天数<<天;
cout<< ENDL;
cout<< 总月份<< tmonth;
cout<< ENDL;
}
错误信息非常明确:
使用未初始化的本地变量tc
快速浏览一下代码即可确认:
dat tc;
int tyear;
tyear = tc.cyear - tc.year-1;
你在第一行宣布
tc
,你在第三行使用它 - 但是你没有给它一个值。
我怀疑你想要做的是dob
返回用户输入的值,然后将其传递给cdob
作为参数(可能替换cd
,tdob
相同 - 但这是你的代码,所以你会知道,不是我!
Quote:未初始化的局部变量tc使用(错误)
这意味着您使用tc
执行某些操作,但其值为random / unknown,因为您未设置该值之前。
struct dat
{
int day;
int month;
int year;
int cday;
int cmonth;
int cyear;
};
void dob()
{
dat d;
cout << " enter date of birth";
cout << endl;
cout << " enter day";
cin >> d.day;
cout << endl;
cout << " month";
cin >>d.month;
cout << endl;
cout << "year";
cin >> d.year;
cout << endl;
cout <<d. day << "-" << d.month << "-" <<d.year;
cout << endl; cout << endl;
}
void cdob()
{
dat cd;
cout << " enter current date ";
cout << endl;
cout << " enter day";
cin >> cd.day;
cout << endl;
cout << " month";
cin >> cd.month;
cout << endl;
cout << "year";
cin >> cd.year;
cout << endl;
cout << cd.day << "-" << cd.month << "-" << cd.year;
cout << endl; cout << endl;
}
void tdob()
{
dat tc;
int tyear;
tyear = tc.cyear - tc.year-1;
int days;
days = tc.day - tc.cday;
int tmonth;
tmonth = tc.month + tc.cmonth;
cout << " year = " << tyear;
cout << endl;
cout << " total days" << days;
cout << endl;
cout << "total month" << tmonth;
cout << endl;
}
void main()
{
dob();
cdob();
tdob();
}
What I have tried:
void tdob()
{
dat tc;
int tyear;
tyear = tc.cyear - tc.year-1;
int days;
days = tc.day - tc.cday;
int tmonth;
tmonth = tc.month + tc.cmonth;
cout << " year = " << tyear;
cout << endl;
cout << " total days" << days;
cout << endl;
cout << "total month" << tmonth;
cout << endl;
}
The error message is very explicit:
Uninitialized local variable tc used
A quick glance at your code confirms this:
dat tc; int tyear; tyear = tc.cyear - tc.year-1;You declare
tc
on the first line, you use it on the third - but at no time do you give it a value.
I suspect that what you want to do is havedob
return the value the user entered, then pass that tocdob
as a parameter (probably replacingcd
, and the same fortdob
- but it's your code, so you would know that, not me!
Quote:Uninitialized local variable tc used ( error)
This means that you do something withtc
but its value is random/unknown because you didn't set the value before.
这篇关于未初始化的局部变量tc使用(错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!