[英]Can someone please explain what's wrong with this c++ code?
The program below is an example from Absolute C++ by Walter J Savitch.下面的程序是来自 Walter J Savitch 的 Absolute C++ 的一个示例。 I'm trying to run this code but getting errors but I cant figure out why.
我正在尝试运行此代码但出现错误,但我不知道为什么。 This is an example of user defined functions.
这是用户定义函数的示例。 The function
round()
is supposed to return an int
value after rounding a double
value函数
round()
应该在舍入double
int
值后返回一个int
值
#include <iostream>
#include <cmath>
using namespace std;
int round (double number);
int main()
{
double doubleValue;
char ans;
do
{
cout << "Enter a double value: ";
cin >> doubleValue;
cout << "rounded that number is " << round(doubleValue) << endl;
cout << "Again?" << endl;
cin >> ans;
}while(ans == 'y' || ans == 'Y');
cout << "end of testing " << endl;
return 0;
}
int round(double number)
{
return static_cast<int>(floor(number + 0.5);
}
[1]: [1]:
http://i.stack.imgur.com/ABj8G.png this is the errors that I'm getting. http://i.stack.imgur.com/ABj8G.png这是我得到的错误。
At first, I don't know why you have implemented round() function, since there is a round() function is c.一开始,我不知道你为什么要实现round()函数,因为有一个round()函数是c。
Second, you have implemented round() function incorrectly.其次,您错误地实现了 round() 函数。 you need something like this:
你需要这样的东西:
int round(double num)
{
return static_cast<int>(num+0.5);
}
From your error round is already defined in another file, make a new one with a new name and it should work从你的错误回合已经在另一个文件中定义,用新名称创建一个新的,它应该可以工作
int my_round (double number);
int main()
{
// ...
cout << "rounded that number is " << my_round(doubleValue) << endl;
// ...
}
int my_round(double number)
{
return static_cast<int>(floor(number + 0.5));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.