不匹配'运营商>>'在' std :: cin?

时间:2014-08-30 23:08:55

标签: c++

简单的程序告诉你,无论我得到多少牛奶成本,我都会得到这个错误“不匹配'运算符>>'在'std :: cin ??“我是c ++的初学者,但仍然是什么地狱。

也是这个错误:“在函数'int main()':”

#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or      input loop */

const double CARTONLOAD = 3.78;
const double CARTONCOST = 3.78 * .38;
const double CARTONPROFIT = 0.27;

int main() 
{
    double totalmilk = 0;
    double milkcartonsneeded = 0;
    double milkcost = 0;

    cout << "Enter total amount of milk produced in the morning in Liters" << endl;
    cin >> totalmilk >> endl;
    milkcartonsneeded = totalmilk/CARTONLOAD;
    cout << " Number of milk cartons needed to hold milk: "  << milkcartonsneeded << endl;
    milkcost = milkcartonsneeded * CARTONCOST;
    cout << " The cost of producing milk is: " << milkcost << endl;
    cout << " The profit for producing milk is: " << milkcartonsneeded * CARTONPROFIT - milkcost << endl;

    return 0;
}

3 个答案:

答案 0 :(得分:7)

endl是输出流操纵器。 cin是输入流。我不确定你期望endl在这里做什么:

cin >> totalmilk >> endl;

但这是错误的。

答案 1 :(得分:4)

这是问题

cin >> totalmilk >> endl;

因为endl而给出错误。删除它。

答案 2 :(得分:1)

operator<<有一个重载,它接受一个函数指针指向一个接收std::basic_ostream的函数。这允许您在std::endl链中使用“流操纵器”,即operator<<。这允许您执行以下操作:

std::cout << "hey.";
std::endl(std::cout);
std::cout << "hello.";

因为std::endl只是一个std::basic_ostream的函数。但是,它也会通过引用返回一个(类似于operator<<),这意味着它可以出现在一个链中,即std::cout << std::endl

由于std::cinstd::basic_istream,因此您拥有不兼容的参数。

相关问题