我将感谢帮助转换int /float到c++中的字符串


#include <iostream>
using namespace std;
int fiveYears;
fiveYears = 5 * 1.5;

int sevenYears;
sevenYears = 7 * 1.5;
int tenYears;
tenYears = 10 * 1.5;
int main()
{
cout << "In years the ocean's level will be higher by " << fiveYears << "millimetersn";

cout << "In years the ocean's level will be higher by " << sevenYears << "millimetersn";

cout << "In years the ocean's level will be higher by " << tenYears << "millimetersn";
return 0;
}

这就是我目前得到的。我大约一周前才开始使用c++,我仍然不确定如何将浮点数和整数转换为字符串。我的输出应该打印语句和字符串的结果。

你可以使用

#include <iostream>
using namespace std;
int fiveYears;
int sevenYears;
int tenYears;
int main()
{
fiveYears = 5 * 1.5;
sevenYears = 7 * 1.5;
tenYears = 10 * 1.5;
cout << "In years the ocean's level will be higher by " << fiveYears << " millimetersn";
cout << "In years the ocean's level will be higher by " << sevenYears << " millimetersn";
cout << "In years the ocean's level will be higher by " << tenYears << " millimetersn";
return 0;
}

#include <iostream>
using namespace std;
int fiveYears = 5 * 1.5;
int sevenYears = 7 * 1.5;
int tenYears = 10 * 1.5;
int main()
{
cout << "In years the ocean's level will be higher by " << fiveYears << " millimetersn";
cout << "In years the ocean's level will be higher by " << sevenYears << " millimetersn";
cout << "In years the ocean's level will be higher by " << tenYears << " millimetersn";
return 0;
}

来解决这个问题,如果你真的想使用全局变量。

可以使用std::to_string将数值转换为std::string。

#include <iostream>
int main()
{
int v1 = 61;
long v2 = 62L;
long long v3 = 63LL;
unsigned int v4 = 64;
unsigned long v5 = 65UL;
unsigned long long v6 = 66ULL;
float v7 = 67.0f;
double v8 = 68.0;
long double v9 = 69.0L;
std::string v1Str = std::to_string(v1);
std::string v2Str = std::to_string(v2);
std::string v3Str = std::to_string(v3);
std::string v4Str = std::to_string(v4);
std::string v5Str = std::to_string(v5);
std::string v6Str = std::to_string(v6);
std::string v7Str = std::to_string(v7);
std::string v8Str = std::to_string(v8);
std::string v9Str = std::to_string(v9);
std::cout << v1Str << std::endl;
std::cout << v2Str << std::endl;
std::cout << v3Str << std::endl;
std::cout << v4Str << std::endl;
std::cout << v5Str << std::endl;
std::cout << v6Str << std::endl;
std::cout << v7Str << std::endl;
std::cout << v8Str << std::endl;
std::cout << v9Str << std::endl;
int fiveYears = 5;
int sevenYears = 7;
int tenYears = 10;
float ratio = 1.5;
std::cout << u8"In " + std::to_string(fiveYears) + u8" years the ocean's level will be higher by " + std::to_string(fiveYears * ratio) + u8" millimeters" << std::endl;
std::cout << u8"In " + std::to_string(sevenYears) + u8" years the ocean's level will be higher by " + std::to_string(sevenYears * ratio) + u8" millimeters" << std::endl;
std::cout << u8"In " + std::to_string(tenYears) + u8" years the ocean's level will be higher by " + std::to_string(tenYears * ratio) + u8" millimeters" << std::endl;
}

61
62
63
64
65
66
67.000000
68.000000
69.000000
In 5 years the ocean's level will be higher by 7.5 millimeters
In 7 years the ocean's level will be higher by 10.5 millimeters
In 10 years the ocean's level will be higher by 15 millimeters

检查/运行这个例子https://repl.it/@JomaCorpFX/IntFloatsToString#main.cpp

最新更新