小白求助,c语言问题

y叶罗 2017-03-15 11:02:15
#include <stdio.h>
#include <string.h>
char * s_gets(char * st, int n);
#define MAXTITL 40
#define PHONE 20
#define NUM 50

struct people {
char name[MAXTITL];
int phone[PHONE];
};
int main()
{
struct people list[NUM];
int count = 0;
int index;

printf("please enter the name:");
while (count < MAXTITL && s_gets(list[count].name , MAXTITL) != NULL && list[count].name[0] != '\0')
{
printf("now please enter the phone number:");
s_gets(list[count].phone , PHONE);
while (getchar() != '\n')
continue;
if (count < NUM)
printf("enter the next title.\n");
}
if (count < 0)
{
printf("here is the list of yours:\n");
for (index = 0; index < count; index++)
printf("%s : %d", list[index].name, list[index].phone);
}
else
printf("no man, no number!");

return 0;
}

char *s_gets(char * st, int n)
{
char * ret_val;
char * find;

ret_val = fgets(st, n, stdin);
if (ret_val)
{
find = strchr(st, '\n');
if (find)
*find = '\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}

[Error] cannot convert 'int*' to 'char*' for argument '1' to 'char* s_gets(char*, int)'

初学c语言 想了好久 不知道是那里出了问题
...全文
179 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
自信男孩 2017-03-16
  • 打赏
  • 举报
回复
phone的类型是什么,要根据你的需求。如果这个phone是代表手机名(厂商),建议定成char phone[PHONE]类型;
#include <stdio.h>
#include <string.h>


#define MAXTITL 40
#define PHONE   20
#define NUM     50

struct people {
    char name[MAXTITL];
    char phone[PHONE];
};

char * s_gets(char * st, int n);

int main()
{
    struct people list[NUM];
    int count = 0;
    int index;

    printf("please enter the name:");
    while (count < MAXTITL && s_gets(list[count].name , MAXTITL) != NULL && list[count].name[0] != '\0')
    {
            printf("now please enter the phone number:");
            s_gets(list[count].phone  , PHONE);
            while (getchar() != '\n')
                continue;
            if (count < NUM)
                printf("enter the next title.\n");
        }
    if (count < 0)
    {
            printf("here is the list of yours:\n");
            for (index = 0; index < count; index++)
                printf("%s    :  %s", list[index].name, list[index].phone);
        }
    else
        printf("no man, no number!");

    return 0;
 }

 char *s_gets(char * st, int n)
     {
          char * ret_val;
          char * find;

          ret_val = fgets(st, n, stdin);
          if (ret_val)
          {
                   find = strchr(st, '\n');
                   if (find)
                      *find = '\0';
                   else
                      while (getchar() != '\n')
                          continue;
               }
          return ret_val;
      }
你我的约定 2017-03-16
  • 打赏
  • 举报
回复
phone是int类型,s_gets的参数要求的是char*类型
幻夢之葉 2017-03-16
  • 打赏
  • 举报
回复
phone是int类型,你s_gets的第一个参数要求的是char*类型 解决方式: 1:把phone修改为char[]类型 2:定义一个临时字符数组,读取char*的数据,然后再转化为你的phone
棉猴 2017-03-15
  • 打赏
  • 举报
回复
问题出在 s_gets(list[count].phone  , PHONE); 中list[count].phone的定义是int的指针,而s_gets函数的第一个参数需要的是char的指针,所以会报错

70,024

社区成员

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

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