TROUBLE WITH Arrays和函数c ++

时间:2016-12-01 02:36:13

标签: c++ arrays function

基本上在某些温度下的水具有不同的比重,我将其设置为2个阵列,具体取决于您使用SI作为公制的单位系统和I表示英制的单位系统。我试图弄清楚如何使功能使用水的用户输入温度来确定其相应的特定重量或如何让用户重新输入值而不重新开始。如果您输入标准温度,我现在已经有了这个功能,但我需要弄清楚如果用户之前已经输入了一个非标准值,如何让用户重新输入温度。

实施例:   如果你通过过程并选择Y和SI和T并得到cout<< “水的温度是多少”; cin<< T [1]; //也不知道为什么我把它放到了一个数组中

如果您输入,请说88 特定权重计算为零,因为它不是温度数组中的值,即使该值显然不是假设为零。是否有任何方法允许用户重新尝试而不关闭并重新启动此过程或以某种方式使用函数或甚至使温度四舍五入到最接近的标准值并只使用该特定的重量??? ps我的编程技巧是超级基础的(如果你不能通过我的代码告诉)

任何帮助将不胜感激,谢谢!

// this program will calculate the specific weight of water at any given temperature in either metirc or imperial and the reslutant force of the water in in tank


#include<iostream>
#include<math.h>
#include<string>
#include<iomanip>
#include<fstream>

using namespace std;

// my sad attempt at trying to write a function

float match_temp_to_sw(float T[1]);

/*float match_temp_to_sw{
for(i=o; T[1]!=SI[i]; i++){
c = i+1;
x = SWSI[c];
}

}
*/



int main() {

    string units_system, units_sw, units_p, units_FR, a;
    string fluid;
    char pressure, pressure1;
    int calculate_specific_weight;
    char calculate_SW, calculate_FR;
    float specific_gravity, specific_weight, temperature;
    float pressureA;
    int i,c;
// these are count variables for the for loops to determine the specific                             weight of water at a specific temperature
    float x, area, height, FR;

// these are the values of the temperature and specific weight of water in metric and imperial units

    int SI[21] = {0,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100};

    float SWSI[21]= {9.81,9.81,9.81,9.81,9.79,9.78,9.77,9.75,9.73,9.71,9.69,9.67,9.65,9.62,9.59,9.56,9.53,9.50,9.47,9.44,9.40};

    int I[19] = {32,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,212};

    float SWI[19] = {62.4,62.4,62.4,62.4,62.3,62.1,62.0,61.9,61.7,61.5,61.4,61.2,61.0,60.8,60.6,60.4,60.1,59.8};
    int T[1];

    cout << fixed << showpoint;// setting the precision of the number of sigfigs
    cout << setprecision(2);

    cout << "Do you want to calculate the resultant force of water acting on a tanks surface?(Y or N)\n";

    cin >> calculate_FR;

    while(calculate_FR == 'Y') { // the outer most loop

// initialzing the string variable to zero so that way when you repeat this whole process they dont remember your previous input

        a = "NULL";
        units_system ="NULL";

// initializing the standard set of units that are going to be used   throughout the calculations

        while(units_system !="Imperial"&& units_system!="SI") {

            cout<< "what units system are you using Imperial or SI\n";
            cin>> units_system;
            cout<< "\nTherefore, you are using the "<<units_system<< " system\n" ;
        }

        fluid = "water";


        while (a!="SG" && a!="T") {

            cout << "\nDo you know the specific gravity(SG) or the temperature(T)?\n";
            cin >> a;
        }

        if(a == "SG") {

            cout << "\nWhat is the specific gravity of the water?\n";
            cin >> specific_gravity;
        }


        else if (a == "T") {
            cout << "\nWhat is the temperature of water\n";
            cin >> T[1];
        }


// if you are using the SI system the units for specific wieght will be kN/m^3

        if(units_system == "SI") {
            units_sw = " kN/m^3";
            units_p = " kPa";
            specific_weight = (specific_gravity *9.81);
            units_FR = "kN";

            if(a == "T") {


                if (T[1]== SI[0]) {
                    specific_weight =SWSI[0];
                }
                for(i=0; T[1]!=SI[i]; i++) {
                    c = (i+1);
                    specific_weight= SWSI[c];


                }
                if(T[1]!=SI[21]) {
                    cout << "You did not enter a standard value, and i dont know how to get you to reinput a proper value without restarting this program so please try again";
                    return 0;
                }
            }
        }
// if you are using the imperial system the units for specific weight will be lb/ft^3
        else if(units_system == "Imperial") {
            units_sw = " lb/ft^3";
            units_p = " lb/ft^2";
            specific_weight = (specific_gravity *62.4);
            units_FR = "lb";

            if(a == "T") {

                if(T[1]== I[0]) {
                    specific_weight=SWI[0];
                }

                for(i=0; T[1]!=I[i]; i++) { // a for loop to determine the corresponding specific wieght to the user input temperature

                    specific_weight= SWI[i];

                }
            }
        }

        cout <<"\nTherefore, the specific weight of "<<fluid<<" is ";
        cout << specific_weight<< units_sw;

// i know this isnt in the right spot lol

//cout << "\n\nDo you want to calculate the specific weight of water at another temperature(Y or N)?\n";
//cin >> calculate_SW;

        cout << "\n\nWhat is the vertical height to the centroid of the shape you are analyzing\n";
        cin >> height;

        cout << "What is the area of the shape you are analyzing\n";
        cin >> area;

        FR = (specific_weight * height * area);

        cout << "\nThe resultant force acting on the tank you are analyzing is " << FR <<" " << units_FR;

        cout << "\n\nDo you want to calculate the resultant force of water at another temperature(Y or N)?\n";
        cin >> calculate_SW;

        while (calculate_FR == 'N') {

            cout << "\nYou do not want to complete anymore calculations";
            return 0;
        }
    }



}

1 个答案:

答案 0 :(得分:0)

while循环可以解决您的问题。见下文:

else if (a == "T") {
    int temperature;
    cout << "\nWhat is the temperature of water\n";
    cin >> temperature;

    while (temperature <= 88) {
        cout << "Temperature should be greater than 88. Try again: ";
        cin >> temperature;
    }
    T[1] = temperature;
}

在这种情况下,一旦输入的温度大于88,while循环将会通过或永不执行。这将允许您无限期地继续输入不正确的温度,直到找到正确的温度而不退出程序并重新开始。

您可能希望定义一个全局const变量来取代88 - 取决于您的下限对用户可以输入的温度 - 使您的代码有点将来更容易理解和编辑。代码顶部有const int MIN_TEMP = 88;之类的内容,在这种情况下,while条件如下所示:

while (temperature <= MIN_TEMP) {

你甚至可以定义一个函数getTemp(),除了上面else if块中的最后一个语句,它返回一个对应于有效用户输入温度的int