C ++试图通过getline函数让用户输入函数调用的值

问题描述 投票:0回答:2

我正在尝试使用类来创建一个程序,该程序可以通过用户输入执行毕达哥拉斯定理的操作,但出现此错误:

错误(活动)E0304没有重载函数“ getline”的实例与参数列表匹配

这是我的代码:

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

class PythagoreanTheorum
{

public:

    double a;
    double b;
    double c;

    void seta(double A)
    {
        a = A;
    }
    void setb(double B)
    {
        b = B;
    }

    void setc(double C)
    {
        c = C;
    }

    double calcAreea()
    {
        return a * pow(a, 2) + b * pow(b, 2) == c * pow(c, 2);
    }    

};


int main()
{
   //Define one right triangles

        PythagoreanTheorum righttriangle1;

    double a;
    double b;
    double c;
    cout << "Enter the value for a: " << endl;
    righttriangle1.a = getline(cin, a);

    cout << "Enter the value for b: " << endl;
    righttriangle1.b = getline(cin, b);


    cout << "Enter the value for c: " << endl;
    righttriangle1.c = getline(cin, c);





}
c++ class cmath
2个回答
0
投票
std::getline读取字符串,而不是双精度。因此,您必须使用std::getline进行读取,然后将其转换为std::string(使用std::string)。

您也可以使用double运算符作为输入:

stod


0
投票
[这就是我编写代码的方式,假定您代码中的calcAreea()旨在显示应用了毕达哥拉斯定理。

我的代码:

stod

我删除了字符串头文件,因为它没有被使用,我也使用了cin而不是getline,因为在这种情况下这样做更好,我也不想使用命名空间std;但由于它在您的代码中,因此我保留了它,并且由于它不是计算区域,因此也将calcAreea重命名为calcTheorem。

编辑:我忘了提到我在私有而不是公共的类中声明了变量

© www.soinside.com 2019 - 2024. All rights reserved.