c++ - 显示函数的枚举值

标签 c++ function struct enums

想法是根据定义的结构和枚举以表格格式显示从用户那里收集来的所有数据。我已经收到一些有关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;
}

最佳答案

您缺少<<

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;

关于c++ - 显示函数的枚举值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62226465/

相关文章:

javascript - 这个 swipe.js 函数返回了什么?

c++ - std::map 使用比较参数检查第一个和第二个值

c++ - 为什么 const 结构数组在按名称引用 const 结构时不放在 .rodata 中?

c++ - 扩展现有的 Spirit 语法(AST 和船长的问题)

c++ - 如何在 C++ 代码中要求某些概念?

function - 如何在 Mathematica 和 Matlab 中查找已定义符号的类型和内存位置?

Android Kotlin 在其他 Activity 中调用函数

c++ - 我将如何实现循环调度模拟器?

c++ - 如何从任意客户端查找 DHCP 租约信息?

c++ - 在使用 R/C++ 函数构建 Rpackage 时遇到问题