Sizeof help finding size of user inputed text

Im not sure if it is possible to get the size of user inputed text in bytes, i tried it and it just keeps saying 4 which is the size of the string not the text. here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main()
{
    string bytes;

    cout << "Enter some text" << endl;
    cin >> bytes;

    if(sizeof(bytes) >= 70)
    {
        cout << "your array is " << sizeof(bytes) << " Bytes long!" << endl;
    }else{
        cout << "Your array is " << sizeof(bytes) << " Bytes long!" << endl;
    }
}
closed account (zb0S216C)
Ch1156 wrote:
sizeof(bytes)

No, that will return the size of the internal pointer. Instead, invoke std::string::length( ), like so: bytes.length( ).

Wazzak
Last edited on
Ok, it stopes reading bytes at the first whitespace can i fix that??
Use getline:
getline(cin,bytes);

http://www.cplusplus.com/reference/string/getline/
ok, that works now i have yet another problem, in my code you see:
if(sizeof(bytes) >= 70)

now, i entered a bunch of words and it all equaled 129 bytes, yet it goes straight to the else statement, saying that its a small amount of byts when it should be large, here is my new code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main()
{
    string bytes;

    cout << "Enter some text" << endl;
    getline(cin, bytes);

    if(sizeof(bytes) >= 70)
    {
        cout << "your array is " << bytes.length() << " Bytes long! (Large)" << endl;
    }else{
        cout << "Your array is " << bytes.length() << " Bytes long! (Small)" << endl;
    }
}


I know i could fix this by using 2 if statements but it seems like every time i use an else statement it never works so i need to figure out why for future use.
Last edited on
It looks like you didn't read Framework's reply. sizeof gives the structure size of an object. It is always a constant known at compile time. Why don't you print the value of sizeof(bytes)?

I know i could fix this by using 2 if statements

I don't see how that would fix it.
Last edited on
Ok yeah it doesnt work. So basically what i want to acomplish with this is impossible?
What? length() gives you the length of a string. So what exactly is the problem?
I want to know the length of each characters byte that the user enters, well, hmm not that i think about it, it really seems dumb. I know getline gets the text length but what i wanted was the size of the bytes that each character entered is.
Unless you're working with some multi-byte encoding scheme (like UTF-8) all characters are 1 byte.

And even if you are working with multi-byte encoding schemes -- that is exactly the number that length() would give you anyway. length() gives you the dumb length (the number of 'chars' used). If a glyph takes multiple chars, std::string doesn't know about that. So length() is the length in chars, not in glyphs.
Last edited on
I see, ok thats all i needed to know, thanks. :)
If you want to account for multi-byte encoding schemes could you do this:

cout << "your array is " << bytes.length()*sizeof(bytes[0]) << " Bytes long!

This takes the size of the first element in bytes instead of the size of the pointer.

EDIT: come to think of it, a multi-byte encoding scheme would use std::wstring instead of std::string so I think that sizeof(bytes[0]) would always be 1 if we don't use std::wstring.
Last edited on
Topic archived. No new replies allowed.