查看: 918|回复: 7
|
[请教]这程序到底出了什么问题?
[复制链接]
|
|
麻烦大家帮我看一下,
这程序没error,
但run出来却不对。
我不知道问题出在哪。。。
- #include<iostream.h>
- void main()
- {
- float kg,cm,bmi;
- cout<<"\nYour weight (in kg) = ";
- cin>>kg;
- cout<<"\nYour height (in cm) = ";
- cin>>cm;
- bmi=(kg/(cm*cm))*10000;
- if (bmi<18.5)
- cout<<"\nYour BMI status = underweight";
- else if ((bmi>18.5)&&(bmi<24.9))
- cout<<"\nYour BMI status = Normal weight";
- else if ((bmi>25)&&(bmi<29.9))
- cout<<"\nYour BMI status = Overweight";
- else if (bmi>30)
- cout<<"Your BMI status = Obesity"<<endl;
-
- }
复制代码 |
|
|
|
|
|
|
|
发表于 12-2-2006 10:18 PM
|
显示全部楼层
你确定没ERROR ? 你用C 或C++ ?
如果是C, 为什么没有 #include<stdio.h>
C++ 的话,好像MAIN 也错了
开头应该是:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
或者你应该 define 你的 float kg=0,cm=0,bmi=0;
不是吗?
有错得话,多多包涵。
[ 本帖最后由 vampcheah 于 12-2-2006 10:20 PM 编辑 ] |
|
|
|
|
|
|
|
发表于 12-2-2006 11:18 PM
|
显示全部楼层
#include<iostream>
using namespace std;
main()
{
float kg,cm,bmi;
cout<<"\nYour weight (in kg) = ";
cin>>kg;
cout<<"\nYour height (in cm) = ";
cin>>cm;
bmi=(kg/(cm*cm))*10000;
if (bmi<18.5)
cout<<"\nYour BMI status = underweight\n";
else if ((bmi>18.5)&&(bmi<24.9))
cout<<"\nYour BMI status = Normal weight\n";
else if ((bmi>25)&&(bmi<29.9))
cout<<"\nYour BMI status = Overweight\n";
else if (bmi>30)
cout<<"Your BMI status = Obesity"<<endl;
return 0;
}
这样应该就没有问题了.... |
|
|
|
|
|
|
|

楼主 |
发表于 13-2-2006 02:41 AM
|
显示全部楼层
原帖由 evo9 于 12-2-2006 11:18 PM 发表
#include<iostream>
using namespace std;
main()
{
float kg,cm,bmi;
cout<<"\nYour weight (in kg) = ";
cin>>kg;
cout<<"\ ...
可不可以不要用using namespace std; ?
因为我没看过这东东。
还有,
怎么会是<iostream>?
后面的.h 呢? |
|
|
|
|
|
|
|

楼主 |
发表于 13-2-2006 02:43 AM
|
显示全部楼层
原帖由 vampcheah 于 12-2-2006 10:18 PM 发表
你确定没ERROR ? 你用C 或C++ ?
如果是C, 为什么没有 #include<stdio.h>
C++ 的话,好像MAIN 也错了
开头应该是:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
retu ...
是C++啊!~
这位仁兄,
很抱歉,
我怎么都看不懂你说的呢。。。 |
|
|
|
|
|
|
|
发表于 13-2-2006 08:58 AM
|
显示全部楼层
原帖由 vampcheah 于 12-2-2006 10:18 PM 发表
你确定没ERROR ? 你用C 或C++ ?
如果是C, 为什么没有 #include<stdio.h>
C++ 的话,好像MAIN 也错了
开头应该是:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
retu ...
楼主用的应该是dos版本的c++ compiler。
iostream, cin,cout是c++,dos版本的规范。
stdio,printf是c,dos版本的规范。
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
以上的coding应该是用windows版本的c++制作出来的console application。是不是c++ .net版本?? |
|
|
|
|
|
|
|
发表于 13-2-2006 02:31 PM
|
显示全部楼层
你的 code 应该没有问题的。
或许,你可以跑到你的 code,
你可以输入 weight 和 height,
但最后却没有 output 显示出来。
如果是这样的话,这是因为你的 if-else 不完美。
如果 bmi 的 value 是 18.500000,24.900001 - 25.000000
或 29.900001 - 30.000000 的话,
你所写的 if-else 是无法辨认的。
应该把你的 if-else 改成:
if (bmi < 18.5)
cout << ......
else if (bmi < 24.9)
cout << ......
else if (bmi < 29.9)
cout << ......
else
cout << ......
这是很多初学者会犯的错误。
你根本不必用在 else if 的挂号里 test 两个 condition,
不止会浪费资源,也把让你的 if-else 无法辨别所有可能的 value 了。 |
|
|
|
|
|
|
|

楼主 |
发表于 13-2-2006 06:05 PM
|
显示全部楼层
原帖由 liawcv 于 13-2-2006 02:31 PM 发表
你的 code 应该没有问题的。
或许,你可以跑到你的 code,
你可以输入 weight 和 height,
但最后却没有 output 显示出来。
如果是这样的话,这是因为你的 if-else 不完美。
如果 bmi 的 value 是 18.5000 ...
哦哦!~
是酱。。。
明白了。。。
也搞定了。。。
多谢各位赐教!~ |
|
|
|
|
|
|
| |
本周最热论坛帖子
|