开发者

copied string converted via atoi delivers 0

开发者 https://www.devze.com 2023-03-28 10:21 出处:网络
this tool should, if it\'s ready, figure out the age of a person. it get the systemdate and ask the user for a birthdate. then it split the entered string into \"year\" (bjahr), \"month\" (bmonat) and

this tool should, if it's ready, figure out the age of a person. it get the systemdate and ask the user for a birthdate. then it split the entered string into "year" (bjahr), "month" (bmonat) and "day" (btag) by copy the important characters into a 开发者_运维百科new string. after this it converts them via atoi to an int value.

to check wheather everything went fine, i let print it. But there the Problem starts. the year works fine, but "intmonat" and "inttag" seems to be 0. I can't find the mistake, can u help me please?

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#include <string.h>

int main ()
{
    SYSTEMTIME time;
    GetSystemTime (&time);

char name[20], bday[10], bjahr[4], bmonat[2], btag[2]; 

int year = time.wYear;
int month = time.wMonth;
int day = time.wDay;
int intjahr, intmonat, inttag;

printf("\n\n today is the: %i.%i.%i \n\n",day,month,year);
printf(" please insert Birthdate (dd.mm.jjjj).\n\n");

gets(bday);

strncpy(bjahr , &bday[6], 5);
strncpy(btag  , &bday[0], 1);
strncpy(bmonat, &bday[3], 1);

intjahr  = atoi(bjahr) ;
intmonat = atoi(bmonat);
inttag   = atoi(btag)  ;

printf("\n\n jahr %i \n\n",intjahr);
printf(" monat %i \n\n",intmonat);
printf(" tag %i \n\n",inttag);        

system("PAUSE");

}

I'm german, that's why some words could be not english hope it doesn't matter.


Your variables are too short: for example, you expect two characters for the month, but you also need to account to for \0 that terminates the string. Also, you're just copying one character per day and month and not null terminating it. So it should be:

char name[20], bday[11], bjahr[5], bmonat[3], btag[3];

...

gets(bday);

strncpy(bjahr , &bday[6], 4);
bjahr[4] = 0;
strncpy(btag  , &bday[0], 2);
btag[2] = 0;
strncpy(bmonat, &bday[3], 2);
bday[2] = 0;

But a way better solution would be to use scanf instead, you should read about it and get familiar with it. It can easily help you to correctly parse input like 1.2.2000, for example.

Also, your current solution with fixed sized arrays can easily yield a buffer overflow (just enter abcdefghijklmnopqrst). You should do this instead:

fgets(bday, sizeof(bday), stdin);


your strncpy's are slightly off, you want this rather, so that you capture the correct amount of digits:

strncpy(bjahr , &bday[6], 4);
strncpy(btag  , &bday[0], 2);
strncpy(bmonat, &bday[3], 2);

also, your buffers should be a bit larger to accommodated the NULL terminator that atoi requires (I align then to multiples of four just for better matching with what the compiles stack allocator will do), which you can add in yourself, or get by pre-zeroing the buffers:

char name[20] = {0}, bday[10] = {0}, bjahr[8] = {0}, bmonat[4] = {0}, btag[4] = {0};

however, you'd do better using something like scanf of sscanf

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号