一个小问题(函数的多次调用)

fengyefenfei 2008-10-23 10:04:02
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAX 20
char *g_pData=NULL;
void InsertChar(char data)
{

if(g_pData == NULL)
{
g_pData = (char*)malloc(2);
memset(g_pData, 0, 2);
*(g_pData+0) = data;
}
else
{
int nCurrentLen = strlen(g_pData);
char* pTemp = (char*)realloc(g_pData,nCurrentLen+2);
if(pTemp != NULL)
g_pData = pTemp;
g_pData[nCurrentLen] = data;
g_pData[nCurrentLen+1] = 0;
}

}
char* FreeInput()
{

char ch;
char szTemp[MAX];
memset(szTemp, 0, sizeof(szTemp));
int nCount=0;
while (1)
{
fflush(stdin);
while (1)
{
ch = getchar();
if (ch=='\n')
{
break;
}
else
{
InsertChar(ch);
}
}
if (g_pData==NULL)
{
free(g_pData);
g_pData=NULL;
printf("您的输入不能为空,请重新输入!\n");
continue;
}
else if(strlen(g_pData) > MAX)
{
free(g_pData);
g_pData=NULL;
printf("您的输入不正确,请重新输入!\n");
continue;
}
else
{
nCount=strlen(g_pData);
for (int i=0; i<nCount; i++)
{
szTemp[i]=g_pData[i];
}
free(g_pData);
g_pData=NULL;
break;
}
}
return szTemp;
}
上面的程序(在FreeInput.cpp中)是我写的一个用来处理用户输入的函数 下面的函数是主函数 他们不在同一个.cpp文件中 为何在主函数中输入的内容不能超过三个字符 我定义的是20个啊 请指点一下

#include "FreeInput.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAX_LENGTH 32


int main(int argc, char** argv)
{
//接收用户的输入
char *p_UserName=NULL;
char *p_PassWord=NULL;
char *p_Temp=NULL;
//分配空间并且初始化
p_UserName=(char *)malloc(MAX_LENGTH);
p_PassWord=(char *)malloc(MAX_LENGTH);
memset(p_UserName, 0, sizeof(p_UserName));
memset(p_PassWord, 0, sizeof(p_PassWord));

printf(">>请输入用户名:");
p_Temp=FreeInput();
for(int i=0; i<strlen(p_Temp); i++)
{
p_UserName[i]=p_Temp[i];
}
printf(">>请输入密码:");
p_Temp=NULL;
p_Temp=FreeInput();
for( i=0; i<strlen(p_Temp); i++)
{
p_PassWord[i]=p_Temp[i];
}
printf("%s", p_UserName);
printf("%s", p_PassWord);
free(p_UserName);
free(p_PassWord);

return 0;
}
...全文
162 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
fengyefenfei 2008-10-23
  • 打赏
  • 举报
回复
for(int i=0; i <strlen(p_Temp); i++)
{
p_UserName[i]=p_Temp[i];
}
printf(">>请输入密码:");
p_Temp=NULL;
p_Temp=FreeInput();
for(int i=0; i <strlen(p_Temp); i++)// i改成int i 这里改后,输出在20个字符内我的是没有问题。
{
p_PassWord[i]=p_Temp[i];
}
这两个for循环都有int 编译是会报错的 !!!我就是把int去掉 在前面定义这个变量int i=0;也不行啊 !
帅得不敢出门 2008-10-23
  • 打赏
  • 举报
回复
>>请输入用户名:jdljdldjlkdjl
>>请输入密码:3908493840830
jdljdldjlkdjl3908493840830
Press ENTER to continue.
帅得不敢出门 2008-10-23
  • 打赏
  • 举报
回复


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//#include <conio.h>
#define MAX 20
char *g_pData=NULL;
void InsertChar(char data)
{

if(g_pData == NULL)
{
g_pData = (char*)malloc(2);
memset(g_pData, 0, 2);
*(g_pData+0) = data;
}
else
{
int nCurrentLen = strlen(g_pData);
//printf("strlen(g_pData):%d",nCurrentLen);
char* pTemp = (char*)realloc(g_pData,nCurrentLen+2);
if(pTemp != NULL)
g_pData = pTemp;
g_pData[nCurrentLen] = data;
g_pData[nCurrentLen+1] = 0;
}

}
char* FreeInput()
{

char ch;
char szTemp[MAX];
memset(szTemp, 0, sizeof(szTemp));
int nCount=0;
while (1)
{
fflush(stdin);
while (1)
{
ch = getchar();
if (ch=='\n')
{
break;
}
else
{
InsertChar(ch);
}
}
if (g_pData==NULL)
{
free(g_pData);
g_pData=NULL;
printf("您的输入不能为空,请重新输入!\n");
continue;
}
else if(strlen(g_pData) > MAX)
{
free(g_pData);
g_pData=NULL;
printf("您的输入不正确,请重新输入!\n");
continue;
}
else
{
nCount=strlen(g_pData);
for (int i=0; i <nCount; i++)
{
szTemp[i]=g_pData[i];
}
free(g_pData);
g_pData=NULL;
break;
}
}
return szTemp;
}


#include "FreeInput.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAX_LENGTH 32
int main(int argc, char** argv)
{
//接收用户的输入
char *p_UserName=NULL;
char *p_PassWord=NULL;
char *p_Temp=NULL;
//分配空间并且初始化
p_UserName=(char *)malloc(MAX_LENGTH);
p_PassWord=(char *)malloc(MAX_LENGTH);
memset(p_UserName, 0, sizeof(p_UserName));
memset(p_PassWord, 0, sizeof(p_PassWord));

printf(">>请输入用户名:");
p_Temp=FreeInput();
for(int i=0; i <strlen(p_Temp); i++)
{
p_UserName[i]=p_Temp[i];
}
printf(">>请输入密码:");
p_Temp=NULL;
p_Temp=FreeInput();
for(int i=0; i <strlen(p_Temp); i++)// i改成int i 这里改后,输出在20个字符内我的是没有问题。
{
p_PassWord[i]=p_Temp[i];
}
printf("%s", p_UserName);
printf("%s", p_PassWord);
free(p_UserName);
free(p_PassWord);

return 0;
}

fengyefenfei 2008-10-23
  • 打赏
  • 举报
回复
我返回的数组的地址 这个程序可以运行 但是输入的用户名和密码都不能超过三个字符
ysuliu 2008-10-23
  • 打赏
  • 举报
回复
char szTemp[MAX];
这是个数组啊,函数结束就没了吧,应该用堆内存吧~

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧