Jump to content
  • Advertisement
Sign in to follow this  
numegil

C++ char* problem

This topic is 3351 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, First off my apologies if this belongs in a different forum, and my apologies for my poor english at the moment, as it is now 4:30 am and I've been working on this project due tomorrow night for many hours straight. I'm taking a class at my university on C++, which I have used in the past but not extensively and not for a few years. As a result, I don't really have a solid foundation on strings and pointers at all. Any help would be appreciated. http://numegil.webs.com/calendar.cpp http://numegil.webs.com/appts.csv The goal of this assignment is to import the appointments from the appts.csv file and create a sort of "calendar" app to search for them and so forth. The bulk of the operations of the program are commented out right now in main(), and are not really giving me any troubles at all. However, I spent the last 2 hours trying to get the days[].appts[].location and days[].appts[].subject pointers to work properly. After I finish running my import function, it seems as if everything worked correctly. For example, cout << days[17].appts[0].location << endl; provides the correct result. However, if this statement is preceded by cout << days[17].month << endl;, it fails and produces gibberish instead. Additionally, there is a cout << "\n" statement higher up in the code, the removal of which causes the latter cout statement to fail. What is causing these difficulties, and what is the easiest way to resolve them? I would greatly appreciate if someone could take a look at this code and point me in the right direction on how to fix this problem. Thanks a lot! -Numegil

Share this post


Link to post
Share on other sites
Advertisement
You aren't allocating storage for your strings. You fill a local buffer and store the address of the local variable. Consider using std::string instead of char pointers.

Share this post


Link to post
Share on other sites
What do you mean by allocating memory? I added appt.location = new char[80]; before appt.location = sLocation;, but I'm still having the same problems.

Unfortunately, I can't use std::string, since the specs specifically say to use "a dynamically allocated array of char named location".

Thanks!
-Numegil


EDIT: Woot, problem solved. I ended up replacing appt.location = sLocation; with strcpy(appt.location, sLocation);

Share this post


Link to post
Share on other sites
Quote:

Listen to SiCrane and start using std::string. It will save you a lot of trouble.

Unless he's specifically disallowed from using std::string in the assignment specification (which he is), in which case it may cause him to fail the assignment.

Quote:

EDIT: Woot, problem solved. I ended up replacing appt.location = sLocation; with strcpy(appt.location, sLocation);

But the important question is, do you understand what this did and why, and did it really solve your problem or just make it harder to spot?

Does your assignment specifically restrict the use of SC++L classes (a poor educational practice in general)? If not you could use std::vector to save yourself some manual memory management and avoid some hard-coded array sizes.

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
Quote:

Listen to SiCrane and start using std::string. It will save you a lot of trouble.

Unless he's specifically disallowed from using std::string in the assignment specification (which he is)[...]

How do you know that? Perhaps I missed it, but I reread the OP and I still can't see it.

Share this post


Link to post
Share on other sites
Quote:

First off my apologies if this belongs in a different forum, and my apologies for my poor english at the moment, as it is now 4:30 am and I've been working on this project due tomorrow night for many hours straight.

Quote:

The goal of this assignment is to import the appointments from the appts.csv file

Quote:

I can't use std::string, since the specs specifically say to use "a dynamically allocated array of char named location"

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
Quote:
I can't use std::string, since the specs specifically say to use "a dynamically allocated array of char named location"


That's the part I missed.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!