Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,988 questions

51,933 answers

573 users

How to convert an integer values in string to integers in C++

1 Answer

0 votes
#include <cstdlib>
#include <iostream>
 
int main()
{
    const auto strings_with_numbers = {
        "89",
        "0x3A", // "0" and string - not hexadecimal
        "3.14159",
        "84933 c++ programmming",
        "c/c++ 20",
        "-0409",
        "900000000000" // out of int32 range
    };
 
    for (const char* str : strings_with_numbers) {
        const int i{std::atoi(str)};
        std::cout << "std::atoi('" << str << "') = " << i << '\n';
        if (const long long ll{std::atoll(str)}; i != ll) {
            std::cout << "std::atoll('" << str << "') = " << ll << " string to long long\n";
        }
    }
}



/*
run:

std::atoi('89') = 89
std::atoi('0x3A') = 0
std::atoi('3.14159') = 3
std::atoi('84933 c++ programmming') = 84933
std::atoi('c/c++ 20') = 0
std::atoi('-0409') = -409
std::atoi('900000000000') = -1943132160
std::atoll('900000000000') = 900000000000 string to long long

*/

 



answered Apr 30, 2024 by avibootz

Related questions

...