You can use fin.getline to read a whole name and store it in a char array[100].
Then through strlen(array) find out its length. After that simply make a new dynamic char array of size length.
@jlb - Judging from the difficulty level of the question, I'm guessing its for a homework. Also, this part of forum is for beginners so that's why I tried to keep it as relevant to his code and as simple as possible.
jlib i cant use vectors. im only supposed to use pointers.
taha ilyas, your code isn't working properly. it only displays one name. The problem seems to be in between line 39 & 41. I don't know why its over righting the previous name
@taha ilyas I didn't see your post before I posted my post so my code has nothing to do with the code you provided.
However I realize that this is for homework, however using C-strings and arrays is not really the best way to accomplish this assignment, so I gave a better way of accomplishing the assignment. If the OP can't, for some strange reason, use C++ to accomplish the assignment I'll let him tell me that in a later post. Then after the vector and string have been eliminated one can move on to the horrible C-string and dynamic memory allocations.
By the way your program doesn't compile for me since you failed to #include a required header file. There are also other problems with your code having to do with your dynamic memory and memory leaks.
jlib it isnt my homework. Im just trying to do it on my own for practice. My teacher said "On the initial stage, you should target your logic building and stay away from shortcuts which are provided in different libraries".
Thats why im trying to not use vector and use pointers for that.
I still cant figure out why the previous name is being over-written at line 41.
My teacher said "On the initial stage, you should target your logic building and stay away from shortcuts which are provided in different libraries".
IMO using the STL is not a shortcut, I feel learning to properly use this library is one of the most important aspects of learning C++. Also IMO you shouldn't consider the STL a "different library". If you're trying to learn C++ start with C++ standard features which includes things as vector and string. Wait to learn pointers until you have the basics down and then start slowly. Pointers can be very confusing and error prone, this is why the Standard Template Library, and the string class were developed, to help reduce errors.
The way you appear to be going seems to be learn the C "features", then learn the proper C++ features, which IMO is backwards. If you want to learn C++ learn C++!
taha ilyas, your code isn't working properly. it only displays one name. The problem seems to be in between line 39 & 41. I don't know why its over righting the previous name
Building on taha ilya's work. The asterisk is there to show the jagged nature (at least in theory - I await the thunder-critique-blitz). I couldn't be bothered doing a strlen() or similar). Strings are for the convenience of not dealing with getline but either way it's just a means to an end. Somebody can do the delete cleanup of course.
My teacher said "On the initial stage, you should target your logic building and stay away from shortcuts which are provided in different libraries".
kamilhassaan wrote:
I know how to do this with string. Im trying to do the same thing with char-array just to enhance my skills
I agree with jlb
In education, teachers get their students implement their own sort routines or data structures such as linked lists, so they can understand how they work in detail, rather than blindly using std::sort or std::list say. I think this is what the Teacher meant by that comment. But I think it is a bit of a backward step to delve into char arrays, that is the domain of C programming. As jlb says: if you want to learn C++, then learn C++. That is not to say one can't learn C at some stage - it is handy to know that stuff eventually.
So maybe your time might be better spent implementing your own linked list or queue using classes. In my mind that would be better than messing around with char arrays.
[Main.cpp:30]: (error) Array 'temp[100]' accessed at index 100, which is out of bounds.
[Main.cpp:52]: (error) Array 'temp[100]' accessed at index 100, which is out of bounds.
[Main.cpp:63]: (error) Array 'temp[100]' accessed at index 100, which is out of bounds.
[Main.cpp:71]: (error) Memory leak: names
In line 47 you allocate the memory for the name but why don't you copy the name to the array?