Hi there, my original lab assignment was to work with strings and pointers. Our assignment was to ask for a first name, middle name and last name. Create and enter them into a dynamic array, then display the array. Well I accomplished this, but wanted to take it one step further.
With the inserted code, I have created a struct and implemented my code to create an array to store multiple names- up to 100 as defined in main. So I can input the name, count the chars- but for some reason cannot pass the fullName to the displayName function for output.
I bothered typing to point out the serious flaw in your code that could cause a segmentation fault...is pointing out mkistakes not helpful? I though it was a joke because you wrote 0...
Also the null char is accounted for in: int totalLength = fnameLength + mnameLength + lnameLength + 3;
Clearly I dont understand, can you explain what you mean by serious flaw. Obviously I am a beginner so the only way to learn is to dangle my code out there to be ripped apart. However I will get better if I know what I am doing wrong- ie someone explains it.
What L B tries to say is that you set userInput to zero and then you create 3 strings of length userInput. So you have 3 zero sized strings which don't even have room for the null char.
This is actually correct. The userInput is set to 0 to initialize the dynamic array, which is populated by the three cin requests. This part works fine, its passing that information on where I am having problems.
Thanks that worked but probably not the way you were suggesting. I simply added std::string str; to my private variables, then pointed str += fullName array.
Enter s1: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
s1: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
s2: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Since you didn't give s1 the proper size, line 7 wrote over s2. That's a really, really bad thing to happen, and one of the most difficult bugs to find. So, either give your strings a proper size or use std::string.
Ok here are the changes- you were absolutely right too. I added a fixed size of 50- making sure to cover any part of the name, assuming it is equal to or under 50 chars. I also forgot to delete the new char array when I was done. Thanks bbgst!
Lines 16 to 19: these are signle characters, and on top of that you never even use them in the GetNames function.
Line 59: If you're going to do this at all, why not have std::strings for first, middle, and last and input to them directly? There is no need to do all the above code if you're going to use std::string...
Line 72: What about fName, mName, and lName?
Line 98: Just a not here, you didn't have to change to j instead of i. The i from line 93 does not exist at this point.
There are other less serious problems, but I didn't want to overwhelm things by having too many things change at once.
Thanks L B, for this assignment we had to use chars. We were asked to create a dynamic array, take in chars for first, middle and last names, store those in one array, count them, then display them together as one name. I took it one step further by wanting to do this multiple times( or as many as the user wants).
Thanks for the suggestions- I have ammended the code: Let me know if you see anything else.
I created pointers in the private area for the member variables for fName, mName and lName. Is this correct usage?
My suggestions were somewhat independent from each other; you seem to have combined incompatible suggestions. Also, I was unsure if you had wanted to learn the hard way (character arrays) or the easy way (std::string) to do this, because you said you had already completed and turned in your assignment. I suggest you pick one or the other but not both.
Lines 77-80: Now that you are correctly storing the arrdesses of the names in your class, you should have this code instead at the beginning of the function and only delete them if they already exist. This way, you avoid memory leaks and thus can call GetNames as many times as you want. Currently, though, you delete them making them useless, leaving str the only way to get the name.
Now, to the stuff I couldn't get to before.
On line 96 you make 100 names, but then you actually ask the user how many they want on line 98. If you switched the order, you could dynamically allocate the array with as many elements as they wanted, be it 1 or 100000.
On line 108, you have to add 1 to j, but you could easily start the loop at j = 1 and have the condition be j <= inputNames, thus you wouldn't have to add one to j. Just a minor issue of preference in loop style; I try to make my code 'lazy'.
If there's anything else I'll point it out next time. ;)
Actually I dont have to hand it in until Sunday. I would very much appreciate learning the hard way(character arrays) and not combining the two as I have done. How would you accomplish the task of inputing chars...etc then passing them for display? The later is where I think I am really struggling..
Thanks for your help. I will adjust the code based on your suggestions.
Also I am really learning alot and appreciate your time.
You're actually there now, just get rid of any traces of the str variable or std::string and make the changes I mentioned, and you should be good to go. ;)