【问题标题】:C++ trying to have user input values for function calls via getline functionC ++试图通过getline函数为函数调用获取用户输入值
【发布时间】:2020-03-29 16:52:10
【问题描述】:

我正在尝试使用类创建一个程序,通过用户输入,可以执行勾股定理的操作,但我收到此错误:

错误(活动)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


【解决方案1】:

std::getline 读取字符串,而不是双精度数。因此,您必须使用 std::string 阅读,然后将其转换为 double(使用 stod)。

您也可以使用 &gt;&gt; 运算符进行输入:

cout << "Enter the value for a: " << endl;
std::cin >> righttriangle1.a;

cout << "Enter the value for b: " << endl;
std::cin >> righttriangle1.b;

cout << "Enter the value for c: " << endl;
std::cin >> righttriangle1.c;

【讨论】:

    【解决方案2】:

    这就是我编写代码的方式,假设您的代码中的 calcAreea() 旨在显示正在应用的勾股定理。

    我的代码:

    #include <iostream>
    #include <cmath>
    using namespace std;
    
    class PythagoreanTheorum
    {
        public:
            void seta(double A)
            {
                a = A;
            }
            void setb(double B)
            {
                b = B;
            }
           void setc(double C)
            {
                c = C;
            }
            double calcTheorem()
            {
                cout<<"Applying Pythagoreas Theorem:"<<pow(a,2)<<"+"<<pow(b,2)<<"="<<pow(c,2);
            }    
        private:
            double a;
            double b;
            double c;
    
    };
    
    
    int main()
    {
        //Define one right triangles
        //Test -> a = 3, b = 4, c = 5
    
        PythagoreanTheorum righttriangle1;
    
        double a;
        double b;
        double c;
        cout << "Enter the value for a: " << endl;
        cin>>a;
        righttriangle1.seta(a);
    
        cout << "Enter the value for b: " << endl;
        cin>>b;
        righttriangle1.setb(b);
    
        cout << "Enter the value for c: " << endl;
        cin>>c;
        righttriangle1.setc(c);
    
        righttriangle1.calcTheorem();
    }
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-07
      • 2018-09-28
      • 1970-01-01
      • 2017-02-18
      • 2019-04-01
      • 1970-01-01
      • 2014-05-19
      • 1970-01-01
      相关资源
      最近更新 更多