0

Der folgende Code fordert den Benutzer auf, eine Form auszuwählen, die Abmessungen für diese Form einzugeben und ihr Volumen anzuzeigen.Möchten Sie wissen, wie Sie seltsame Ergebnisse zur Laufzeit in VS2015 umgehen können?

Es wird hauptsächlich von Variablen Deklarationen und Funktionsaufrufen verwendet, wie dies erforderlich ist.

Wenn ich den Code ausführen ich folgende Ausgabe:

enter image description here

Ich verstehe nicht, warum - nan (ind) anstelle des Ergebnisses erscheint.

hier ist der vollständige Code:

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

PLease kann mir jemand helfen, einen Weg zu finden, diesen Code zu ändern, so dass es die richtigen Ergebnisse ausgibt? Vielen Dank.

Antwort

0

Sie lesen den Radius und die Höhe in lokale Variablen, die in Ihrer switch-Anweisung deklariert sind. Die globalen Variablen, die in der Berechnung verwendet werden, werden nie festgelegt und ihre Standardwerte von 0 in ihnen haben. Ihre Berechnung ergibt 0,0/0,0, was zu Ihrer NaN (Not a Number) führt.

+0

Vielen Dank, dass Sie darauf hingewiesen haben, wie würde ich sie in globale Variablen umwandeln? Klingt vielleicht albern, aber ich dachte, ich hätte es bereits am Anfang des Codes getan. –

+0

@FlewittConnor Sie haben, aber Sie haben auch 'Int Radius, Höhe' unter 'Fall 3', die einige lokale Variablen mit dem gleichen Namen und einem anderen Typ erstellt. Sie lesen Ihre Eingaben in die Locals, nicht in die Globals. Um dies zu beheben, löschen Sie einfach die Variablendeklarationen in den 'case' Anweisungen in' readshapeddimension'. – 1201ProgramAlarm