944,072 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1071
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Nov 4th, 2009
0
Re: Troubleshooting Segmentation Fault Involving Pointer
To avoid buffer overflow, prefer using cin.getline(char_ptr, size);

>> With that code, calling the >> operator will always store it into appt.subject, but what about storing information to appt.location? Any suggestions on an approach?
Actually there are more than one ways to do that. Simplest being :

C++ Syntax (Toggle Plain Text)
  1. in >> subject >> location

However you get few problems:
1. Cant use space, in strings
2. Buffer overflow
3. Will surely confuse the user

So you print message before every input like you do in normal program:
C++ Syntax (Toggle Plain Text)
  1. cout << "\nEnter Location: ";
  2. in.getline(location, 60);
  3. cout << "\nEnter Subject: "
  4. in.getline(subject, 60);
Last edited by vishesh; Nov 4th, 2009 at 6:17 pm.
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Nov 4th, 2009
0
Re: Troubleshooting Segmentation Fault Involving Pointer
The second one is exactly what I'm looking for. I don't know how I forgot about using getline. My problem is, how would I call that operator in the first place? I feel like the order would be off.

I very specifically have to have my Day class call this Appointment class, which is how it is now, but then wouldn't I need some sort of cout statement before I call the >> operator? In which case, wouldn't I then have cout statements in both my makeAppt() function and also in my overloaded operator? It seems messy to me. Or I'm missing something.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dotnabox is offline Offline
18 posts
since Oct 2009
Nov 4th, 2009
0
Re: Troubleshooting Segmentation Fault Involving Pointer
So I think part of my problem is that I haven't fully explained exactly what I am looking for. Both of your responses are spot on, I understand them, but they don't quite fit my need here and I'm not sure how to adapt them. Let me try to clarify.

The exact console should look like this:

Calendar Menu
0. Done.
1. Search for date.
2. Search for subject.
3. Add an appointment.
Your choice >> 3

Please enter the month and day (mm/dd) >> 10/29
Subject >> Appointment
Location >> 3052 Kemper
Start time >> 6:15 PM
End time >> 7:37 PM

The overloaded >> operator in the Day class will then call the overloaded >> operator or the Appointment class (what we've been talking about this whole time), which will finally call the overloaded >> operator in the Time class.

Therefore, I need to use the overloaded operator in my Appointment class to read in an Appointment and store the user input to *location and *subject, but they must be on separate lines, as shown above.

And thank you two for all your help. I'm writing down all of what you've told me as it's invaluable, even beyond thescope of my current assignment.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dotnabox is offline Offline
18 posts
since Oct 2009
Nov 4th, 2009
0
Re: Troubleshooting Segmentation Fault Involving Pointer
Click to Expand / Collapse  Quote originally posted by dotnabox ...
I very specifically have to have my Day class call this Appointment class, which is how it is now, but then wouldn't I need some sort of cout statement before I call the >> operator? In which case, wouldn't I then have cout statements in both my makeAppt() function and also in my overloaded operator? It seems messy to me. Or I'm missing something.
Glad it's been helpful; one more guess before I call it a day. You could delegate makeAppt to operator>> and leave the appointment prompting in operator>> .
C++ Syntax (Toggle Plain Text)
  1. void Appointment::makeAppt()
  2. {
  3. cin >> *this;
  4. }
Reputation Points: 36
Solved Threads: 9
Newbie Poster
boblied is offline Offline
24 posts
since Mar 2009
Nov 4th, 2009
0
Re: Troubleshooting Segmentation Fault Involving Pointer
That will do the trick. I know it's not what he had in mind, but again, I can revise it later. Thanks for everything!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dotnabox is offline Offline
18 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Some thoughts
Next Thread in C++ Forum Timeline: How to insert data in CLOB column





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC