简体   繁体   中英

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;
}

So this is what I have so far. I have only started c++ about a week ago and I am still unsure about how to convert floats and integers into strings. My output should prints the statements with the results of the strings.

You can either use this

#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;
}

or this

#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;
}

to resolve this issue if you really want to use global variables.

You can usestd::to_string Converts a numeric value to std::string.

Example

#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

Check/run this example in https://repl.it/@JomaCorpFX/IntFloatsToString#main.cpp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM