为什么这个工具不打印字符串?

发布于 2024-11-30 04:38:13 字数 1175 浏览 1 评论 0原文

这个工具是为了让我学习如何使用字符串。但如果完成,有时,它应该比较两个日期(系统日期和输入的生日日期)并告诉用户该人的年龄。但我被卡住了。我想首先尝试一些基础知识,因此输入的字符串被分成日、月和年,然后它应该打印字符串。 但我真正的目标是将它们转换为 int 值,以便我可以用它们进行计算。

但由于某些原因,该工具不打印输入的字符串,它只打印 3 \n 并且我无法找出问题所在。

请帮忙。

编辑:再次修复它几乎准备好了,只是“btag”造成了一些麻烦。 “bmonat”和“bjahr”现在工作正常,谢谢你!

/*age check (c) By Tim Hartmann*/


#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[3], btag[3]; 

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], 4);
    bjahr[4] =  '\0';

strncpy(bmonat, &bday[3], 2);
    bmonat[2]=  '\0';        

strncpy(btag  , &bday[0], 2); /* here is the probleme */
    btag[2]  =  '\0'; 

printf("\n %s \n",   bjahr);
printf("\n %s \n",  bmonat);
printf("\n %s \n",    btag);

system("PAUSE");

 }

this tool is for me to learn how to work with strings. but if it finished, sometimes, it should compare two dates (the systemdate and a entered birthday date) and tell the user how old the person is. but i get stuck. i wanted to try some basics at first, so the entered string gets split into day month and year and then it should print the strings.
But my real aim is, to convert them to an int value so that i can calculate with them.

But for some reasons the tool don't print the entered string, it just print 3 \n and i can't figure out the problem.

please help.

edit: fixed it again almost ready only the "btag" makes some some trouble. "bmonat" and "bjahr" works fine now thank u so far!

/*age check (c) By Tim Hartmann*/


#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[3], btag[3]; 

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], 4);
    bjahr[4] =  '\0';

strncpy(bmonat, &bday[3], 2);
    bmonat[2]=  '\0';        

strncpy(btag  , &bday[0], 2); /* here is the probleme */
    btag[2]  =  '\0'; 

printf("\n %s \n",   bjahr);
printf("\n %s \n",  bmonat);
printf("\n %s \n",    btag);

system("PAUSE");

 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

围归者 2024-12-07 04:38:13

这有一些问题。对于初学者来说,
bjahr[4] = 0;
bmonat[2]= 0;
btag[2] = 0;

这些都是试图访问超出缓冲区大小限制的内容。

另一件事是您可能想要

bjahr[4] =  '\0';
bmonat[2]=  '\0';
btag[2]  =  '\0';

相反,因为这会给您空字符而不是 0。

There are a few things wrong with this. For starters,
bjahr[4] = 0;
bmonat[2]= 0;
btag[2] = 0;

These are all trying to access things outside your size limitations on your buffer.

Another thing is that you are probably want

bjahr[4] =  '\0';
bmonat[2]=  '\0';
btag[2]  =  '\0';

Instead, as this gives you null characters instead of 0's.

请别遗忘我 2024-12-07 04:38:13

您声明的字符串(bday、bjahr、bmonat、btag)缺少 1 个字符来存储 \0

btag 例如应该是 btag[3],特别是因为您稍后要执行 btag[2] = 0在源代码中,

您可以尝试在所有字符串中再添加 1 个字符并检查它是否可以纠正所有内容吗?


它应该如下所示:

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

如果您想要名称最多 20 个字符,则为 10对于生日,...

然后,在提取日期时:

strncpy(btag, &bday[0], 2);
    btag[2] = '\0';

这样,您可以从 btag 中复制生日中的 2 个字符(从偏移量 0 开始),然后将第 3 个字符设置为 '\0'结束字符串

The strings you declare (bday, bjahr, bmonat, btag) lack 1 char to store the \0

btag for instance should be btag[3], particularly since you are doing btag[2] = 0 later in the source

Can you try adding 1 more char to all your strings and check if it corrects everything?


It should look like this:

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

if you want 20 chars max for the name, 10 for the bday, ...

then, when extracting the date:

strncpy(btag, &bday[0], 2);
    btag[2] = '\0';

This way, you copy 2 chars from bday in btag (starting at offset 0) and then you set the 3rd char to '\0' to end the string

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文