[try Beta version]
Not logged in

 
how to convert a word to int

Aug 14, 2017 at 9:43am
Hy guys i need a simple way to convert a generic word to int!
Last edited on Aug 14, 2017 at 9:43am
Aug 14, 2017 at 10:51am
Is the following simple enough?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <numeric>
#include <iostream>
#include <string>

void waitForEnter();

int main()
{
    std::string text {"Hy guys i need a simple way to convert a generic word to int!"};
    std::cout << "\"" << text << "\" converted into integer is "
              << std::accumulate(std::next(text.begin()), text.end(), 
                                 static_cast<int>(text.at(0)),
                                 [](int a, char b) { 
                                    return a + static_cast<int>(b);
                                 })
              << '\n';
    waitForEnter();
    return 0;
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

Last edited on Aug 14, 2017 at 10:53am
Aug 14, 2017 at 11:02am
Thank you a lot !! :)
Topic archived. No new replies allowed.