LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-10-2003, 11:56 AM   #1
oceaneyes2
LQ Newbie
 
Registered: Dec 2003
Location: Maryland
Posts: 7

Rep: Reputation: 0
Angry C++ char pointer issue


This gone runs but only gives a single letter as output instead of the full name. Can anyone help me out?


#include <iostream.h>
#include <iomanip.h>
#include <string.h>
#include <ctype.h>


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 ;
}

int GetHrs()
{
int hrs;
cout << "\nEnter hours: \n";
cin >> hrs;
cin.ignore( 80, '\n' );

return ( hrs );

}

char GetPayCode()
{
char payCode;
cout << "\nEnter a pay code\n";
cin.get( payCode );
cin.ignore( 80, '\n' );

return ( payCode );
}

//output

void main()
{


for ( int i; i < 6; i++ )
{
cout << "\nEmployee " << i;

char name = *GetName();
char payCode = GetPayCode();
int hrs = GetHrs();
cout << setw( 30 )
<< setiosflags( ios::left )
<< setiosflags( ios::fixed )
<< "Name: " << (name)<< endl;
cout << setw( 7 )
<< setiosflags( ios::fixed )
<< setiosflags( ios::left )
<< "Pay Code: " << payCode << endl;
cout << setw( 7 )
<< setiosflags( ios::fixed )
<< setiosflags( ios::left )
<< "Hours: " << hrs << endl;

}

}

Last edited by oceaneyes2; 12-10-2003 at 12:44 PM.
 
Old 12-10-2003, 01:47 PM   #2
ooagentbender
Member
 
Registered: Sep 2003
Distribution: Vector Linux
Posts: 110

Rep: Reputation: 15
I would say use a string and use cin, getline has delimeters that may be causeing your problem, though I don't know exacly what they are.
 
Old 12-10-2003, 01:59 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Not having initialised the i in the
for-loop, how long does it run? :)


Cheers,
Tink
 
Old 12-10-2003, 02:03 PM   #4
oceaneyes2
LQ Newbie
 
Registered: Dec 2003
Location: Maryland
Posts: 7

Original Poster
Rep: Reputation: 0
runtime

5 sets...i set i
i<6
 
Old 12-10-2003, 02:06 PM   #5
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Quote:
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*.
 
Old 12-10-2003, 02:13 PM   #6
oceaneyes2
LQ Newbie
 
Registered: Dec 2003
Location: Maryland
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
Old 12-10-2003, 02:28 PM   #7
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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...

Code:
char* GetString(char *lpszValue)
{
    strcpy(lpszValue, "ReturnValue");

    return lpszValue;
}

int main()
{
    char szBuffer[256];

    cout << GetString(szBuffer) << endl;

    return 0;
}
 
Old 12-10-2003, 02:31 PM   #8
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Re: runtime

Quote:
Originally posted by oceaneyes2
5 sets...i set i
i<6
That does NOT set i. That compares it.

The code that Tinkster is talking about, is this part here:

Code:
for ( int i; i < 6; i++ )
You never SET i initially, so it is likely to have a garbage value.

Instead, you should do something like so for 5 loops:

Code:
for ( int i=0 ; i < 5; i++ )

Last edited by deiussum; 12-10-2003 at 02:33 PM.
 
Old 12-10-2003, 02:34 PM   #9
oceaneyes2
LQ Newbie
 
Registered: Dec 2003
Location: Maryland
Posts: 7

Original Poster
Rep: Reputation: 0
I missed that. thanks!
 
Old 12-10-2003, 03:14 PM   #10
oceaneyes2
LQ Newbie
 
Registered: Dec 2003
Location: Maryland
Posts: 7

Original Poster
Rep: Reputation: 0
i decided to go with a struc instead


#include <iostream.h>
#include <iomanip.h>



int main()
{
struct PayRecord
{
char name[30]; //max char for name
char payCode[2]; //max char for code
int hrs;
};

PayRecord Employee[5];
int i;

for ( i = 0; i < 5; i++ )
{
cout << "\nEnter a name: \n" << endl;
cin >> Employee[i].name;
cout << "\nEnter hours: \n";
cin >> Employee[i].hrs;
cout << "\nEnter a pay code\n";
cin >> Employee[i].payCode;

cout << "\nName: " << Employee[i].name;
cout << "\nHours: " << Employee[i].hrs;
cout << "\nPay Code: " << Employee[i].payCode;
}
return 0;
}


it is a much more simple process
 
Old 12-10-2003, 04:28 PM   #11
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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.
 
Old 12-10-2003, 04:35 PM   #12
oceaneyes2
LQ Newbie
 
Registered: Dec 2003
Location: Maryland
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration