Thread: Name

  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,057
    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,708
    post the code you're having trouble with.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  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,708
    >> 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?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.

  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,164
    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,057
    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