[英]I would appreciate help converting ints/floats to strings in 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 << "millimeters\n";
cout << "In years the ocean's level will be higher by " << sevenYears << "millimeters\n";
cout << "In years the ocean's level will be higher by " << tenYears << "millimeters\n";
return 0;
}
這就是我到目前為止所擁有的。 我大約一周前才開始使用 c++,但我仍然不確定如何將浮點數和整數轉換為字符串。 我的 output 應該打印帶有字符串結果的語句。
你可以使用這個
#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 << " millimeters\n";
cout << "In years the ocean's level will be higher by " << sevenYears << " millimeters\n";
cout << "In years the ocean's level will be higher by " << tenYears << " millimeters\n";
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 << " millimeters\n";
cout << "In years the ocean's level will be higher by " << sevenYears << " millimeters\n";
cout << "In years the ocean's level will be higher by " << tenYears << " millimeters\n";
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;
}
Output
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中檢查/運行此示例
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.