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
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.
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.
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??
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...
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 .