C ++做的同时不断重复

时间:2014-08-04 21:46:31

标签: c++ while-loop

#include <iostream>
#include <string>
#include <cctype>
#include <sstream>

using namespace std;

int getTreeNo(int);
int getSpeciesCode(int);
float getDbh(float);
int getTotHt(int);
int getTotHt(int);
double calcTotVol(double[], double[], float, int);

int main() {
    int i = 0;
    int treeNo = 0;
    int speciesCode = 0;
    string speciesDesc[6] = {};
    float dbh = 0.00;
    int totalHt = 0;
    double totalVol = 0.00;
    int Species[6] = {};
    double b0[6] = {};
    double b1[6] = {};
    int noTrees = 0;

    treeNo = getTreeNo(treeNo);
    /* Gets number of trees for calculations*/
    speciesCode = getSpeciesCode(speciesCode);
    /* Get Species code from user*/
    dbh = getDbh(dbh);
    /* get DBH from user*/
    totalHt = getTotHt(totalHt);
    /*Gets tree height from user*/
    totalVol = calcTotVol(b0, b1, dbh, totalHt);
    /* Calculates values */

    /* All constants and variables
        int treeNo;
        int speciesCode;
        string speciesDesc[6];
        float dbh;
        int totalHt;
        double totalVol;
        int Species[6];
        double b0[6];
        double b1[6];
        int noTrees;
        double avgTotVol;
    */
    if (speciesCode = 11) {
        speciesDesc[0] = "Loblolly Pine";
    } else if (speciesCode = 12) {
        speciesDesc[1] = "White Pine";
    } else if (speciesCode = 13) {
        speciesDesc[2] = "Red Pine";
    } else if (speciesCode = 21) {
        speciesDesc[3] = "White Oak";
    } else if (speciesCode = 22) {
        speciesDesc[4] = "Red Oak";
    } else if (speciesCode = 23) {
        speciesDesc[5] = "Other Oak";
    }

    Species[0] = 11;
    Species[1] = 12;
    Species[2] = 13;
    Species[3] = 21;
    Species[4] = 22;
    Species[5] = 23;

    if (speciesCode = 11) {
        b0[0] = 1.2446;
    } else if (speciesCode = 12) {
        b0[1] = 0.000;
    } else if (speciesCode = 13) {
        b0[2] = 2.0822;
    } else if (speciesCode = 21) {
        b0[3] = .7316;
    } else if (speciesCode = 22) {
        b0[4] = 1.6378;
    } else if (speciesCode = 23) {
        b0[5] = .7554;
    }
    if (speciesCode = 11) {
        b1[0] = .002165;
    } else if (speciesCode = 12) {
        b1[1] = .002364;
    } else if (speciesCode = 13) {
        b1[2] = .002046;
    } else if (speciesCode = 21) {
        b1[3] = .001951;
    } else if (speciesCode = 22) {
        b1[4] = .002032;
    } else if (speciesCode = 23) {
        b1[5] = .002174;
    }
    totalVol = b0[6] + b1[6] * pow(dbh, 2) * totalHt;

    for (i = 0; i < treeNo; i++) {
        cout << "Tree Number " << i + 1 << ": " << speciesCode << dbh << totalHt
             << speciesDesc[6] << b0[6] << b1[6] << totalVol;
    }
}

/************************************************************************************************
 * *************/
int getTreeNo(int treeNo) {
    do {
        cout << "Please enter the number of trees :" << endl;
        cin >> treeNo;
        if ((treeNo <= 0) || (treeNo > 999)) {
            cout << "ERROR!!!,You cannot have more then999 entries" << endl;
        }
    } while ((treeNo <= 0) || (treeNo > 999));
    return treeNo;
}

int getSpeciesCode(int speciesCode) {

    do {
        cout << "Please enter your Species Code";
        cin >> speciesCode;

    } while ((speciesCode != 11) || (speciesCode != 12) ||
             (speciesCode != 13) || (speciesCode != 21) ||
             (speciesCode != 22) || (speciesCode != 23));

    cout << "ERROR!!!,That information does not exist wthin our system" << endl;

    return speciesCode;
}

float getDbh(float dbh) {
    do {
        cout << "Please enter the DBH of the tree, The DBH must be greter then "
                "and equal to  five and less then 50.6" << endl;
        cin >> dbh;
        if ((dbh < 5) || (dbh > 50.6)) {
            cout << "ERROR!!!, The DBH must be greter then and equal to five "
                    "and less then 50.6" << endl;
        }
    } while ((dbh < 5) || (dbh > 50.6));
    return dbh;
}

int getTotHt(int totalHt) {
    do {
        cout << "Please enter the height of the tree" << endl;
        cin >> totalHt;
        if ((totalHt < 24) || (totalHt > 160)) {
            cout << "ERROR!!!, Please enter a height thats not larger then "
                    "160, but greater then 24" << endl;
        }

    } while (totalHt < 24 || totalHt > 160);
    { return totalHt; }
}
double calcTotVol(double array[], double array1[], float dbh, int totalHt) {
    double totalVol;

    totalVol = array[6] + array1[6] * pow(dbh, 2) * totalHt;

    return totalVol;
}

任何人都可以帮助我我的speciesCode函数保持循环并且不接受我的输入实际上我不相信任何函数接受它我已经工作了几个小时试图找出原因并且卡住了

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

此while语句中的条件永远不会为false,并且您的循环永远不会退出。为了使其为假,speciesCode一次必须有超过1个值。

while((speciesCode != 11) || (speciesCode != 12) || (speciesCode != 13) || ...)

您可能希望继续重复,直到获得有效代码。在这种情况下,请使用&&

while((speciesCode != 11) && (speciesCode != 12) && (speciesCode != 13) && ...)