2016-04-16 32 views
0

以下代碼要求用戶選擇一個形狀,輸入所述形狀的尺寸並顯示其體積。想知道如何繞過VS2015運行期間的奇怪結果

它主要用於變量聲明和函數調用,因爲這是必需的。

當我運行的代碼中,我得到以下輸出:

enter image description here

我不明白爲什麼 - 南(IND)出現,而不是結果。

這裏是全碼:

#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

您正在讀取半徑和高度爲本地在您的switch語句中聲明的變量。計算中使用的變量從不設置,並且其默認值爲0。您的計算結果爲0.0/0.0,這會導致您的NaN(非數字)。

+0

謝謝你指出,我將如何去切換到全局變量?可能聽起來很愚蠢,但我認爲我已經在代碼開始時這樣做了。 –

+0

@FlewittConnor你做了,但是你也在'case 3'下面有'int radius,height',它創建了一些具有相同名字和不同類型的局部變量。你正在讀取你的輸入到當地人,而不是全局。要解決這個問題,只需刪除'readshapedimension'中'case'語句中的變量聲明即可。 – 1201ProgramAlarm