C++ 使用strtol在c++;
我正试图从数组中取出长双精度C++ 使用strtol在c++;,c++,arrays,strtol,long-double,C++,Arrays,Strtol,Long Double,我正试图从数组中取出长双精度 long double num; char * pEnd; char line[] = {5,0,2,5,2,2,5,4,5,.,5,6,6}; num = strtold(line1, &pEnd); 由于某些原因,我得到的数值被四舍五入为502522545.6 我对C++很陌生,所以我有什么不对吗?需要做什么才能得到num中的整数而不是四舍五入的整数 谢谢你的帮助 抱歉,这是我在这里的第一篇帖子=) 因此,整个程序代码如下所示: cla
long double num;
char * pEnd;
char line[] = {5,0,2,5,2,2,5,4,5,.,5,6,6};
num = strtold(line1, &pEnd);
由于某些原因,我得到的数值被四舍五入为502522545.6
我对C++很陌生,所以我有什么不对吗?需要做什么才能得到num中的整数而不是四舍五入的整数
谢谢你的帮助
抱歉,这是我在这里的第一篇帖子=)
因此,整个程序代码如下所示:
class Number
{
private:
long double num ;
char line[19], line2[19];
int i, k;
public:
Number()
{}
void getData()
{
i = 0;
char ch= 'a';
cout << "\nPlease provide me with the number: ";
while ((ch = _getche()) != '\r')
{
line[i] = ch;
line2[i] = ch;
i++;
}
}
void printData() const
{
cout << endl;
cout << "Printing like an Array: ";
for (int j = 0; j < i; j++)
{
cout << line[j];
}
cout << "\nModified Array is: ";
for (int j = 0; j < (i-k); j++)
{
cout << line2[j];
}
cout << "\nTHe long Double is: " << num;
}
void getLong()
{
char * pEnd;
k = 1;
for (int j = 0; j < i; j++)
{
if (line2[j+k] == ',')
{
k++;
line2[j] = line2[j + k];
}
line2[j] = line2[j + k];
}
line2[i -k] = line2[19];
num = strtold(line2, &pEnd);
}
};
int main()
{
Number num;
char ch = 'a';
while (ch != 'n')
{
num.getData();
num.getLong();
num.printData();
cout << "\nWould you like to enter another number ? (y/n)";
cin >> ch;
}
return 0;
}
类号
{
私人:
长双数;
字符行[19],第2行[19];
int i,k;
公众:
编号()
{}
void getData()
{
i=0;
char ch='a';
CUT可能有更多的C++方式,但SCASNF将起作用:
const char *str = "3.1459";
long double f;
sscanf(str, "%Lf", &f);
可能有更多的C++方式,但是sSCANF将工作:
const char *str = "3.1459";
long double f;
sscanf(str, "%Lf", &f);
<>这个C++的方式很简单:
#include <sstream>
#include <iostream>
#include <iomanip>
int main() {
const std::string line = "502522545.566";
long double num;
std::istringstream s(line);
s >> num;
std::cout << std::fixed << std::setprecision(1) << num << std::endl;
}
#包括
#包括
#包括
int main(){
const std::string line=“502522545.566”;
长双数;
std::IStringStreams s(线);
s>>num;
STD::CUT< P>这样做的C++方式很简单:
#include <sstream>
#include <iostream>
#include <iomanip>
int main() {
const std::string line = "502522545.566";
long double num;
std::istringstream s(line);
s >> num;
std::cout << std::fixed << std::setprecision(1) << num << std::endl;
}
#包括
#包括
#包括
int main(){
const std::string line=“502522545.566”;
长双数;
std::IStringStreams s(线);
s>>num;
STD::CUT使用现代C++,你可以简单地做:
auto line = "502522545.566"s;
auto num = std::stold(line);
< P>使用现代C++,你可以简单地做:
auto line = "502522545.566"s;
auto num = std::stold(line);
< /P>这不是有效的C++代码。你想做什么?你肯定这是你的代码吗?我不知道有任何编译器会接受这个。你可以在SCASNF中添加一个修改器来读取长的双倍。这不是有效的C++代码。你想做什么?你肯定这是你的代码吗?我不知道有任何编译器。我接受。你可以在sscanf中添加一个修饰符来读取长双精度。别忘了为了清晰起见省略了#include、#include和#include,但是是的,这些是必需的。很好的一点。如果这个样板真的让你紧张,你可以编写一个转换包装器。我仍然觉得你也可以自己手动解析它应该有一个std::to_float=)@Segmented有:。别忘了为了清晰起见省略了#include、#include和#include,但是是的,这些是必需的。很好的一点。如果这个样板真的让你紧张,你可以随时编写一个转换包装器。你也可以自己手动解析它,我仍然觉得应该有一个std::to_float=)@分段有:。