views:

70

answers:

6

My code basically is to list ASCII codepoints of a string that is input, my following code is simple, here:

#include <iostream>
#include <string.h>

using namespace std;

int main() {
    char str[20];
    int result[20];
    cin >> str;

    for(int i = 0; i != strlen(str); i++) {
        result[i] = (int)i; 
    }

    for(int i = 0; i != 20; i++)
        cout << result[i] << ", "; 
}

when I run it, no matter what the input it outputs a pile of gibberish like undefined memory like so:

0, 1, 2, 3, 4, 5, 1, -1217349408, -1220040795, -1220041307, -1076427112, 134514781, -1218903292, 134519344, -1076427096, 134514004, -1217411568, 134519344, -1076427048, 134514681,

Am I missing something simple in how I append each integer to the array?

Just note this is a simple example, my input will not be larger than 20 characters.

EDIT Typo in my result.. cin>>result was cin>>str

A: 

You've not initialized str and you are taking its strlen

When you did

cin >> result; // this does not even compile!!!

I guess you meant

cin >> str;

Its not clear what you are trying to do. But you can try this to get some meaningful result:

char str[20];
int result[20] = {0};
cin >> str;
...// rest all unchanged.
codaddict
Along with my edit.. How do I initialize? `char str[20] = {""};int result[20] = {0};`, I'm not sure how to initialize arrays. My result is 20 or so zeros with a few numbers, maybe it's a mistake I made assigning to array..
John
+1  A: 

strlen(str) will give you an undefined output, because you haven't initialised the contents of str[].

Oli Charlesworth
A: 

stlen( str ) will give the number of characters before the null terminator.

This means that only strlen(str) integers are valid. The rest are uninitialized.

Also: have a look at std::transform. You can avoid the temporary array of integers to achieve the same, or transform right into one.

int to_codepoint( char c ) { return (int) c; }


// ...
char cs[] = "abcd";
std::transform( cs, cs+strlen(cs)
             , std::ostream_iterator<int>( std::cout, ", " ), to_codepoint);

// or transform into an array:
int is[ 1000 ]; // 1000 enough?
std::transform( cs, cs+strlen(cs)
              , is, to_codepoint );

(test code at codepad.org)

xtofl
+1  A: 

You have 3 problems:

  1. You didn't initialise str with a proper string, thus strlen will return an unpredictable value.
  2. You initialise the first strlen(str) positions of result, but later you print it until the index 20. You should use the same condition on both loops.
  3. You should definitely use std::string and its iterator.
jweyrich
And you, you cannot count. But 2 seems to be the answer :)
xtofl
@xtofl: oops, indeed forgot to update the count. Thanks!
jweyrich
+1  A: 

Essentially, you failed to correctly initialize the string and you didn't check that it was the correct size. Correct code:

#include <iostream>
#include <string> // NOT <string.h>, <string>

int main() {
    std::string str;
    std::cin >> str;
    std::cin.ignore();
    for(std::string::iterator it = str.begin(); it != str.end(); it++) {
        std::cout << (int)(*it);
        if (it + 1 != str.end())
            std::cout << ", ";
        else
            std::cout << "\n";
    }
    std::cin.get();
}
DeadMG
+1  A: 

This loop will iterate a number of times equal to the length of 'str'. That is, it will iterate once for each character in 'str', and stop at the 'null terminator' (char value of 0) which is how c strings are ended. In each loop, the value of 'i' is the loop number, starting at 0 - and this is the value you assign to that index in the results array.

for(int i = 0; i != strlen(str); i++) {
    result[i] = (int)i; 
}

So for example, for a string of length 5, you will assign the values '0, 1, 2 ,3, 4' to the result array at those indexes, respectively. The other values in the result array are not assigned - and so could hold any value (generally, whatever was in that bit of memory before you started using it). If your string is longer than 20 characters, you're in trouble, because you will start trying to access the array at index 20 and beyond, which is not memory that belongs to your program.

This loop prints out all the values in the 'result' array, from the value at index 0 to the value at index 19:

for(int i = 0; i != 20; i++)
    cout << result[i] << ", "; 

So it will print the initialised values, and, if the string was less than 20 characters long, the uninitialised values as well.

At a minimum, to start getting anything like the results you're after, you want to change

result[i] = (int)i; 

to

result[i] = str[i];

but as mentioned by others, and to escape some of the memory access issues I mentioned above, it would be much better if you use an iterator to get the character values.

for(string::iterator i = str.begin(); i != str.end(); i++)
  // access char here using '*i'
sje397
THANK YOU, it's so late, kept making mistake in my OP. That was a stupid mistake. Thanks.
John