【发布时间】:2012-10-18 15:30:03
【问题描述】:
所以我被设置了一个任务,使用这个等式在 C++ 中创建一个温度转换器:
Celsius = (5/9)*(Fahrenheit – 32)
到目前为止,我已经想出了这个(我从一开始就删掉了价值 10 行的 cmets,所以发布的代码从第 11 行开始,如果这有意义的话)
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
int main ()
{
float celsius;
float farenheit;
std::cout << "**************************" << endl;
std::cout << "*4001COMP-Lab5-Question 1*" << endl;
std::cout << "**************************" << endl << endl;
std::cout << "Please enter a temperature in farenheit: ";
std::cin >> farenheit >> endl;
std::cout << "Temperature (farenheit): " << endl;
std::cout << "Temperature (celsius): " << celsius << endl;
std::cin.get();
return 0;
}
每次我尝试运行这个程序时都会遇到一堆错误,每次都会出现这个错误:
1>m:\visual studio 2010\projects\week 5\week 5\main.cpp(26): error C2678: binary '>>' : no operator found which take a left-hand operand of type 'std ::basic_istream<_elem>'(或没有可接受的转换)
我已经尝试了所有我能想到的方法来消除这个错误,但它每次都会重新出现,你知道如何解决这个问题吗?
【问题讨论】:
-
在 cin 之后需要 endl 吗? (对不起,我的 c++ 技能现在已经尘埃落定了......)
-
为什么是
std::cout和using namespace std;?请选择其中一个。 -
可以使用std::endl,唯一的区别是std::endl会刷新输出缓冲区,而'\n'不会。如果您不想频繁刷新缓冲区,请使用“\n”。如果你这样做了(例如,如果你想得到所有的输出,而程序是不稳定的)。
标签: c++ temperature