How to use setw and setfill properly

First, this is a homework assignment. I am not trying to hide that. I also don't expect anyone to do my homework for me.

But I do need help understanding HOW this works.

We are random generating an array of 100 numbers.
Then we set it to display in 5 columns.
Then we set it to display in 5 right aligned columns using setw.

Now, this is where I get lost. We are supposed to set it to display a "----" in front of any number that is less than 100 and "++" in front of any number greater than 1000.
Ex: ----99 128 ++1024 ----88 ++1001 567

The only example in our book shows how to use setfill to put the desired character in front of every number.

I attempted to use "if/else" statements, but still ended up with every number displayed with '-' filling each unused space.

ANY HELP explaining this would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//*************************
//* Project Name: 
//* Program Name: 
//* Written By: 
//* Date: 
//*
//* This program displays 100 random numbers in a formatted table.
//*************************

//**Preprocessor Directives
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{							//-BEGIN MAIN()			
//*****Local Variables and Constants
	const int SIZE = 100;
	const int UPPER = 1024;
	const int LOWER = 8;
	int array[SIZE];
	int x;

//*****Program Statements

	// load the array with random numbers
	for (x = 0; x < SIZE; x++)
		array [x] = LOWER + rand() % (UPPER - LOWER + 1);
	
	// display array
	for (x = 0; x < 100; x++)
	{	cout << setw(5) << array[x] <<"  ";
		if (x % 5 == 4)
			cout << endl;
	}
	cout << endl;
	return 0;
}							//-END MAIN() 



You can use the conditional operator
eg:
cout << setw(5) << setfill( array[x] < 100 ? '-' : '+' ) << array[x];


http://www.cplusplus.com/doc/tutorial/operators.html
Bazzy, when I do that, I end up with '-' in front of the numbers less than 100 but a '+' in front of everything else. And there weren't columns anymore.

Our book only gives one example - displaying the desired character in every open space. Which is not what our assignment is.

The only logical thing to me is a if/else situation...
if (x < 100)
do this..
if (x > 1000)
do this
else...

But when I tried it, it continually failed, and I don't know if I am using incorrect syntax or what. Plus, I think it is too many steps for what he is asking for. "Use the fewest explicit text characters."


You can see the examples he gave us here:
http://www.fiercewebs.com/homework/bigarray/index.htm

Last edited on
I've missed a 0 when reading your question...
so
1
2
3
4
5
6
if (x < 100)
    cout << setfill('-');
else if (x > 1000)
    cout << setfill('+');
else
    cout << setfill(' '); // should just be a space if 100 ≤ x ≤ 1000 ? 
Is this what you are trying to have?
That seems like the logical way to do it.

But all I end up with is a five column array full of '----' in front of them.


As I mentioned the text only gives one example of how to use setw() and setfill().

1
2
3
4
5
6
7
x = 15
y = 7634

cout << setw(5) << setfill('@') << x
        << setw(7) << setfill('#') << y
        << setw(8) << setfill('^') << "Warm"
        << endl;



Which results in
@@@15###7634^^^^Warm

I'll be dipped if I know how that is supposed to translate into a formula that only uses setfill for SOME of the numbers and not others.
Just use the if conditions
1
2
3
4
if (You_Want_To_Use_Manipulators)
    cout << setw( n ) << setfill( c );
else cout << setw(0);
cout << x;
Finally got it...
Just a matter of proper placement and syntax. Geez I hate C++.

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

for (x = 0; x < 100; x++)
    {   
		cout << setfill(' ');

		if (array[x] < 100)
			cout << setfill('-');

		if (array[x] > 1000)
			cout << setfill('+');

		cout << setw(6) << array[x] << "  ";

		if (x % 5 == 4)
			cout << endl;
    }
Topic archived. No new replies allowed.