2016-12-01 65 views
-2

基本上在一定溫度下的水具有不同的比重,我根據你使用的SI單位系統和英制單位系統設置爲2個數組。我試圖找出如何做一個函數來使用水的用戶輸入溫度來確定其相應的比重或如何讓用戶重新輸入一個值而不重新開始。現在如果你輸入一個標準溫度,這個東西就可以工作,但是我需要弄清楚如何讓用戶重新輸入一個溫度,如果他們以前有過一個非標準值的話。故障與數組和功能C++

例如: 如果您經歷過程並選擇Y和SI和T並得到結果「什麼是水的溫度」? 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; 
     } 
    } 



} 
+2

請修正所示代碼的縮進。隨機縮進使得所顯示的代碼幾乎不可讀。 –

+0

'cin >> a;' - 給你的變量1個字母的名字使得代碼更難閱讀,更難理解,難以調試,並且更容易犯錯誤。使用更多描述性名稱。 – PaulMcKenzie

+0

對於不在數組中的值,可以*插入*。最簡單的方法是假設你的數據點是通過直線段連接的;這被稱爲線性插值。它看起來像你有*並行數組*。使用每個項目都包含一組相關值的項目數組通常更簡單,更簡單。您可以使用'struct'來定義合適的項目類型。 –

回答

0

A 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; 
} 

while循環將要麼通過下降或從未執行一次輸入的溫度是大於88在這種情況下。這將允許您無限期地繼續輸入不正確的溫度,直到找到正確的溫度而不退出程序並重新開始。

你可能想要定義一個全局的const變量來取代88 - 取決於你的下限是用戶可以輸入的溫度 - 以使你的代碼更容易理解和編輯未來。像const int MIN_TEMP = 88;在你的代碼的頂部,在這種情況下,您while情況是這樣的:

while (temperature <= MIN_TEMP) { 

你甚至可以在上面else if塊返回int的一切,但最後一條語句定義一個函數getTemp()對應於有效的用戶輸入溫度。