未初始化的局部变量tc使用(错误) [英] Uninitialized local variable tc used ( error)

查看:102
本文介绍了未初始化的局部变量tc使用(错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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 have dob return the value the user entered, then pass that to cdob as a parameter (probably replacing cd, and the same for tdob - 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 with tc but its value is random/unknown because you didn't set the value before.


这篇关于未初始化的局部变量tc使用(错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆