int to string

Hello,

so p=11 which is an int. I convert it to a string and I do the following for loop to display the strings content it will output, 1 1

if (p != 0) {
std::string str_n = to_string(n);
break;
}

for (auto vec: str_n)
cout << vec << " ";

output: 1 1

shouldn't it be 11 despite the space I have in between in cout " "?
No. Why would it be?
the original value is an int converted to a string so I was expecting a string element that is 11 and at index 0, as opposed to it being split into 1 char at index 0 and another char at index 1
A string is just a fancy array of chars. A string isn’t an actual type. It’s a class. Do you know what an array is and do you know what a class is.

Edit: also, if you insert a space into a stream, it’s inserted into the stream. Period. It doesn’t just disappear, there are some things that don’t show up, like some escape characters. (Ever heard of the bell character? That doesn’t show up when you cout it, it makes a sound.)
Last edited on
your help is about as useful as a fart in a wind storm..

Edit: superiority complex much
Last edited on
shouldn't it be 11 despite the space I have in between in cout " "?

You are treating the std::string as a C-style string. Your range-based for loop is printing out individual characters, each array element in the string, with the loop iterations. The output you are getting is correct.
11 is 2 chars as a string (3 in C style). you can't fit it into 1 char. If you just want to print it, cout already knows what to do with int. just cout <<intvar << " " and you have an int followed by a space, if that is what you want. Char is not a string, its a single letter and in reality its a signed byte, nothing more.
Last edited on
I see. got it now.

Thanks for your informative and helpful reply, Furry Guy and jonnin.
And changing int to double clarifies a little.

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 <iostream>
#include <string>

int main()
{
    double n;
    std::string str_n;
    
    std::cout << "Please enter a number: ";
    std::cin >> n;
    str_n = std::to_string(n);
    
    std::cout
    << "Number: " << n << " as a <string> is " << str_n
    << " which is " << str_n.length() << " char's long\n";
    
    int position = 0;
    
    while (position < str_n.length() + 1) // +1 FOR '\0' ARRAY TERMINATOR
    {
        std::cout << '[' << str_n[position] << ']';
        position++;
    }
    std::cout << '\n';
}
nfrate wrote:
superiority complex much

Inferiority complex much?
clearer now. Thanks againtry. I've just been "staring" at the same area of code for several days for a coding challenge. Most of my program works and I have the results in a vector of int's that are the prime factors of a given number. I just need to count the duplicates in order output the results to a string. Hence, I've been playing around with converting ints to strings, etc. I got confused with the size of types, strings, char... anyhow, I think the option might be to use std:map containers to count out the duplicates easily.
"staring" is not an unknown response - we've all done/do it :)

(<maps> are good, but they can produce prolonged stares too)
dutch wrote: Inferiority complex much?


nope, look above at his comments SJW....superiority complex is developed from a defensive need to overcome "inferiority" in the first place...in case you didn't know.
True indeed againtry. I think I'm ready for more pain?? lol
nope, look above at his comments SJW

I'm no SJW you cocksucking little faggot. :-)
Please excuse me if I'm interrupting the flow(?) here, but is there any chance the thread can be green-ticked please?
Last edited on
> superiority complex much?

I apologize if what I said was in any way offensive, I simply have no idea of you skill level. It was an actual question. It just seemed a kinda strange statement to say that a single index of a string would hold another string. I apologize.
Topic archived. No new replies allowed.