Is there any way that I can format my output so that instead of outputting "1" it will output "0001"?
Is there any way that I can format my output so that instead of outputting "1" it will output "0001"?
Code:#include <iomanip> #include <iostream> using namespace std; int main() { int value = 1; // Should be "1" cout << value << endl; // Should be "0001" cout << setfill('0') << setw(4) << value << endl; // Alternate way to do above... don't need to use the iomanip header cout.fill('0'); cout.width(4); cout << value << endl; return 0; }
Last edited by hk_mp5kpdw; 05-19-2005 at 12:37 PM.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
this is the line of code that I have put it into:
still doesn't seem to be working out...Code:cout << setfill('0') << setw(4) << "\nID# for customer " << file[n].custname << " is " << file[n].custID << endl << endl;
Rearrange it a little.Code:cout << "\nID# for customer " << file[n].custname << " is " << setfill('0') << setw(4) << file[n].custID << endl << endl;
If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein
Try:
The effect of the setw and setfill command disappear after the custname gets output so everything returns to "normal" (formatting-wise) by the time you output the custID. You should put the calls to them just before you output the value you need to have formatted like I have done above.Code:cout << "\nID# for customer " << file[n].custname << " is " << setfill('0') << setw(4) << file[n].custID << endl << endl;
Last edited by hk_mp5kpdw; 05-19-2005 at 01:31 PM.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
I changed the line around to look like this now:
now it works like a charm, thnx for the helpCode:cout << "\nID# for customer " << setfill('0') << setw(4) << file[n].custname << " is " << file[n].custID << endl << endl;
whoops, wrong order there, that was an old iteration of it, I did get it to work though by just altering the order slightly, thnx again