Thread: char array help

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    5

    Question char array help

    I am outputting five characters of a consumer name using a char array named consumerN. The actual array in some cases is longer than five letters, and in some cases shorter. If I use setw() it doesn't chop it off at five, I am assuming that that does not work because it is a string array. I am reading info from a file and outputting to another file line by line so it is in a while loop. So I set it to left align and output byte by byte the first five in the string:

    Code:
    report.setf(ios::left);
    
    report << setfill(' ') << consumerN[0] << consumerN[1] << consumerN[2] << consumerN[3] << consumerN[4];
    The only problem with this is that it outputs to the report the '\0' null byte (looks like a square) if the consumerN length is 4, and if it is less than four it outputs the fifth byte from the previous consumerN. Is there an easy way to fix this??? Can I clear the consumerN after each output so as to erase as the example, the fifth byte from the previous consumerN, even if I did that though, it would not solve the output of the null byte. I do not want to do a system clear because I have other information that I need to continue storing in variables. And how do I avoid writing the null byte to the report output file and just output a space.

    Thanks for any suggestions!!!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Dunno, but you might try the C function memset( void *, int setTo, int len)...

    Like memset(consumerN, 0, 5) // ...assuming 5 elements
    //..in the above case, only write to the first four!!
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  2. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  3. code help required
    By Yobbo in forum C Programming
    Replies: 9
    Last Post: 09-02-2005, 11:15 PM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM