getline problem

A

Alex

here is the code i am having trouble with.
// first, I have a global variable
char *sName[ 1 ];
....
.....
// and then this function
void set_characterNameAgeSex()
{
char setName[ 50 ];
char *sTransfer;
short setSex;
short setAge;

// set name
cout << "Enter a name for your character(50 char. max): ";
cin.getline( setName, 49 );

sTransfer = setName;
sName[ 0 ] = sTransfer;
......
.........

-------------------
So what's happening is that the line: cin.getline( setName, 49 ); is being
skipped....but the cout line before that is being printed.

I'm using VC++6.0, and I have applied the fixes, but nothings changed.

I don't know if something is wrong with the compiler.......

Please help.
Thanks in advance.
 
V

Victor Bazarov

Alex said:
here is the code i am having trouble with.
// first, I have a global variable
char *sName[ 1 ];

Why would you ever need an array of length 1, honestly?
...
....
// and then this function
void set_characterNameAgeSex()
{
char setName[ 50 ];
char *sTransfer;
short setSex;
short setAge;

// set name
cout << "Enter a name for your character(50 char. max): ";
cin.getline( setName, 49 );

Hey, that's cheating. You say "50 char. max" but read only 49...
Our QA wouldn't let it through.
sTransfer = setName;
sName[ 0 ] = sTransfer;

What's this game for, I wonder. Couldn't you just copy the 'setName'
address to 'sName[0]'?
.....
........

What do you mean "is being skipped"? Could it be that your stream
is in no good condition? Check it

if (cin.good())
cout << "good\n";
else
cout << "no good\n";

If it is no good, no reading will happen.
I'm using VC++6.0, and I have applied the fixes, but nothings changed.

I don't know if something is wrong with the compiler.......

Nah, something is usually wrong with the programmer.

V
 
J

James Moughan

Alex said:
here is the code i am having trouble with.
// first, I have a global variable
char *sName[ 1 ];
...
....
// and then this function
void set_characterNameAgeSex()
{
char setName[ 50 ];
char *sTransfer;
short setSex;
short setAge;

// set name
cout << "Enter a name for your character(50 char. max): ";
cin.getline( setName, 49 );

sTransfer = setName;
sName[ 0 ] = sTransfer;

Hmm, what's the point of this? Not to get rid of compiler warnings by
any chance?
.....
........

Hmm, unlikely. Sounds to me like you're doing something like calling
set_characterNameAgeSex() and then checking your global pointer sName
to try to retrieve the data. By this point the array setName is out
of scope so this probably isn't working for you, as the pointer sName
is pointing to something which doesn't exist. Try returning a copy of
the data instead. And try using std::string, as it makes your life
easier; (untested code)

#include <string>

std::string set_characterNameAgeSex()
{
std::string setName;
short setSex;
short setAge;

// set name
cout << "Enter a name for your character(As many characters as you
like!): ";
cin.getline( setName );
return setName;

If you need to return multiple values you might consider using a
struct or class

struct character{
character(std::string n, short a, short s){
name = n;
sex = s;
age = a;
}
std::string name;
short sex;
short age;
};

and then in the method do
character set_characterNameAgeSex(){
.....

character new_char(setName, setAge, setSex);
return new_char;

}

if you then call

character a_char = set_characterNameAgeSex();
std::string some_name = a_char.name;

you can retrieve the values. Keeping them bound up in the struct also
helps keep your program simple. If you want to be adventurous, learn
about classes...

I'm using VC++6.0, and I have applied the fixes, but nothings changed.

I don't know if something is wrong with the compiler.......

There is, but I don't believe this is it. :)
 
A

Alex

ummmm.....could you be a little more polite? I'm a beginner.

Anyways, theres nothing wrong with:

cin.getline( setName, 49 );

yeah, its 50, but I use 49, to leave space for \n.

I use char *sName[ 1 ] so that sName[ 0 ] holds 1 string

sTransfer = setName;
sName[ 0 ] = sTransfer;

Can't do that. char[50] to char *[1] is not allowed.



Victor Bazarov said:
Alex said:
here is the code i am having trouble with.
// first, I have a global variable
char *sName[ 1 ];

Why would you ever need an array of length 1, honestly?
...
....
// and then this function
void set_characterNameAgeSex()
{
char setName[ 50 ];
char *sTransfer;
short setSex;
short setAge;

// set name
cout << "Enter a name for your character(50 char. max): ";
cin.getline( setName, 49 );

Hey, that's cheating. You say "50 char. max" but read only 49...
Our QA wouldn't let it through.
sTransfer = setName;
sName[ 0 ] = sTransfer;

What's this game for, I wonder. Couldn't you just copy the 'setName'
address to 'sName[0]'?
.....
........

What do you mean "is being skipped"? Could it be that your stream
is in no good condition? Check it

if (cin.good())
cout << "good\n";
else
cout << "no good\n";

If it is no good, no reading will happen.
I'm using VC++6.0, and I have applied the fixes, but nothings changed.

I don't know if something is wrong with the compiler.......

Nah, something is usually wrong with the programmer.

V
 
V

Victor Bazarov

Alex said:
ummmm.....could you be a little more polite?

I could. Do you think you deserve it?
I'm a beginner.

So? Have you read the FAQ? I bet you didn't. Start
there. Once you conquered all of it, come back and ask
more questions.
 
V

Victor Bazarov

X-No-archive: yes
Alex said:
FAQ or no FAQ, beginner or not, I think you should be polite either way.

Nice of you to share with us what you think. Do you want to
know what I think? 'fraid not. So, I won't tell you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top