Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (2)

Powered by Vanilla. Made with Bootstrap.
Adding to an array of strings
  • Sh3llc0d3
    Posts: 1,910
    Ok guys need help, I've got an array of strings, thing is I'm trying to add to it. I can't work out how it'd be done. My books tell me how to add to plain strings using strcat(str1, str2) I believe. Is there anyway anyone can help me with this? I've got so far, listing how to search for a password in the array using a strcmp statement for the name of the user. I want to add a function so the user can input a new user and password into the array. But like I said I can't work out how, can anyone help?

    #include <iostream>
    #include <cstdio>

    int main()
    {
    using std::cout;
    using std::cin;
    using std::endl;

    int i;
    char str[80];
    char users[10][80] =
    {
    \"tom\", \"pass1\",
    \"tim\", \"pass2\"
    };

    cout << \"Enter name: \";
    cin >> str;

    for(i=0; i<10;i+=2)
    if(!strcmp(str, numbers[i]))
    {
    cout << \"Passowrd is \" << users[i+1] << endl;
    break;
    }
    if(i == 10) cout << \"Not found.\n\";

    return 0;
    }
  • Shit. If it were C I'd be able to help you out. Unfortunately I don't know C++
  • WTF :S
    The program is all fucked up. You have not declared number array, yet you use it.

    But could you elaborate what you (or this program) are trying to do. I would help you sort it out once you explain the problem properly to me.
  • Sh3llc0d3
    Posts: 1,910
    said:


    WTF :S
    The program is all fucked up. You have not declared number array, yet you use it.

    But could you elaborate what you (or this program) are trying to do. I would help you sort it out once you explain the problem properly to me.



    Well at the time I was trying to make a c++ user list complete with passwords. I thought "char users[10][80]" was declaring the array... I then go to define what is in the array as user1 then pass 1 then next line in the array user 2 then pass2 etc all the way to 10 for each.(excuse my memory)

    I wanted to add to this program using an add function extra user's and alter passwords. That was the point of it. And this code was screwed up because I had a concept in my head after looking up arrays and learning a bit about them and couldn't put it down as I visualised it happening.
  • sangf
    Posts: 203
    megabump! i know you most likely don't need an explanation now, but i figured i would give the thread a bit of closure.

    i'm not surprised you were having trouble with this, arrays in C are pretty complex, which is not helped by the fact that many tutorials only cover basic static arrays - dynamic arrays (the most logicially useful type of array) are a different ballgame. well, maybe if ballgames were completely frustrating and confusing.. and played without balls.. /badjoke.

    however, in C++ the STL makes it much easier for us. what you had in that sample is psuedo C/C++, which is still ok.. but unneccesarily complicated, moreso if you want to get something done in code than to learn (dynamic char arrays are a great way to learn about memory management.. how things are stored, etc.).

    if you don't mind using the static array like you already declared, it can actually be quite simple using functions from the C string function family you already mentioned (strcmp/strcat/etc).


    char users[10][10] =
    {
    \"tom\", \"pass1\",
    \"tim\", \"pass2\"
    };


    how this is actually represented in memory, where $ is the null string terminator (identifies where the string ends - usually represented by \0 which is the escape sequence in C/C++):


    . 0 1 2 3 4 5 6 7 8 9
    0 [t][o][m][$][ ][ ][ ][ ][ ][ ]
    1 [p][a][s][s][1][$][ ][ ][ ][ ]
    2 [t][i][m][$][ ][ ][ ][ ][ ][ ]
    3 [p][a][s][s][2][$][ ][ ][ ][ ]
    4 [$][ ][ ][ ][ ][ ][ ][ ][ ][ ]
    5 [$][ ][ ][ ][ ][ ][ ][ ][ ][ ]
    6 [$][ ][ ][ ][ ][ ][ ][ ][ ][ ]
    7 [$][ ][ ][ ][ ][ ][ ][ ][ ][ ]
    8 [$][ ][ ][ ][ ][ ][ ][ ][ ][ ]
    9 [$][ ][ ][ ][ ][ ][ ][ ][ ][ ]
    be careful, we're almost entering the matrix
    p.s. note; probably a good idea to think of this flipped on the axis (my bad)


    so, as you can see there are plenty of places to insert new data. if it's acceptable to specify the size of your array like this and fill the data in using what you have, you can see the below example and description for a pointer in the right direction.



    // initialize 2d char array (users[0], users[1], users[2], users[3] = tom\0, pass1\0, tim\0, pass2\0)
    char users[10][10] =
    {
    \"tom\", \"pass1\",
    \"tim\", \"pass2\"
    };

    // strcpy() copies the string \"tam\" to the array (at 4,0+ on the grid example)
    // users[4] is actually a pointer to the first character of the second dimension of the array - the string is determined by everything between that pointer('s pointee) and the null terminator.
    // so in fact, strcpy() copies the string \"tam\" from anywhere users[4] points to, to 10-1 (80-1 in yours) sizeof(char)s later (ie. be careful because if the string you copy contains more data than that.. well, buffer overflow).
    strcpy(users[4], \"tam\");

    // print the array to test it worked! (we might as well use printf since it's mostly C code anyway, but oh well.
    for (int i = 0; i < sizeof(users)/sizeof(users[0]); i++)
    {
    if (users[i][0] != '\0') cout << users[i] << endl;
    }


    and if that's no good to you, you can either learn about dynamic memory allocation functions in C (malloc, realloc, calloc, etc.) and create your own dynamic array structure/functions. or you can go the C++ route and make use of std::string and std::vector, which i promise is much cleaner to deal with. i've just included a simple sample below which demonstrates its use, there are good STL resources online which you can read to understand further.


    #include <iostream>
    #include <string>
    #include <vector>

    using namespace std;

    int main()
    {
    // declare new vector of strings
    vector <string> users;

    // use push_back() to add a string to the vector (dynamic resizing is done for you)
    // ugly initializing list of values: commence
    users.push_back(\"tom\");
    users.push_back(\"pass1\");
    users.push_back(\"tim\");
    users.push_back(\"pass2\");

    cout << \"Enter additional user to be added to the vector: \";
    string additional_user;
    cin >> additional_user;
    users.push_back(additional_user);

    for (unsigned int i = 0; i < users.size(); i++)
    cout << users[i] << endl; // or users.at(i)

    return 0;
    }


    note: feel free to bitch about things that might be wrong or need correcting and i'll update it with relevant info.
  • Sh3llc0d3
    Posts: 1,910
    Fuck me. I wasn't expecting that. I understand the basic concepts of the memory allocation in arrays you mentioned in your very nice diagram by the way :P lol. At the time I had little experience working with arrays, however since working with arrays in other languages I understand a lot more than I did back then :)

    If I remember rightly at the time I was struggling between whether using strcpy to add to the next line in the array or 'append' if thats the right word with strcat. Something random like that.

    Thanks so much for the reply though, wait till next time you tell me you only know a bit of C++!! :)
  • sangf
    Posts: 203
    said:


    Fuck me. I wasn't expecting that. I understand the basic concepts of the memory allocation in arrays you mentioned in your very nice diagram by the way :P lol.


    thanks lol.

    and yeah, strcat() would work for empty strings, but it's really for appending strings (cat -> concatanate), so strcat(users[0], "tom"); would actually append "tom" to the string that already had "tom" -> "tomtom". both functions actually take a char pointer as their copy/overwrite destination; so just a pointer to the first character, from there strcpy() straight out copies characters to the right of that point till \0 (not that memory have directions, lol but yeah..) while strcat() goes up to the \0, overwrites it following with whatever, and adds a new \0. both do no checking and can easily crash (buffer overflow) iirc.

    what's more interesting is why we are still awake! :p
  • Sh3llc0d3
    Posts: 1,910
    It's 7 am. I spent the best part of 3/4hrs trying to sort that script out and cannot for the life of me work out where I'm going wrong. I need sleep but really getting past the point of tiredness lol.

    Again, great explanation :)
  • sangf
    Posts: 203

    I need sleep but really getting past the point of tiredness lol.


    haha, that's the kind of thing that turns my mornings to nights ;)