Timmah339 Posted November 30, 2009 Share Posted November 30, 2009 hi all, i'm really new to C++, and i want to be able to create an array of a variable length. for example: int myarray; i know this cannot be done as i've typed above, but i've heard there is another way to do it. does anyone know how? it's crucial to my project. also, i know that strings are just arrays, and i want to make a string of variable length, with an underscore for each place.(hangman game) i wrote this loop, but the compiler won't accept it. why? for(int i=0; i<len; i++) { word[i]='_'; } Link to comment Share on other sites More sharing options...
0 Argote Posted November 30, 2009 Share Posted November 30, 2009 Yes, you need to use a pointer to an int and a malloc, like this: // This will be your array, you can access it like a regular array if you want to e.g.: myarray[X] where X is a number between 0 and size-1 int* myarray; [...] // code here until it's time to declare // This declares the number of bytes you want to use, thats why we multiply the size you want by the number of bytes used for each int myarray = malloc(sizeof(int) * size); [...] // code here until you're done with it // Don't forget to free the memory. free(myarray); Hope this helps :) Link to comment Share on other sites More sharing options...
0 Timmah339 Posted November 30, 2009 Author Share Posted November 30, 2009 Yes, you need to use a pointer to an int and a malloc, like this: int* myarray; [...] // code here until it's time to declare myarray = malloc(sizeof(int) * size); // This declares the number of bytes you want to use, thats why we multiply the size you want by the number of bytes used for each int [...] // code here until you're done with it free(myarray); // Don't forget to free the memory. that looks good, but what's a malloc? also, i heard something about the "new" command, which is supposed to be C++ specific. is that any better? Link to comment Share on other sites More sharing options...
0 Argote Posted November 30, 2009 Share Posted November 30, 2009 As for your other snippet, what compiler are you using? You might need to declare all of your variables at the top of each function so you'd: // declare i int i; [...] // Then use it for(i = 0; i < len; i++) { word[i] = '_'; } Also, how did you declare word? and where/how are you getting len? Link to comment Share on other sites More sharing options...
0 Argote Posted November 30, 2009 Share Posted November 30, 2009 that looks good, but what's a malloc? also, i heard something about the "new" command, which is supposed to be C++ specific. is that any better? Oh ok, C++ specific you can use new, it's a bit easier to use and can be used to declare objects as well. I'm really not that familiar with the differences between the two for arrays. // This will be your array, you can access it like a regular array if you want to e.g.: myarray[X] where X is a number between 0 and size-1 int* myarray; [...] // code here until it's time to declare // This declares the array of size size myarray = new int [size]; [...] // code here until you're done with it // Don't forget to free the memory. delete[] myarray; Link to comment Share on other sites More sharing options...
0 Timmah339 Posted November 30, 2009 Author Share Posted November 30, 2009 Oh ok, C++ specific you can use new, it's a bit easier to use and can be used to declare objects as well. I'm really not that familiar with the differences between the two for arrays. // This will be your array, you can access it like a regular array if you want to e.g.: myarray[X] where X is a number between 0 and size-1 int* myarray; [...] // code here until it's time to declare // This declares the array of size size myarray = new int [size]; [...] // code here until you're done with it // Don't forget to free the memory. delete[] myarray; ok, i think i figured it out, your code helped a lot! thank you, sir! Link to comment Share on other sites More sharing options...
0 Timmah339 Posted November 30, 2009 Author Share Posted November 30, 2009 ok, now that i've figured that out, i've run into another hitch. i know how to replace a character in a string with another character, but how do i replace multiple occurrences of that character within the same string? Link to comment Share on other sites More sharing options...
0 Solid Knight Posted November 30, 2009 Share Posted November 30, 2009 Use a vector. Link to comment Share on other sites More sharing options...
0 Timmah339 Posted November 30, 2009 Author Share Posted November 30, 2009 Use a vector. yeah...we haven't been taught vectors yet. it's a painfully slow class Link to comment Share on other sites More sharing options...
0 Argote Posted November 30, 2009 Share Posted November 30, 2009 ok, now that i've figured that out, i've run into another hitch. i know how to replace a character in a string with another character, but how do i replace multiple occurrences of that character within the same string? Loop through the string and check each instance, if it matches what you want to replace then replace it. Link to comment Share on other sites More sharing options...
0 Timmah339 Posted November 30, 2009 Author Share Posted November 30, 2009 Loop through the string and check each instance, if it matches what you want to replace then replace it. oh, wow, that's simple. how did i NOT think of that? Link to comment Share on other sites More sharing options...
0 Andre S. Veteran Posted November 30, 2009 Veteran Share Posted November 30, 2009 Use a vector.Yeah, basically: vector<char> here_s_an_array_of_variable_length_that_s_full_of_win; And problem solved. :) Link to comment Share on other sites More sharing options...
0 +SOOPRcow MVC Posted November 30, 2009 MVC Share Posted November 30, 2009 A vector is way to a complex of a solution for such a simple problem. Link to comment Share on other sites More sharing options...
0 Andre S. Veteran Posted November 30, 2009 Veteran Share Posted November 30, 2009 A vector is way to a complex of a solution for such a simple problem.The implementation of vector is complex, but using it? You just need to declare it, and you're done. One line of code and problem solved. Link to comment Share on other sites More sharing options...
0 Xilo Posted November 30, 2009 Share Posted November 30, 2009 (edited) A vector is way to a complex of a solution for such a simple problem. I had a class with Dr. Stroustrup teaching it (creator of C++). Even he advocated use of std::vector whenever possible over pointer arrays or arrays period. There are very few cases of where using an array or pointer array is better than using a vector as vectors offer similar performance to arrays and are much easier to use. Edited November 30, 2009 by Xilo Link to comment Share on other sites More sharing options...
0 +SOOPRcow MVC Posted November 30, 2009 MVC Share Posted November 30, 2009 Yeah, but a vector class for a string? Why not just use a string class? Link to comment Share on other sites More sharing options...
0 Xilo Posted November 30, 2009 Share Posted November 30, 2009 Yeah, but a vector class for a string? Why not just use a string class? I'm pretty confused about that myself... Link to comment Share on other sites More sharing options...
0 Lant Posted November 30, 2009 Share Posted November 30, 2009 And for replacing characters there's always std::replace Link to comment Share on other sites More sharing options...
0 Andre S. Veteran Posted December 1, 2009 Veteran Share Posted December 1, 2009 Yeah, but a vector class for a string? Why not just use a string class?True. vector is the general alternative to arrays, string is better specifically for character arrays.Wonder how I missed that. :blush: Link to comment Share on other sites More sharing options...
0 Timmah339 Posted December 1, 2009 Author Share Posted December 1, 2009 yeah i just used a for() loop to find and replace some stuff after converting the string to an array. thanks for the suggestions, though, i think they'll help me out later on. Link to comment Share on other sites More sharing options...
Question
Timmah339
hi all, i'm really new to C++, and i want to be able to create an array of a variable length. for example:
int myarray;
i know this cannot be done as i've typed above, but i've heard there is another way to do it. does anyone know how? it's crucial to my project.
also, i know that strings are just arrays, and i want to make a string of variable length, with an underscore for each place.(hangman game)
i wrote this loop, but the compiler won't accept it. why?
for(int i=0; i<len; i++) { word[i]='_'; }Link to comment
Share on other sites
19 answers to this question
Recommended Posts