【解決方法】別の関数で定義された変数を関数に認識させるにはどうすればよいですか?


ユーザーが指定した変数に応じて、円の面積、長さ、半径、または直径を見つけるプログラムを作成しています。 また、ユーザーが指定した小数点以下の桁数に数値を丸めます (必要な場合)。
問題: 34 行目で、これを「小数点以下または最も近い X」という出力にする必要があります。X は、ユーザーが最初の関数で選択した mm/cm/dm/m/km です。

これが私の元のコードです:

#include <iostream>
using namespace std;



void whatmeasure () {             /* a function to determine what units of measurment are being used*/
      string measure;
    cout << "mm = millimeters \n"   //answer options
    << "cm = centimeters\n"
    << "dm = decimeters\n"
    << "m = meters\n"
    << "km = kilometers\n"
    << "Your value is in: " ;     //the question 
    cin>> measure;                
    
}

void whatfind () {                            /*a function determining what the user wants to find*/
    char find;
    cout << endl << "r = radius of the circle\n" // answer options
    << "d = diameter of the circle\n"
    << "a = area of the circle\n"
    << "l = kreisumfang\n"
    << "You need to find: ";              // the question 
    cin >> find;                          

}

void howround () {                                /* the beginning of the function that detrmines if the answer needs rounding, and if yes, how.*/
     string answerneedround;
     cout << "Do you want to round the answer? (yes/no)\n";
     cin >> answerneedround;
     if (answerneedround == "yes") {
         cout << "To decimal places or nearest " <<measure; //THE PlACE OF DOUBT
     }
     
}

int main() {
    
    whatmeasure ();
    whatfind();
    howround ();
   

    return 0;
}

私が試したこと:

34行目を次のように書いてみました:

if (answerneedround == "yes") {
        cout << "To decimal places or nearest " << measure;
    }

しかし、エラーが表示され、「メジャー」が定義されていないと表示されます。
私が試したことが受け入れられないのはなぜですか? 7行目で「メジャー」を定義しました! 私は何をすべきか?
前もって感謝します)

解決策 1

最善の方法は、関数が必要とする値をパラメーターとして渡し、他の関数が入力されたときにそれらを返すようにすることです。

それで whatmeasure 文字列を返します(または、より良いのは、 enum 値であるため、常に有効な測定値です)。
whatfind char(または別の列挙値)を返します
howround からの戻り値を取ります whatmeasure 列挙型も返します。

次に、それらを呼び出すときに、値をローカル変数に格納し、必要に応じて関数に渡します。

これにより、アプリが柔軟になり、メソッドが再利用可能になります。これにより、コードが読みやすくなり、変更しやすくなり、信頼性が高まります。

解決策 2

変数 measure で定義され、存在するだけです。 whatmeasure 関数。 そして同じ findwhatfind. そのため、値を返すように関数を変更し、変更する必要があります main 関連する値を関数に渡します。 次のようなもの:

C++
#include <iostream>
using namespace std;



string whatmeasure () {             /* a function to determine what units of measurment are being used*/
      string measure;
    cout << "mm = millimeters \n"   //answer options
    << "cm = centimeters\n"
    << "dm = decimeters\n"
    << "m = meters\n"
    << "km = kilometers\n"
    << "Your value is in: " ;     //the question 
    cin>> measure;                
    
    return measure; // *** pass the value back to the caller
}

string whatfind () {                            /*a function determining what the user wants to find*/
    char find;
    cout << endl << "r = radius of the circle\n" // answer options
    << "d = diameter of the circle\n"
    << "a = area of the circle\n"
    << "l = kreisumfang\n"
    << "You need to find: ";              // the question 
    cin >> find;                          
    
    return find; // *** return the unit type
}

string howround (string measure) {                                /* the beginning of the function that detrmines if the answer needs rounding, and if yes, how.*/
     string answerneedround;
     cout << "Do you want to round the answer? (yes/no)\n";
     cin >> answerneedround;
     if (answerneedround == "yes") {
         cout << "To decimal places or nearest " <<measure; //THE PlACE OF DOUBT
     }
// you need to get the unit of rounding here

    return rounding; // *** return rounding value
     
}

int main() {
    
    string measurement = whatmeasure ();
    string tofind = whatfind();
    string roundingunit = howround (measurement);
   
// here you need to add the code to do the calculations

    return 0;
}

コメント

タイトルとURLをコピーしました