Hello,
I am developing the project of "Employee Information System" using C language, In this project i want to add the mobile number for the each employee {Each Mobile number having 7 characters, Bcoz if we using the integer means, Any of one employee has no mobile number it’s not print the "0000000" format of mobile number} So if it okay means You will send the clear details about, "How to add the mobile number and also How to increase that to each employee "ASAP.
Contact Mail ID : [removed]

Thanks & Regards,
B.Karthikeyan

Dani AI

Generated

Summary of safe, practical options (building on suggestions from , , and ):

Store the 7-digit mobile as text so leading zeros survive (use an 8-byte buffer: seven digits + terminating NUL). To auto-assign the next number, either convert->increment->format (watch for overflow and bad input) or implement a simple digit-by-digit carry. The latter is small, fast, and avoids integer overflow or locale issues — it is what suggested in words and is implemented below.

/* Increment a 7-digit phone string in-place.
   phone must be an 8-byte buffer (7 chars + '\0').
   returns 0 on success, 1 if overflow ("9999999"), -1 on invalid input.
*/
int inc_phone7(char phone[8]) {
    for (int i = 6; i >= 0; --i) {
        if (phone[i] < '0' || phone[i] > '9') return -1;
        if (phone[i] != '9') { phone[i]++; return 0; }
        phone[i] = '0';
    }
    return 1;
}

Example usage pattern: keep the last-assigned number persistent (file or DB). When adding an employee, copy the last number into the new record, call inc_phone7() and save it back as the new last-assigned value. Keep the phone field as char phone[8] inside your struct Employee.

Troubleshooting and best practices:

  • Validate input digits with isdigit() before incrementing.
  • Decide policy for overflow ("9999999"): error, wrap, or expand format.
  • Don’t use "0000000" as the only indicator of “no phone” — use an extra flag (e.g., has_phone) or an empty string to avoid clash with a real number.
  • For multi-process/multi-user programs, persist the last-assigned value and use a lock or transactional DB update to avoid races.
  • If you prefer numeric conversion, use strtoull + snprintf with error checks (safer than atol), but remember to validate and handle overflow.

This keeps numbers fixed-width, preserves leading zeros, and gives predictable behavior when auto-incrementing.

Recommended Answers

All 11 Replies

Use char string[10] .

Use char string[10] .

thanks fore ur reply.
But i need the number are automatically incrementing that is If u enter Mobile no :1234567 for employee 1 na 2nd employee will directly get the next no that is 1234568
Reply me ASAP.

Regards,
B.Karthikeyan

It seems as if you have a solution in mind, why don't you try to implement it and if you have any trouble, post your code along with the question. (Something like "I expected it to do ___ but it is doing ___")

Oh, and when posting c code, please use c code tags
[code=c] /* Your code here */

[/code]

You can enter the first mobile number

scanf("%d",&mobileno[0];

rest all the numbers will be as

for(i=0;i<n;i++)
mobileno[i+1]=mobile[i]+1;

It seems as if you have a solution in mind, why don't you try to implement it and if you have any trouble, post your code along with the question. (Something like "I expected it to do ___ but it is doing ___")

Oh, and when posting c code, please use c code tags
[code=c] /* Your code here */

[/code]

HI,

I store employee's 7digit phone number as string. I want to allocate numbers to them For each employee the phone number should be 1 value greater than previous employee.
So, I need to increment ph number by 1.
How to increment a string?
Regards,
Karthikeyan.B

Please don't answer each post since your last with the same string, we all have to read through all of them.

I can't imagine any phone system where you can pre-order sequential 7 digit phone numbers, but ignoring that for now...

Why does the 7 digit phone number have to be a string?

Or if it does, why can't you convert it from a string to a number, add one and convert it back?

If you're REALLY desperate, you could convert the last character of the phone number to a number and add 1. If the number is now 10, put a '0' down for the last digit and 'carry' the one to the next-to-the last character. (At least that's how I learned to do manual addition when I was in school.)

commented: Very good, nice simple "string" addition, just like how it's done on paper. +27

Please don't answer each post since your last with the same string, we all have to read through all of them.

I can't imagine any phone system where you can pre-order sequential 7 digit phone numbers, but ignoring that for now...

Why does the 7 digit phone number have to be a string?

Or if it does, why can't you convert it from a string to a number, add one and convert it back?

If you're REALLY desperate, you could convert the last character of the phone number to a number and add 1. If the number is now 10, put a '0' down for the last digit and 'carry' the one to the next-to-the last character. (At least that's how I learned to do manual addition when I was in school.)

Hi,
Bcoz,1 employee is not having a mobile no means, output for that employee is 0000000 by using string,But if i am using integer means its not possible.That y i asked you.
Regards,
Karthikeyan.B

What about the rest of the post?

Why can't you convert from a string to a number, add one and convert it back?

And if an employee didn't have a mobile, I'd leave the field blank, not fill it with zeros.

What code have you tried until now??

It's quite simple, you take
char phone[] = "0000000";

Then write a function to do addition like you would on paper.
char phone[] = "0000001";

When you get to
char phone[] = "0000009";
the next one will be
char phone[] = "0000010";

Or just convert to int, add one, then convert back to string, as previously suggested

char phone[] = "0000000";
int ph = atol(phone)+1;
sprintf(phone,"%08d", ph);
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.