ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
char *GetName()
{
const int NUMEMP = 5;
const int MAXNAME = 30;
char name[30];
char i;
cout << "\nEnter a name: \n";
cin.getline (name,30);
for ( int i = 0; i < NUMEMP; i++ )
return name ;
}
// snip
char name = *GetName();
There are a couple of problems in the bits of code I pasted above...
First, you are returning a pointer to an array that has been allocated on the function stack!! This is bad. REAL BAD! The memory that the pointer you are returning will no longer be allocated once the function returns!
Second, assuming for a moment that you aren't going to get a segmentation fault due to my first point... You are storing the result of your function (a char*) into a char, dereferncing it in the process. This will give you only a single character.
One option (not necessarily the best) would be to dynamically allocate the memory for the pointer that you are returning from your function. (Use new/malloc), then when you are done be sure to release the memory (using delete/free).
Another, would be to have your function take in a char*, allocating memory for it outside the function.
Another option is the suggestion that ooagentbender mentioned. Using a std::string instead of a char*.
I am desperately trying to get this program to work. I have to have it all turned in by 5 pm and its already 3pm. I need to create a program that asks for names, and hours. and from that data collected create a new program that displays each name the pay code which has been converted into a int {20,15, 10} based on char{A, B, C}. Then i have to calculte overtime (hours over 40 get time and a half) as well as regular hours. I've been up for 3 days without sleep, and I have very littel tworkign code to show for it. any help is appreciated.
So essentially, you are asking us to do your homework for you.
Maybe sometime in those 3 days, you could have discussed the problems you are having with your teacher. In my experience, teacher's are generally pretty understanding and will try to help you out by explaining what you are doing wrong.
I won't help you cheat, but I'll give you some sample code for one of the more appropriate ways to return a char* from a function...
One quick comment on that code... you are defining the struct within the main() function... Usually, it's not a good idea to define a struct within the scope of a function. It will only be usable within that function, so if you later wanted to pass that struct to another function, you wouldn't be able to unless you moved the struct outside the main() function.
thank you deiussum! I know this way will give me more touble if i were to try and use it elsewhere, at this point i just want it to run
I need to turn it in, and get a grade rather than a zero. as long as it runs I'll be in the clear at this point. I'll touch it up and move things around one i get things running.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.