首页 > 解决方案 > 0.2的增量不遵守循环条件?

问题描述

我们有关于循环的作业,它显示两个计算并通过输入的 i 增加一个变量。这些代码运行并且一切都很好,直到我输入a=1, b=3 i=0.2,发生的是3即使 while 条件是它也不会达到a<=b。唯一有效的时候a=1, b=2, and i=0.2

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

double inputI(double i){
    for(i=0;i<=0;cin>>i){
        cout<<"i must be greater than 0"<<endl;
        cout<<"Input i: ";
    }
    return i;
}

double compX(double s, double b){
    double x;
    x = s/cbrt(b)+2*pow(s,2);
    return x;
}

double compY(double s, double x){
    double y;
    y = (x+s/x)+3*s;
    return y;
}

void display(double x, double y,double a){
    cout<<fixed<<setprecision(2)<<a<<"\t";
    cout<<fixed<<setprecision(4);
    cout<<x<<"         "<<y<<endl;
}

int main(){
    double x,y,a,b,i;

    cout<<"Input a: ";
    cin>>a;
    cout<<"Input b: ";
    cin>>b;
    i = inputI(i);
    //is there something wrong???
    do{
        x = compX(a,b);
        y = compY(a,x);
        display(x,y,a);
        a+=i;
    }while(a<=b);

}

标签: c++dev-c++

解决方案


您更正以下警告:

警告 C4700:使用了未初始化的局部变量“i”

例如:

double x,y,a,b,i = 0;

在此处输入图像描述


推荐阅读