【问题标题】:Displaying enumeration values from functions显示函数的枚举值
【发布时间】:2020-06-06 02:00:50
【问题描述】:

这个想法是根据定义的结构和枚举以表格格式显示从用户收集的所有数据。我收到了一些关于使用 switch 语句的警告,但最令人讨厌的事情似乎是在尝试显示值时使用“s.width”。当我尝试编译时,它建议它在 s.width 中的 s 之前需要一个分号,并且它会继续将分号移动到下一个值。我从枚举中引用值的方式有什么不正确的地方吗?

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

//TODO 1: ShapeKind Enumeration goes here
enum class ShapeKind{
      CIRCLE,SQUARE,RECTANGLE
};

//TODO 2: Shape Structure goes here
struct Shape{
     ShapeKind kind;
     double length, width;
};

// Function prototypes and definitions
double area(Shape s);
//TODO 3: area() definition goes here
double area(Shape s){
  double a;
    switch(s.kind){
      case ShapeKind::CIRCLE:
        return a= (s.length * s.width * 3.14) /4 ;
      case ShapeKind::SQUARE:
        return a = s.length *s.length;
      case ShapeKind::RECTANGLE:
        return a = s.length * s.width;

    }

}

double perimeter(Shape s);
//TODO 4: perimeter() definition goes here

double perimeter(Shape s){
  double p;
  switch(s.kind){
      case ShapeKind::CIRCLE:
        return p= s.length * 3.14;

      case ShapeKind::SQUARE:
        return p = s.length *4;

      case ShapeKind::RECTANGLE:
        return p = 2 *(s.length + s.width);

    }


}

string nameOf(Shape s);
//TODO 5: nameOf() definition goes here
string nameof(Shape s){
  switch(s.kind){
      case ShapeKind::CIRCLE:
        return "Circle";
      case ShapeKind::SQUARE:
        return "Square";
      case ShapeKind::RECTANGLE:
        return "Rectangle";

    }
}

void promptAndReadInputFor(Shape& shape);
//TODO 6: promptAndReadInputFor() definition goes here
void promptAndReadInputFor(Shape& s){
    switch(s.kind){

      case ShapeKind::CIRCLE:
        cout <<"Enter the diameter or a circle: "<< endl;
        cin >> s.length;
        s.width =s.length;

      case ShapeKind::SQUARE:
        cout << "Enter the length of one side: "<< endl;
        cin >>s.length;
        s.width=s.length;

      case ShapeKind::RECTANGLE:
        cout << "Enter the length and width: "<< endl;
        cin >>s.length>>s.width;

          if (s.length == s.width)
            {
              s.kind = ShapeKind::SQUARE; 
            }


    }


};

// The main function
int main() {
  // Shape objects
  Shape circle = { ShapeKind::CIRCLE, 0, 0 };
  //TODO 7: define two more shape objects: a square and and a rectangle
  Shape square= {ShapeKind::SQUARE, 0, 0};
  Shape rectangle ={ShapeKind::RECTANGLE, 0, 0}; 

  //TODO 8: Call the promptAndReadInputFor() function on each of the above three shapes
    promptAndReadInputFor(circle);
    promptAndReadInputFor(square);
    promptAndReadInputFor(rectangle);

  //TODO 9: Print a out a report of these shapes in a table-like format

cout << setw(4) << "Shape" << setw(4) << "Width" << setw(4) << "Height" << setw(4) << "Perimeter"<< setw(4) << "Area"<<endl;    
cout << setw(4) << nameOf(circle) << setw(4) s.width  << setw(4) s.length << setw(4) perimeter(circle)<< setw(4) << area(circle)<< endl;

  return 0;
}

【问题讨论】:

    标签: c++ function struct enums


    【解决方案1】:

    您缺少&lt;&lt;

    cout << setw(4) << nameOf(circle) << setw(4)  s.width  << setw(4) s.length
        //                                      ^HERE there should probably be '<<' 
        << setw(4) perimeter(circle)<< setw(4) << area(circle)<< endl;
    

    【讨论】:

    • 谢谢,我什至没有注意到在所有深夜之间。它现在告诉我我的标识符 s 是未定义的,所以我会尝试 circle.width ,看看它是否适合我。感谢您指出这个错误。我有点筋疲力尽了。
    【解决方案2】:

    在您的代码中,nameOf 的函数声明和 nameof 的定义不同;

    应该是一样的

        string nameOf(Shape s); 
    
        string nameof(Shape s) {} // not the same in your code
    

    还需要进行一项更改,

    您没有在 main 中声明任何本地 s 变量,您被声明为 circle 您的 Shape 对象。

    【讨论】:

    • 谢谢。在之前的评论开始为我指明方向之后,我实际上只是做了这些更正。我还更正了我的 switch 语句,在每个函数中都有一个 return 0 语句,我的代码似乎没有错误,但是现在当我尝试运行它时,我被告知即使它已经编译,也不存在这样的文件或目录所以我去看看我能不能解决这个问题。
    【解决方案3】:

    修复了所有的错误。想感谢您的帮助,并为可能需要类似帮助的任何人传递一些信息。我的 promptAndReadInputFor 函数缺少每个案例的 return 语句,导致函数永远不会结束。我收到的两个答案都是正确的,因为对“s.width,s.etc”的引用在主要功能的范围内不适用,因此将它们更改为“圆形、方形等”,这是创建要使用的对象解决了这个问题。我感谢你们提供的帮助。终于解决了这个问题,感觉很棒。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-11
      • 1970-01-01
      • 2016-01-18
      • 2021-10-03
      • 2020-04-08
      • 1970-01-01
      相关资源
      最近更新 更多