getline problem

Discussion in 'C++' started by Alex, Jun 5, 2004.

  1. Alex

    Alex Guest

    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.
     
    Alex, Jun 5, 2004
    #1
    1. Advertising

  2. "Alex" <> wrote...
    > 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]'?

    > .....
    > ........
    >
    > -------------------
    > So what's happening is that the line: cin.getline( setName, 49 ); is

    being
    > skipped....but the cout line before that is being printed.


    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
     
    Victor Bazarov, Jun 5, 2004
    #2
    1. Advertising

  3. "Alex" <> wrote in message news:<jMcwc.10585$%F2.8654@attbi_s04>...
    > 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?

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


    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. :)

    > Please help.
    > Thanks in advance.
     
    James Moughan, Jun 5, 2004
    #3
  4. Alex

    Alex Guest

    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" <> wrote in message
    news:Ggdwc.45710$3x.27014@attbi_s54...
    > "Alex" <> wrote...
    > > 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]'?
    >
    > > .....
    > > ........
    > >
    > > -------------------
    > > So what's happening is that the line: cin.getline( setName, 49 ); is

    > being
    > > skipped....but the cout line before that is being printed.

    >
    > 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
    >
    >
     
    Alex, Jun 5, 2004
    #4
  5. "Alex" <> wrote...
    > 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.
     
    Victor Bazarov, Jun 5, 2004
    #5
  6. Alex

    Alex Guest

    FAQ or no FAQ, beginner or not, I think you should be polite either way.

    "Victor Bazarov" <> wrote in message
    news:Tymwc.12026$%F2.1180@attbi_s04...
    > "Alex" <> wrote...
    > > 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.
    >
    >
     
    Alex, Jun 6, 2004
    #6
  7. X-No-archive: yes
    "Alex" <> wrote...
    > 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.
     
    Victor Bazarov, Jun 6, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John
    Replies:
    10
    Views:
    10,058
    Buster Copley
    Jul 14, 2003
  2. Poof
    Replies:
    1
    Views:
    385
  3. Joe
    Replies:
    4
    Views:
    7,755
    Chris Theis
    Jan 22, 2004
  4. solartimba

    getline problem

    solartimba, Apr 6, 2004, in forum: C++
    Replies:
    1
    Views:
    432
    Leor Zolman
    Apr 6, 2004
  5. bg

    problem with getline

    bg, Jun 26, 2004, in forum: C++
    Replies:
    1
    Views:
    513
    David Harmon
    Jun 26, 2004
Loading...

Share This Page