数组和功能C 的麻烦

TROUBLE WITH Arrays and functions c++

本文关键字:麻烦 功能 数组      更新时间:2023-10-16

基本上在某些温度下的水具有不同的特定权重,我将其设置为2个阵列,具体取决于您使用SI用于公制的单元系统,而我将其用于帝国。IM试图弄清楚如何使函数使用用户输入温度来确定其相应的特定权重,或者如何使用户在不重新开始的情况下重新输入值。现在,如果您输入标准温度,我可以让这件事可以正常工作,但是如果以前提出不可算的值,我需要弄清楚如何让用户重新输入温度。

示例: 如果您通过proccess并选择y和si and t,然后进行cout<<"水温度是多少";cin<<t [1];//也不知道为什么我将其放入Itslef

的数组中

如果您输入说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 SIn";
            cin>> units_system;
            cout<< "nTherefore, you are using the "<<units_system<< " systemn" ;
        }
        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 watern";
            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 << "nnDo you want to calculate the specific weight of water at another temperature(Y or N)?n";
//cin >> calculate_SW;
        cout << "nnWhat is the vertical height to the centroid of the shape you are analyzingn";
        cin >> height;
        cout << "What is the area of the shape you are analyzingn";
        cin >> area;
        FR = (specific_weight * height * area);
        cout << "nThe resultant force acting on the tank you are analyzing is " << FR <<" " << units_FR;
        cout << "nnDo 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;
        }
    }

}

while循环应解决您的问题。请参阅下面:

else if (a == "T") {
    int temperature;
    cout << "nWhat is the temperature of watern";
    cin >> temperature;
    while (temperature <= 88) {
        cout << "Temperature should be greater than 88. Try again: ";
        cin >> temperature;
    }
    T[1] = temperature;
}

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

您可能需要定义一个全局的const变量来代替88-取决于您在用户可以输入的温度上的下限 - 将来使您的代码更易于理解和编辑。像const int MIN_TEMP = 88;在您的代码顶部一样,在这种情况下,您的while条件看起来像这样:

while (temperature <= MIN_TEMP) {

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