瑞星卡卡安全论坛

首页 » 技术交流区 » 系统软件 » 为什么要赋初值?
bigmonkey - 2007-1-27 20:57:00
#include<iostream>
using namespace std;
int main()
{
    double lang,ma;
    ma=lang*220;
    cout<<"Enter the lang:";
    cin>>lang;
    cout<<"It equals to "<<ma<<" ma"<<endl;
    return 0;
}
这个小程序
提示出来这个
F:\study\second.cpp(6) : warning C4700: local variable 'lang' used without having been initialized
由于是warning可以运行,运行后无论输入什么,出来的都是

-2.03631e+|064

这又是为什么呢?
bigmonkey - 2007-1-27 20:59:00
忘记问题了
1。为什么要赋初值呢。。?
2。如果就那样写程序,为什么无论输入什么都只出来那个呢?
Coolpanda - 2007-1-27 22:34:00
ma does not have a initial value. for c ++ compiler it doesn't give a value at beginning. you should:

cin>>lang;
ma=lang*220;
cout<<"It equals to "<<ma<<" ma"<<endl;

it is very simple just follow the lines your r coding.
菜青虫虫 - 2007-1-28 10:35:00
程序是顺序执行的,那就是说
先执行ma=lang*220;
再执行:
cout<<"Enter the lang:";
cin>>lang;
在进行运算时lang里面的值是不可预知的
中阕小神 - 2007-1-28 17:36:00
大家和我想的一样,呵呵...
天下奇才 - 2007-1-29 15:39:00
初始值是为了结果的可控,没有初始值就直接运用,是十分危险的
tankk - 2007-2-10 21:02:00
不赋就是随机的数字]

肯定要赋了!
Kaká - 2007-2-12 9:52:00
ma=lang*220;
cout<<"Enter the lang:";
cin>>lang;
把ma=lang*220;放到后面,就可以了
先赋值再运算
1
查看完整版本: 为什么要赋初值?