Thread: setting the width

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    setting the width

    Code:
    #include <iostream>
    using namespace std;
    
    void  initialize(double [], int);
    void  process   (double [], int);
    void  format              (void);
    void  display   (double [], int);
    
    int main()
    {
    	double array[5];
    
    	cout << "Enter five numbers\n" << endl;
    
    	initialize(array, 5);
    
    	process(array, 5);
    
    	format();
    
    	display(array, 5);
    
    	return 0;
    }
    
    void  initialize(double array[], int size)
    {
    	for (register int i = 0; i < size; i++)
    	cin >> array[i];
    }
    
    void  process   (double array[], int size)
    {
    	for (register int i = 0; i < size; i++)
    		array[i] = array[i] * 2;
    }
    
    void  format                        (void)
    {
    	cout.precision(2);
    
    	cout.width(10);
    
    	cout.setf(ios::fixed | ios:: showpoint);
    }
    
    void  display   (double array[], int size)
    {
    	cout << "\nThe numbers doubled are..." << endl;
    
    	for (register int i = 0; i < size; i++)
    		cout << array[i] << endl;
    }
    Why doesn't cout.width(10) work?

    The only way I can get it to work is to change the cout statement in the display function's definition to cout << setw(10) << array[i] << endl;

    I don't want to use that way since it defeats the purpose of having a formatting function, and the setw method requires that I add #include <iomanip>

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    AFAIK, setw and width only affect the next piece of data sent to an ostream.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    I don't want to use that way since it defeats the purpose of having a formatting function
    You could pass the formatting function to display as a function pointer and then use it that way.
    Code:
    void  display(double array[], int size, void (*fmt)())
    {
        cout << "\nThe numbers doubled are..." << endl;
        for (register int i = 0; i < size; i++)
        {
            fmt();  
            cout <<"|"<<array[i] <<"|"<< endl;
        }
    }
    Code:
    display(array, 5, format);
    p.s. What the alphabet would look like without q and r.

  4. #4
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Is there a way to do this without pointers?

  5. #5
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    Is there a way to do this without pointers?
    Yes, hard code the formatting in your display function. Just for my own curiosity, why is everyone so freaked about using pointers?
    p.s. What the alphabet would look like without q and r.

  6. #6
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Pointers are scary.

    I think I saw one stalking me in the streets the other night. *shivers*


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  2. rescaling areas
    By hannibar in forum C Programming
    Replies: 2
    Last Post: 02-28-2006, 03:22 PM
  3. Display list not displaying?
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 09-19-2004, 06:47 PM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM