Troubleshooting Segmentation Fault Involving Pointer

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2006
Posts: 1,311
Reputation: vishesh is on a distinguished road 
Solved Threads: 36
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso
 
0
  #11
Nov 4th, 2009
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 :

  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: dotnabox is an unknown quantity at this point 
Solved Threads: 0
dotnabox dotnabox is offline Offline
Newbie Poster
 
0
  #12
Nov 4th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: dotnabox is an unknown quantity at this point 
Solved Threads: 0
dotnabox dotnabox is offline Offline
Newbie Poster
 
0
  #13
Nov 4th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 24
Reputation: boblied is an unknown quantity at this point 
Solved Threads: 9
boblied boblied is offline Offline
Newbie Poster
 
0
  #14
Nov 4th, 2009
Originally Posted by dotnabox View Post
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>> .
  1. void Appointment::makeAppt()
  2. {
  3. cin >> *this;
  4. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: dotnabox is an unknown quantity at this point 
Solved Threads: 0
dotnabox dotnabox is offline Offline
Newbie Poster
 
0
  #15
Nov 4th, 2009
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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC