"cout" formating issue

My question is how can i get my final output std:: cout << std::hex << xx << yy << zz << endl; to print out zeroes before conversion cuz i can get like
d7b2 but i need 0d007b02 ??

i can use this : printf("%02x%04x%02x\n", xx, yy, zz);
but i want to use cout and only iostream lib so any help would be so nice

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
#include <iostream>
#include <vector>

using namespace std;
int sum_digits(int x) {
  int s = 0;
  while (x > 0) {
    s += x%10;
    x /= 10;
  }
  return s;
}
int main(int argc, char** argv) {
	int n;
	vector<int> d;
	cout << "Unesi broj pjesama za komprimiranje: " << endl;
	cin >> n;
	d.resize(n);
	
	for (int i=0; i<n; i++)
	{
		cin >> d[i];
	}
	int xx = 0;
  	int t0 = 2;
  	int yy = 0;
  	int zz = n;
  	for (int i=0; i<n; i++)
	{
    xx += sum_digits(t0);
    t0 += d[i];
    yy += d[i];
  	}
  	 xx %= 255;
  	std:: cout << std::hex << xx << yy << zz << endl; // problem
	return 0;
}


Tnx in advance guys ! :)
printf("%02x%04x%02x\n", xx, yy, zz);

==

1
2
3
std::cout << std::hex << std::setfill('0') << std::setw(2) << xx
                                           << std::setw(4) << yy
                                           << std::setw(2) << zz << "\n";
oh tnx mate, i couldn find how to fix it, so i just need to make fixed width and it automaticly adds zeroes , but i got this

setw is not a member of std, i think for setw i ned to use some lib like "iomanip" , how can i just do it with iostream, i cant figure it out??
Last edited on
Yes, you need #include <iomanip> as well as iostream.
but is there a way of doing it without iomanip, just with iostream, that was the question alla long, i maybe asked it so badley but :) ??
setw() is part of the iomanip library.
If you really wanted to do it without using setw(), you might try to manually add the correct number of spaces or something, but that is making things difficult.
can't you just do
std::cout << std::hex << "00" << xx << "0000" << yy << "00" << zz << std::endl;?

I don't see why you need setfill and setw if it is always the same..
u missed the whole point, if i set like u said, then it will always be 2 zeroes and my result, and 4 zeroes and my result, then again 2 zeroes and result, so that is how many digits, i can only have 8 digits used, and depending on result it can be lets say 0100b05
The real question is why this: "i want to use cout and only iostream lib"
Is this just a personal project or something set by a school or college, what is the reason for these particular restrictions?

restrictions are cuz the whole taks is based on learning basic stuff (as well as dec->hex), so i dont want this problem to be resolved with simple output formatting, and again writing a function that converts dec to hex is not an option...it is not a school task, this was one of ICT semi finals tasks for 8-th grade students
Still, <iomanip> is part of the basics isn't it? Certainly if you want to replicate the capabilities of printf() I would say it is.

Please don't get me wrong, I'm not trying to be awkward here, but coming up with a solution depends upon first understanding the requirements. Perhaps if you could post the original question it might help.
well in the paper given to kids it was written," u can only use iostream and its sublibs"

the task is not an issue, it is the stupidity of the task restrictions and the way it was given to kids, so the ones who solved the task like this one above or with printf, that works had 20 points less than others, but no official resolved taks was given to us (teachers) so i contacted the guy who organized and worked on tasks... no response. Now i am trying to see what could be done to get those 20 point.
> well in the paper given to kids it was written," u can only use iostream and its sublibs"

Manipulators are part of the C++ stream library; use std::cout, std::setfill() and std::setw(). What is the problem?


> there a way of doing it without iomanip, just with iostream, that was the question alla long

There is, obviously (manipulators are syntactic sugar):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

std::ostream& print_hex_to_stdout( int val, int width = 2 )
{
    std::cout.width(width) ;
    std::cout.fill('0') ;
    return std::cout << std::hex << val ;
}

int main()
{
    //0d007b02
    int xx = 13, yy = 123, zz = 2 ;
    print_hex_to_stdout(xx) ;
    print_hex_to_stdout(yy,4) ;
    print_hex_to_stdout(zz) << '\n' ;
}

http://ideone.com/z0CIjc
@JLBorges Thank you. I missed the point there.
i cant think that any kid figureod this out and knew hot to do it properly, so what's the point, to search documentation while competing in programming??

well tnx man :)
Searching documentation is part of a programmer's job, there's no shame in that. But the parts which are practised regularly get stuck in the memory of course.
i just dont see that good from teaching logic, it is semi final 8-th grade, so for them is to hard, and i bet nobody did it the way the judges wanted it...
dragcro wrote:
u missed the whole point, if i set like u said, then it will always be 2 zeroes and my result, and 4 zeroes and my result, then again 2 zeroes and result, so that is how many digits, i can only have 8 digits used, and depending on result it can be lets say 0100b05


If I missed the point then so did they? They had default values for the set fill aswell.

If you really don't want to use iomanip I guess you could use loops?
1
2
3
4
5
6
7
8
9
10
11
12
void output( int &hex , int width = 2  )
{
    for( int i = 0; i < width; ++i ) std::cout << 0 << std::flush;
    std::cout << std::hex << hex << std::flush;
}

int main()
{
    output( some_hex , 1 );
    output( some_hex  );
    output( some_hex , 1 );
}
this solution that u have presented seemes most appropriate , i really dont see the point of the task, but it should be resolved like this (loop), cuz they only learn loops at that age, and functions .

Topic archived. No new replies allowed.