注册 登录
编程论坛 C++教室

求助——关于循环

ohr798546 发布于 2008-07-10 14:03, 973 次点击
这个程序的功能是用三角函数来求树的高度,内有一个英尺/英寸的转换式。
为了避免一闪而过,使用了do-while循环。
结果不起作用……
之前编过的程序都没有出现这种现象……
问题何在啊……

原代码:
//the hight of the tree
//17-06-08 14:30

#include <iostream>
#include <cmath>
#include <cctype>
using std::cout;
using std::cin;
using std::tan;
int main () {
    float d[3]={0};
    float h=0;
    float angle=0;
    float resault=0;
    const float ipf=12.00;
    char agian='n';
    do {
    cout <<"Enter the hight of Protractor.\nInches:";
    cin >>h;
    cout <<"\nEnter the angle on the Protractor.:\n";
    cin >>angle;
    cout <<"\nEnter the long between the tree and the Protractor.\n";
    cout <<"Feet,Inches:";
    cin >>d[0]>>d[1];
    d[2]=d[0]*ipf+d[1];
    resault=h+d[2]*tan(angle);
    cout <<"\nThe hight of the tree is:\n"<<resault<<" inches";
    cout <<"\nRetry(Y\\N)?";
    cin >>agian;
    } while (tolower(agian)=='y');
    return 0;
}

本人初学C++,编的都是一些练习小程序。
高手指教下哈……
6 回复
#2
zhgxy282008-07-10 15:44
出来的结果不符合要求,
问题出在:
resault=h+d[2]*tan(angle)的tan(angle)其算出的值是错的,
应在前定义:
#define PI 3.1415926
该句改为:
resault=h+d[2]*tan((angle/180)*PI)
就可以了。
#3
ohr7985462008-07-10 15:51
意思就是说在tan()中应使用弧度值?
了解&谢谢
#4
水中无月2008-07-11 17:11
用getch()函数能防止程序“一闪而过”
#5
我是杨过2008-07-12 21:25
你是在VC中编的吗,如果不是建议,包含conio.h,然后用getch()
#6
ohr7985462008-07-14 13:00
我用的编译器是Dev-C++
至于循环吗……
总不会只输入一轮就算了……
还能多试几下编译成功的那种感觉
嘎嘎……
1