I want to have a variable that contains item number. int didn't work because i want to make the item number start with 3 or 4 zeros. if i use char * I can't increment the value of char?

any help will be appreciated

Recommended Answers

All 4 Replies

Padding integers with 0 to the left is a display thing only -- internally integers do not have leasing zeros. How to make them depends on how you are displaying them, such as are you using printf() or cout ?

int number = 12;
printf("%04d", number);

cout << setfill('0') << setw(4) << number << "\n";

now how can i store the data that this code show:

cout << setfill('0') << setw(4) << number << "\n";

or this code show

printf("%04d", number);

It would be good to store it into char * variable


Oh I see you want to store it in a character array with those leading 0's ? In c++ you would use stingstream class which works on strings just like fstream works on files

#include <sstream>
// other includes here
int main()
    int number = 12;
    string line;
    stringstream stream;
    stream << setfill('0') << setw(4) << number;
    stream >> line;
    cout << line << "\n";

    return 0;

thankss alot
this really solved my problem

Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.