1   1  /  1  页   跳转

为什么要赋初值?

为什么要赋初值?

#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

这又是为什么呢?
最后编辑2007-02-12 09:42:52
分享到:
gototop
 

忘记问题了
1。为什么要赋初值呢。。?
2。如果就那样写程序,为什么无论输入什么都只出来那个呢?
gototop
 

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.
gototop
 

程序是顺序执行的,那就是说
先执行ma=lang*220;
再执行:
cout<<"Enter the lang:";
cin>>lang;
在进行运算时lang里面的值是不可预知的
gototop
 

大家和我想的一样,呵呵...
gototop
 

初始值是为了结果的可控,没有初始值就直接运用,是十分危险的
gototop
 

不赋就是随机的数字]

肯定要赋了!
gototop
 

ma=lang*220;
cout<<"Enter the lang:";
cin>>lang;
把ma=lang*220;放到后面,就可以了
先赋值再运算
gototop
 
1   1  /  1  页   跳转
页面顶部
Powered by Discuz!NT