Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么要一直打印圆的宽度?_C++_Methods_Enums_Console_Structure - Fatal编程技术网

C++ 为什么要一直打印圆的宽度?

C++ 为什么要一直打印圆的宽度?,c++,methods,enums,console,structure,C++,Methods,Enums,Console,Structure,你好, 我希望这个代码示例对您来说很容易理解。 我想创建一个程序,其中用户必须通过控制台创建一个几何对象 矩形和三角形目前运行得很好,但圆形有一个小问题 每次我创建一个圆并调用putGeo方法时,宽度也会被打印出来,面积用零计算 有人知道为什么会这样吗 这是我课程中的练习,现在我真的不知道了 程序完全是德语的,但我翻译了输出。对于枚举,DREIECK是三角形,RECHTECK是矩形,KREIS是圆形 谢谢你的帮助 输出中的编辑问题 如果我创建半径为5的圆 我想通过putGeo得到印刷品 半径:5

你好,

我希望这个代码示例对您来说很容易理解。 我想创建一个程序,其中用户必须通过控制台创建一个几何对象

矩形和三角形目前运行得很好,但圆形有一个小问题

每次我创建一个圆并调用putGeo方法时,宽度也会被打印出来,面积用零计算

有人知道为什么会这样吗

这是我课程中的练习,现在我真的不知道了

程序完全是德语的,但我翻译了输出。对于枚举,DREIECK是三角形,RECHTECK是矩形,KREIS是圆形

谢谢你的帮助

输出中的编辑问题

如果我创建半径为5的圆

我想通过putGeo得到印刷品

半径:5 面积:78.5398163397

但实际上我得到了

半径:5 宽度:0 区域:0查看此处:

    #include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>


using namespace std;

const double PI = 3.141592653589793238463;

enum Figur {DREIECK, RECHTECK, KREIS};

struct Geo {
    mutable Figur figur;
    mutable double a, b, c;
};

Geo createGeo(Figur f, double a, double b, double c)
{
    Geo d;
    d.figur = f;
    d.a = a;
    d.b = b;
    d.c = c;
    return d;
}

Geo getGeo() {
    double a=0.0, b=0.0, c=0.0;
n:
    cout << "Please insert which geometrical object you want to create:" << endl;
    cout << "0 for triangle" << endl << "1 for rectangle" << endl << "2 for circle" << endl;
    Figur f;
    int p = 3;
    cin >> p;
    if (p == 0)
    {
        f = DREIECK;

        cout << "Please insert the length: ";
        cin >> a;

        cout << endl << "Please insert the width: ";
        cin >> b;

        {   m:
                cout << endl << "Please set the angle >0 and <180°: ";
                cin >> c;
                if (c <= 0 || c >= 180)
                {
                    cout << endl << "Invalid value! Please try again" << endl;
                    goto m;
                }
        }
        cout << endl << "Your figure is now going to be created" << endl;
        Geo geo = createGeo(f, a, b, c);
        return geo;
    }
    else if (p == 1)
    {
        f = RECHTECK;
        cout << "Please insert the length: ";
        cin >> a;
        cout << endl << "Please insert the width: ";
        cin >> b;
        cout << endl << "Your figure is now going to be created" << endl;
        Geo geo = createGeo(f, a, b ,0);
        return geo;
    }
    else if (p == 2)
    {
        f = KREIS;
        cout << "Please insert the radius: ";
        cin >> a;
        cout << endl << "Your figure is now going to be created" << endl;
        Geo geo = createGeo(f, a,0,0);
        return geo;
    }
    else
    {
        cout << "Invalid value. Please try again." << endl;
        goto n;
    }


}


double flaeche(Geo const &g)
{
    double flaeche;
    if(g.figur = DREIECK) 
    {
        flaeche = 0.5 * g.a*g.b;
    }
    else if (g.figur = RECHTECK)
    {
        flaeche = g.a*g.b;
    }

    else if(g.figur = KREIS)
    {
        flaeche = PI * pow(g.a, 2);
    }

    return flaeche;
}

void putGeo(double a, Geo &g)
{
    string typ;
    if (g.figur == DREIECK) typ = "triangle";
    else if (g.figur == RECHTECK) typ = "rectangle";
    else typ = "circle";
    cout << "Type of figure: " << typ << endl;
    if (g.figur == DREIECK) {
        cout << "Length: " << g.a << endl;
        cout << "Height: " << g.b << endl;
        cout << "Angle: " << g.a << endl;
        cout << "Area: " << a << endl;
    }
    else if (g.figur == RECHTECK) {
        cout << "Length: " << g.a << endl;
        cout << "Width: " << g.b << endl;
        cout << "Area: " << a << endl;
    }
    else if(g.figur == KREIS){
        cout << "Radius: " << g.a << endl;
        cout << "Area: " << a << endl;
    }
}

int main()
{
    Geo g1 = getGeo();

    double a = flaeche(g1);

    putGeo(a, g1);

    return 0;
}
你本想做一个测试,但这是一个任务。这将g.figur的值设置为DREIECK。在此之后,面积的计算将不正确

当您以这种方式使用赋值时,一个好的编译器会警告您。如果您抑制或忽略编译器警告,则会给自己带来麻烦

正确的形式是

if(g.figur = DREIECK)

您得到的确切输出是什么?您希望得到什么?例如,如果我输入圆的半径为5,我希望得到圆的5^2*PI。所以面积应该是78.5398163397。对于putGeo,我只想打印类型、半径和面积,所以:类型:圆半径:5面积:78.5398163397,有什么问题?再次展示你得到的结果与你期望得到的结果,并将其张贴在问题中。好的,改变了-希望现在更清楚谢谢。我一直在监督。问题解决
if(g.figur == DREIECK)