2016-04-17 10 views
1

次のコードでは、図形を選択し、その図形の寸法を入力し、そのボリュームを表示するように求められます。ローカル変数がグローバルでないために実行時エラーが発生する

私は、コードを実行すると、私は結果が非数(NaN)ではない私を示し、次のような出力が得られます。enter image description here

私はこれが私の変数はグローバルローカルではないという事実とは何かを持っている必要があります実現、

choice = ReadInputShapeChoice(); 
readshapedimension(choice); 
result = CalculateBasicVolume(choice); 

必要なデータが渡されていないことを確認してください。これを回避し、正しい場所に変数を入れようとしましたが、私はどのような形式のコーディング言語を使用しても初心者であり、プログラム全体がかなり難しいと感じました。

私は同様の質問を掲載しましたが、プログラムが正常に動作するために誰かが自分の変数の場所を変更する方法について誰かがわかれば、私は非常に感謝します。ありがとうございました。

完全なコードは次のとおりです。

#include <iostream> 
using namespace std; 

double height, width, length, radius, base_area, result; 

//Function prototypes 

int ReadInputShapeChoice(); 
void readshapedimension(int choice); 
float CalculateBasicVolume(int choice); 
void PrintResult(int choice); 

double rectangular_solid(double length1, double width1, double height1); 
double cylinder(double radius2, double height2); 
double cone(double radius3, double height3); 
double sphere(double radius4); 
double square_based_pyramid(double height5, double base_area5); 

//function definitions 
double rectangular_solid(double length1, double width1, double height1) 
{ 
    double value; 
    value = (length1 * width1 * height1); 
    return value; 
} 
double cylinder(double radius2, double height2) 
{ 
    double value; 
    value = (3.14159 * (radius2 * radius2) * height2); 
    return value; 
} 
double cone(double radius3, double height3) 
{ 
    double value; 
    value = ((3.14159 * (radius3 * radius3) * height3)/3); 
    return value; 
} 
double sphere(double radius4) 
{ 
    double value; 
    value = ((3.14159 * (radius4 * radius4 * radius4))*(4/3)); 
    return value; 
} 
double square_based_pyramid(double height5, double base_area5) 
    { 
    double value; 
    value = ((height5 * base_area5) * (1/3)); 
    return value; 
    } 


int ReadInputShapeChoice() 
{ int choice; 
    cout << "Choose what shape you want to calculate" << endl; 
    cout << "1 = Rectangular solid" << endl; 
    cout << "2 = Cylinder" << endl; 
    cout << "3 = Cone" << endl; 
    cout << "4 = Sphere" << endl; 
    cout << "5 = Square based pyramid" << endl; 
    cin >> choice; 
    return choice; 
} 

void readshapedimension(int choice) 
{ 
    switch (choice) 
    { 
    case 1: 
    { 
     int length, width, height; 
     cout << "You have chosen rectuangular solid" << endl; 
     cout << "Enter the values for length width and height" << endl; 
     cin >> length >> width >> height; 
     break; 
    } 
    case 2: 
    { 
     int radius, height; 
     cout << "You have chosen cylinder" << endl; 
     cout << "Enter the values for radius and height" << endl; 
     cin >> radius >> height; 
     break; 
    } 
    case 3: 
    { 
     int radius, height; 
     cout << "You have chosen cone" << endl; 
     cout << "Enter the values for radius and height" << endl; 
     cin >> radius >> height; 
     break; 
    } 
    case 4: 
    { 
     int radius; 
     cout << "You have chosen sphere" << endl; 
     cout << "Enter the radius" << endl; 
     cin >> radius; 
     break; 
    } 
    case 5: 
    { 
     int height, base_area; 
     cout << "You have chosen square based pyramid" << endl; 
     cout << "Enter height and area of the base" << endl; 
     cin >> height >> base_area; 
     break; 
    } 
    } 
} 

float CalculateBasicVolume(int choice) 
{ 
switch (choice) 
{ 
    int result; 
case 1: 
{ 
    result = rectangular_solid(length, width, height); 
    break; 
} 
case 2: 
{ 
    result = cylinder(radius, height); 
    break; 
} 
case 3: 
{ 
    result = cone(radius, height); 
    break; 
} 
case 4: 
{ 
    result = sphere(radius); 
    break; 
} 
case 5: 
{ 
    result = square_based_pyramid(height, base_area); 
    break; 
} 
    return result; 
    } 
} 
void PrintResult(int choice) 
{ 
    switch (choice) 
    { 
case 1: 
{ 
    cout << "The volume of the rectangular solid is " << result << endl; 
    break; 
} 
case 2: 
{ 
    cout << "the volume of the cylinder is " << result << endl; 
    break; 
} 
case 3: 
{ 
    cout << "The volume of the cone is " << result << endl; 
    break; 
} 
case 4: 
{ 
    cout << "The volume of the sphere is " << result << endl; 
    break; 
} 
case 5: 
{ 
    cout << "the volume of the square based pyramid is " << result <<  endl; 
    break; 
} 

} 
} 



int main() { 
    int choice; 
    choice = ReadInputShapeChoice(); 
    readshapedimension(choice); 
    result = CalculateBasicVolume(choice); 
    PrintResult(choice); 

     return 0; 
}} 
+0

ケース内では、ブロックがスコープ外になったときにcinを使用して読み込んだ変数が存在しないことをブロックします。 – drescherjm

+0

***これは、私の変数がグローバルではないという事実と関係があることに気がついています。***問題は、あなたが定義した狭い範囲です。これらをスイッチの中で宣言しないでください。 switch文の外で宣言をすべて取得します。また、必要に応じてグローバル変数を削除し、変数を渡します。 – drescherjm

+0

初期化していないグローバルを使用するため、ランタイムエラーが発生します。それらは、case文の中で同じ名前の変数との関係や接続がありません。 – drescherjm

答えて

1

関数内で再宣言しています。

// This is our global declaration 
double height, width, length, radius, base_area, result; 

void readshapedimension(int choice) 
{ 
    switch (choice) 
    { 
    case 1: 
    { 
     // Take out our local declarations 
     // Otherwise cin below will write to the local and these values 
     // will subsequently be lost when the function exits 
     //int length, width, height; 
     cout << "You have chosen rectuangular solid" << endl; 
     cout << "Enter the values for length width and height" << endl; 
     cin >> length >> width >> height; 
     break; 
    } 
    case 2: 
    { 
     //int radius, height; 
     cout << "You have chosen cylinder" << endl; 
     cout << "Enter the values for radius and height" << endl; 
     cin >> radius >> height; 
     break; 
    } 
    case 3: 
    { 
     //int radius, height; 
     cout << "You have chosen cone" << endl; 
     cout << "Enter the values for radius and height" << endl; 
     cin >> radius >> height; 
     break; 
    } 
    case 4: 
    { 
     //int radius; 
     cout << "You have chosen sphere" << endl; 
     cout << "Enter the radius" << endl; 
     cin >> radius; 
     break; 
    } 
    case 5: 
    { 
     //int height, base_area; 
     cout << "You have chosen square based pyramid" << endl; 
     cout << "Enter height and area of the base" << endl; 
     cin >> height >> base_area; 
     break; 
    } 
    } 
} 

は私が以内にあなたがint使用していること、しかし、注意してください:私はあなたのreshapedimensions関数内で再宣言をコメントアウトしてきた例としては、グローバルではなく、自分のローカル値を使用する機能を引き起こしグローバル宣言のdouble;これが意図的だった場合は、変換をどこかに含めて結果をグローバルに戻す必要があります。

ローカル名と混同しないように、グローバル名は一意にしておくことをお勧めします。一般的な技術は、ALLCAPSまたはglob_myvarのようなプレフィックスを使用することです。

+0

はい、これは確かに助けになり、私のプログラムは正常に動作しています!非常にありがとう、またポインタとクラスについて私に知らせる時間を取ってくれてありがとう。私の次のワークショップでは説明されていますが、記述言語が英語ではなくコーディング言語のみを知っているロシア人によって書かれた標準化された大学の説明を助けるでしょう。大変お手伝いしているおかげさまでもう一度おねがいします:) –

+1

問題はありません。私はクラスを使ってクラスを使う方法について長らく説明し始めました.C++が大好きなものですが、私の答えはテキストブックになります。私は[Dynamic Memory Allocation](http://www.cplusplus.com/doc/tutorial/dynamic/)と[Inheritance](http://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm)を調べることをお勧めします。彼らが本当にこのようなコードをより多くの再利用可能で有用なものにするのを助けてくれるからです。 – SteJ

0

あなたは結果変数を再定義しています。

ファイルの先頭に宣言します。

double result; 

ただし、初期化することはなく、後で再定義して、印刷する値をこの新しい変数に置きます。

float CalculateBasicVolume(int choice) 
{ 
    switch (choice) 
    { 
     int result; 

あなたが後で印刷する変数は、実際にコーンのボリュームを含むintではなく、初期化されていないdoubleです。

+0

ありがとうございました、私は先に行って2番目の初期化を削除しました。そのため、「二重の結果」は一番上に宣言されています。しかし、私はまだ同じランタイムエラーが発生しています。なぜこれが知っていますか? –

関連する問題