Name

This is a discussion on Name within the C++ Programming forums, part of the General Programming Boards category; Hi, i have a problem, i am writing the initials and the surname on one line of code, how do ...

  1. #1
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215

    Name

    Hi,

    i have a problem,

    i am writing the initials and the surname on one line of code, how do i copy just the initials and leave the surname, and put the initials on another line?

    Thanks

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Do you have the initials and surname in a string? Do you want to split the string?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    post the code you're having trouble with.



    ITSA
    Socket Library!

  4. #4
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    not got anything yet really. if i was to put it in a string how would i do that and then how would i spilt the string to do what i am wanting it do do?

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    >> not got anything yet really.

    me neither.

    >> if i was to put it in a string how would i do that and then how would i spilt the string to do what i am wanting it do do?

    why don't you give it a try first and see what you come up with? after all programming *is* about problem solving, right?



    ITSA
    Socket Library!

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > not got anything yet really
    Apparently, you have the code which prints both on the same line.
    How about you post that, then perhaps we can tell you how to change it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    hi,

    this is what i have
    Code:
            char szname[50];
    
            cout << "Please enter your initials and surname: ";
    
                    char str2[3];
            strncpy (str2,szname, 3);
            str2[3] = '\0';
            puts (str2);
            cout <<endl;
    
            cin.getline(szname, 20);
    it manages to copy the first 3 letter whiich i want it to do, but i also want it to copy the remiaining letters onto another line.

    eg.

    user enter - AN Other

    Output
    AN
    Other

  8. #8
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    can any one help with that?

  9. #9
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Quote Originally Posted by peckitt99
    Code:
    char szname[50];  // define variable, it's loaded with -- random values
                      // possibly not even characters
    cout << "Please enter your initials and surname: ";  // output a prompt
    
    char str2[3];  // create a char array of 3 characters
    strncpy (str2,szname, 3);  // copy the first 3 random characters from 1st into 2nd array
    str2[3] = '\0';  // set the 4th char of a 3 char array to null -- a byte of memory trashed
    puts (str2);  // Output the random characters
    cout <<endl;  // next line
    
    cin.getline(szname, 20);  // Now, finally, input the name from the keyboard.
    it manages to copy the first 3 letter whiich i want it to do, but i also want it to copy the remiaining letters onto another line.
    Not really.

    1) Move the cin.getline(szname, 20); immediately below the prompt and you have it.
    2) Fix definition of str2 -- make it 4, not 3
    3) Instead of puts(), use your cout
    4) The rest of the string is at the address (&) of the 4th character ([3]) in szname. Put that together and output it.

    [edit]And don't bump your threads -- it's not a good thing[/edit]
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  10. #10
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    managed to do most of that but i dont understand 4.

    4) The rest of the string is at the address (&) of the 4th character ([3]) in szname. Put that together and output it.

    what do you mean?

    Thanks for your help.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    4) The rest of the string is at the address (&) of the 4th character ([3]) in szname. Put that together and output it.

    what do you mean?
    The first element of an array is [0]. The last element is [elements-1]. So if you have an array declared like this:
    Code:
    int array[3];
    the addressable elements are [0] to [2]. [3] is out of bounds.

    If you take the address of array[1], like this:
    Code:
    &array[1]
    it's like you have another array that starts at array[1] (of course, it's the same array, occupying the same memory). So if you have
    Code:
    char s[] = "hello world";
    cout << &s[5];
    the output will be
    Code:
    world
    which is what you want (&szname[3]).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    Hi,

    Got it working thanks for all your help much appreciated!!

    Thanks!!!

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21