0

I am currently trying to make my program so that it has the file name as the persons name + their date of birth. The issue i am having is copying just the name into the array, as I currently have a loop where it loops 15 times. I am unsure of how I am able to limit the program to only loop the amount of times as there were input by the user, or to make the program stop when it reaches the end of the user input characters in the array.

#include <stdio.h>
#include <conio.h>
#include <string.h>

int main()
{
    char firstname[16], lastname[15], filename[23];
    int dob, i;

    printf("Please enter the first name of the player:");
    fgets(firstname, 15, stdin);
    printf("Please enter the date of birth of the player(ddmmyy): ");
    scanf("%6d", &dob);
    for(i = 0; i < 15; i++)
    {
        strncpy(filename[i], firstname[i], 15);
        if(lastname[i] == '\0');
        {
            break;
        }
    }
    for(i = 0; i < 6; i++)
    {
        strncpy(filename[i+14], dob, 6);
        if(dob[i] == '\0');
        {
            break;
        }
    }
    printf("%s", filename);
}
3
  • Compiler is telling me that passing the argument of strncpy makes pointer from integer Commented Dec 8, 2015 at 16:07
  • I get this error when trying to use it with firstname aswell Commented Dec 8, 2015 at 16:12
  • That did work for the first part, thanks! Just need to add the dob to filename now. Commented Dec 8, 2015 at 16:26

2 Answers 2

1

Here's a slightly simpler version of your code:

#include <stdio.h>
#include <conio.h>
#include <string.h>

int main()
{
    char firstname[16], lastname[15], filename[23];
    int dob, i;
    char *ptr;

    printf("Please enter the first name of the player:");
    fgets(firstname, sizeof(firstname), stdin);
    printf("Please enter the date of birth of the player(ddmmyy): ");
    scanf("%6d", &dob);

    ptr = firstname;
    while( *ptr != '\n' ) ++ptr;
    *ptr = '\0';

    sprintf(filename, "%s%06d", firstname, dob);

    printf("%s", filename);
}

Note: sprintf() is very powerful and can format the dob for you at the same time it copies firstname. (Note: sprintf() is likely to cause buffer over-runs if you're not extremely careful with it. Visual studio has a safer version sprintf_s())

As an alternative, you could do the following if you don't want to call any functions and would rather do the string copies manually:

int dob, i, n;
// ...

i = 0;
while( firstname[i] != '\n' && firstname[i] != '\0' )
{
   filename[i] = firstname[i];
   ++i;
}

for( n=100000; n>=1; n /= 10 )
{
   int digit = (dob / n) % 10;
   filename[i] = (char)('0' + digit);
   ++i;
}

if( i >= sizeof(filename) )
   return -1;
filename[i] = '\0';
Sign up to request clarification or add additional context in comments.

5 Comments

Been trying your first version and I am having a problem that i cannot call the char in the middle of code, and does not work when I have char ptr at the top of the function
@Thecube Modifying it now for C syntax, sorry I was using a C++ compiler.
Compiler crashes right after entering Date of birth
Managed to get it working but the combination of firstname and dob gets output 3 times
@Thecube Are you running just the first example or some combination of the two?
1

After reading your input, sanitize it as indicated by @Thecube, and ensure that each sting is null terminated.. Once the input is sanitize, copy firstname into filename.

You just need strncpy(filename, firstname, 15) to do this. You don't need to do it in a loop.

Next step is to add lastname. Use strncat (filename, lastname, 15)

2 Comments

Richard - enclose your code fragments in backticks like this, e.g.: You just need strncpy(filename, firstname, 15) to do this. It will really help the readability. (or you can put them on a separate line and indent by 4-spaces for code syntax.
Thanks for the advice, the first part is now fixed. Your second step is not what I am trying to do, instead of adding the first + lastname together, I am trying to add the dateofbirth after the firstname to save as the filename. So if i didn't make it clear, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.