Thread: truncate

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    2

    truncate

    I'm having trouble trying to truncate a character array. I tried to use cin.getline but that didn't work. I also tried to assign temp1 to temp2 but that also didn't work. Any suggestions would be greatly appreciated.
    Code:
    #include<iostream>
    #include<conio.h>
    #include<string>
    
    using namespace std;
    
    void main()
    {
    
    char temp1[26];
    char temp2[11];
    cout<<"Please enter a password no more than 25 characters in length: ";
    cin>>temp1;
    cout<<temp1;
    cin.getline(temp1, 11);
    //temp2=temp1;
    cout<<temp1<<endl;
    
    getch();
    }
    &#91;code]&#91;/code]tagged by Salem
    Shame about the lack of indentation though...

  2. #2
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    First, why include string if you aren't using it?

    Second, if you are bent on using character arrays, you can use:

    strncpy (char* dest, char* source, size_t n);

    (found in the cstring header)

    to copy n characters from source to dest, provided dest is big enough. The way you are implemnenting it is dangerous, as it won't properly copy the NUL terminator in most cases, so be careful and make sure you either get the terminator or do it yourself.

    If you want to use string objects, then you can use an istringstream to get istream-like functionality FROM a string variable, and then apply getline to it, to copy a number of characters out, which seem to be what you are trying to do with your code.

    Either solution should work, have a look and see if you have any more problems.
    Last edited by Imperito; 11-02-2002 at 06:46 PM.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Voila!

    Code:
     
     array[25] = 0;
    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
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Maybe im confused from your coding or you made a mistake.

    Code:
     
    cout<<"Please enter a password no more than 25 characters in length: ";
    cin>>temp1;
    cout<<temp1;
    cin.getline(temp1, 11);
    why would you possilby do cin>>temp1 and then do cin.getline when you already got input from cin. what are you trying to get from cin.getline. is it for the temp2 that you havent fixed yet?



    NOTE: i personally dont like using void main() but thats just me

  5. #5
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    NOTE: i personally dont like using void main() but thats just me
    It is not just you.

    void main is wrong.

    Not just bad, not just a poor practice, it is in fact quite entirely incorrect.

    That is not an opinion of mine or of anyone else, that is the Standard.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. truncate help
    By munna_dude in forum Linux Programming
    Replies: 3
    Last Post: 10-18-2007, 06:04 AM
  2. how can i truncate this
    By munna_dude in forum C Programming
    Replies: 11
    Last Post: 04-10-2007, 11:35 AM
  3. is there a way to truncate an array
    By panfilero in forum C Programming
    Replies: 1
    Last Post: 11-03-2005, 04:07 AM
  4. MYSQL++ truncate
    By LTLhawk in forum C++ Programming
    Replies: 2
    Last Post: 08-30-2003, 06:56 PM
  5. How to truncate URL string to filename?
    By theLastSlacker in forum C++ Programming
    Replies: 6
    Last Post: 09-11-2002, 12:00 AM