| iHutch105 (1080) | |||||||
|
Not writing anything in the cpp file doesn't make sense at all. It's the most reasonable place for all of this code to go. Like I said earlier, you could use initialisation lists for some of these constructors, but given what you've told me so far you won't have covered them yet. So the cpp file seems right to me. Right, I don't normally hand out full code because we usually get a lot of people on these forums looks for handouts. On this case, I'll make a bit more of an exception, because I feel you genuinely want to learn it and I think it's the best way to show you. That way, I can go through it line by line as well. So, here it goes...
Probably way simpler than you thought it'd be. So, what are we doing. We haven't been given a person object, but we've been given the data to create on with. So that's what we'll do.
That creates a person object. Rather than hard coding text values like you have in your "TEST1" example a couple of posts ago, we've been given those values in the arguments. So we'll just pass them straight in. Then we need to add that person to the book. That's easy enough, we've done it before and there's a function for it. So we'll use it.
Remember that "p" in this case is the person object we've just created using the data passed in. And that's it. :-) | |||||||
|
|
|||||||
| xanthian23 (19) | |||
|
And since I have to have this done today, I might as well show you what I've done so far on the array requirement. I'm a little rusty on arrays, and I still have no idea how to pop it into addPerson, but here's what I have: my header: addressBook( PERSON *p, int size );my addressbook.cpp:
and I haven't sorted out how to get that into main yet. Assuming it's even done right. }; | |||
|
Last edited on
|
|||
| xanthian23 (19) | |||
|
Ok, I would have to go with holy cr@p! I can't believe it's that simple. I have been killing myself for days, and it never occurred to me to pass first, last , and address through PERSON, and then call the actual info through the constructor. I had though about using the PERSON function like this, but just passing the actual names and such into it. That of course would have negated calling them them through the constructor in main. I didn't realize I could do it this way. I thought that since fname, lname and Address are all already a part of the PERSON struct that you couldn't do that. Would I still have to use the * when I called them into PERSON?
or is that wrong? | |||
|
|
|||
| iHutch105 (1080) | |
|
That's wrong. The * is a part of the type. You want to pass just the identifier. If you prefer, you can write the arguments in the form char* fName if it helps. That is with the star next to the type, rather than the identifier. The compiler doesn't care, it's whatever makes sense to you.I put the * next to the identifier out of convention. | |
|
|
|
| xanthian23 (19) | ||
When I do it without the star, i get errors for all three saying:
And when I throw the star in there, i runs fine, but nothing comes up in the addressbook when I tell it to print the addressbook. | ||
|
|
||
| xanthian23 (19) | |
| OK, I got it to accept it, I had to go and delete and retype the star, for whatever reason, so no more errors, but it still doesn't show up in the program. | |
|
|
|
| xanthian23 (19) | |||||||
|
I'm gonna repost where I'm at in the code, because I've added and removed and added stuff over the last few days: My Header:
MY addressbook.cpp:
and my main.cpp:
Mabey I just screwed something up | |||||||
|
Last edited on
|
|||||||
| iHutch105 (1080) | |||
|
Doesn't look like you're printing the book you've created using that constructor (the one you called newBook). You'll need newBook.printBook();To clarify, when you make the temporary person in the constructor, there shouldn't be any stars:
| |||
|
|
|||
| xanthian23 (19) | |
|
Yeah, I caught that, but it still gives me errors if I dont put them, and it doesnt place the info in the program if I do. As for the printbook, I just caught that right before I came back here, I realized I had forgotten to add that at the end of main with the other addressbooks. what do you think could be causing the errors? | |
|
Last edited on
|
|
| xanthian23 (19) | |
| yeah, I caught the printbook thing, and realized I hadn't added the new addressbook to it. As for the PERSON p =, if I don't add the star, I get errors, and if I do add the star, I don't get errors, but then it doesn't store the info to the addressbook, am I missing something? | |
|
|
|
| iHutch105 (1080) | |||
|
Oh I see what's happened. Your teacher has created the strings using the array method, rather than the pointer method. I guess the easiest fix would be this...
That should do it. strcpy_s is a function used for copying strings. It the first string you give it is the destination, the second is the source. This will only work on C-style strings.More information here: http://www.cplusplus.com/reference/cstring/strcpy/ I hate C-style strings... | |||
|
|
|||
| xanthian23 (19) | |
|
Yeah, I had seen in our forums for class that everyone was trying it with the char *fName~ and not getting it either, (which makes me feel a little better) I'm assuming he's just taking us slow, but this does seem to be a bit of extra work. I had tried this once as well, but didn't realize I could pass it to the PERSON p, so just gave up on it. As far as I know, so far nobody has been able to get this to print onto the screen in the program, even the one girl who said she was using this method. Even with this, it still isn't coming up with the info. Maybe its something wrong with the starter code he gave us. When you get a chance, could you have a look at my array and tell me what I'm doing wrong or missing. It's been a while since I've done one, and I'm looking up to formatting info about it now from the teachers level one lectures I still have from years ago in my level 1 class, but any help would be appreciated. | |
|
|
|
| iHutch105 (1080) | |
|
It'll be a quick post because I'm off to bed. With your array... Get rid of the int size = 25; statement.Loop statement is fine. Inside the loop, you should have a call to addPerson passing in the person at index i of the array passed in. | |
|
|
|
| xanthian23 (19) | |
| Cool, I'm surprised I actually did something right for a change. Thanks again for all your help. | |
|
|
|