gpt4 book ai didi

C++试图通过getline函数为函数调用获取用户输入值

转载 作者:行者123 更新时间:2023-12-02 10:34:40 24 4
gpt4 key购买 nike

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

错误(事件)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);





}

最佳答案

std::getline 读取字符串,而不是 double 数。所以你必须阅读 std::string 然后将其转换为 double (与 stod )。

您可以改用 >>输入运算符:

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;

关于C++试图通过getline函数为函数调用获取用户输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60917629/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com