1

我是 C 新手,我正在努力创建典型的初学者电话簿应用程序。我在谷歌上看到了很多不同的链接,这些链接显示了电话簿应用程序的示例,但没有显示我想要完成的任务。基本上我想要做的是创建一个具有名字、姓氏和 2 个电话号码的结构,然后我想为每个创建 2 个不同的函数。对于结构中的每个变量,我都会有一个设置输入,然后另一个读取输入。这给了我 8 个函数,但是我想再添加三个函数,一个用于向结构中添加 11each 新合同,一个用于删除联系人,然后一个用于查看电话簿。现在我专注于添加功能和查看功能。我的问题是我得到了非常奇怪的输出。

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

typedef struct friends_contact{
       char First_Name[10];
       char Last_Name[10];
       char home[15];
       char cell[15];

}fr;

void menu(fr*friends ,int* counter,int user_entry, char* nullStr,int i);
void setFirst(fr*,int *,int i);
char getFirst(fr*,int*,char*,int i);
void setLast(fr*friends, int* counter, int i);
char getLast(fr*friends ,int* counter,char *nullStr, int i);
void add_contact(fr*friends,int* counter,int i);
void show_contact(fr*friends ,int* counter,char *nullStr, int i);


int main()
{
  int user_entry=0;
  fr friends[5];
  int counter=0;
  char nullStr[21] = {"\0"}; 
  int i=0;

 menu(friends, &counter,user_entry,nullStr,i);
 getch();
 return 0;
}

void menu(fr*friends,int* counter,int user_entry,char *nullStr,int i)
{
  do{
  printf("Phone Book Application\n");
  printf("1) Add friend\n2) Delete friend\n3) Show a friend\n4) Show phonebook\n5)Exit);   
 scanf("%d", &user_entry);
  if(user_entry==1){

                    add_contact(friends,counter,i);
                    }
   if(user_entry==4){
                  show_contact(friends, counter,nullStr,i);
                   } 
   }while(user_entry!=5);                 
 }

 void setFirst(fr*friends, int* counter, int i)
{
 (*counter)++;
 printf("Enter a first name \n");
 scanf("%s",&friends[*counter-1].First_Name);
}
void setLast(fr*friends, int* counter, int i)
{
 (*counter)++;
 printf("Enter a first name \n");
 scanf("%s",&friends[*counter-1].Last_Name);
}
char getFirst(fr*friends ,int* counter,char *nullStr, int i)
{
     for(i=0; i<*counter; i++)
     {
        if (strcmp(nullStr, friends[i].First_Name) != 0) 
        {
           printf("%s\n", friends[i].First_Name);
        }
     }

return *friends[i].First_Name;
}

char getLast(fr*friends ,int* counter,char *nullStr, int i)
{
     for(i=0; i<*counter; i++)
     {
        if (strcmp(nullStr, friends[i].Last_Name) != 0) 
        {
           printf("%s\n", friends[i].Last_Name);
        }
     }

 return *friends[i].Last_Name;
}
void add_contact(fr*friends,int* counter,int i)
{ 
 setFirst(friends,counter,i); 
 setLast(friends,counter,i);
}    

void show_contact(fr*friends ,int* counter,char *nullStr, int i)
{
 getFirst(friends,counter,nullStr,i);
 getLast(friends,counter,nullStr,i);
}

这是我现在得到的输出:

Enter a first name
john
Enter a first name
smith
Phone Book Application
1) Add friend
2) Delete friend
3) Show a friend
4) Show phone book
5) Exit

4
john
╝ ♠

smith

当我在列表中添加另一个名称时,我的结果同样奇怪。奇怪的是,我在创建姓氏函数后才开始遇到这些问题。当我只输入名字时,它工作得很好。有任何想法吗?

4

5 回答 5

0

你的问题是:

1) 在函数 setFirstscanf("%s",&friends[*counter-1].First_Name);
中 2) 在函数 setLast 中 scanf("%s",&friends[*counter-1].Last_Name);

在这两种情况下,当您对字符串变量使用 scanf 时,您不应该将 '&' 放在变量前面,因为字符串变量已经是字符串第一个元素的地址。

这工作:

1) 在函数 setFirstscanf("%s",friends[*counter-1].First_Name);
中 2) 在函数 setLast 中 scanf("%s",friends[*counter-1].Last_Name);

于 2012-11-02T19:12:25.713 回答
0

看起来问题在于两者setFirstsetLast在递增计数器。因此,第一个条目填写名字,第二个条目填写姓氏。

于 2012-11-02T19:12:44.687 回答
0

您的错误输出是因为当您调用函数时showContacts,它首先调用函数getFirst,然后调用函数getLast,当您添加名称时john smithjane doe函数getFirst将打印john jane和函数getLast打印smith doe 为了解决这个问题,您应该执行以下操作:

void show_contact(fr*friends,int* counter,char *nullStr,int i){

for(i = 0 : *counter)

if (strcmp(nullStr, friends[i].First_Name) != 0)

// 这里你应该调用函数getFirstgetLast

}

你应该修改getFirst并只打印函数中找到getLast的位置的名字/姓氏ishowContact

于 2012-11-02T20:00:13.457 回答
0

下面的代码解决了你所有的问题:

void show_contact(fr*friends ,int* counter,char *nullStr, int i) {

for( i = 0; i < *counter; i++)

  if (strcmp(nullStr, friends[i].First_Name) != 0){
    getFirst(friends, i);
    getLast(friends, i);
  }

}

char getFirst(fr*friends , int pos) {

      printf("%s\n", friends[pos].First_Name);

      return *friends[pos].First_Name;

}

char getLast(fr*friends , int pos) {

      printf("%s\n", friends[pos].Last_Name);

      return *friends[pos].Last_Name;

}

于 2012-11-02T20:29:23.070 回答
0

好的,我做了一些更改,然后将我去掉了变量 nullStr 的整个代码(包括主代码)粘贴给你。当您想查看名称是否为 balnk 时,您可以使用 strlen(string) 它为您提供该字符串的大小。如果大小为 0,则表示字符串为空白。

这是代码:

#包含<stdio.h> # 包含<string.h>

typedef struct friends_contact{

  char First_Name[10];
   char Last_Name[10];
   char home[15];
   char cell[15];

}f;

void menu(fr*friends ,int* counter,int user_entry,int i);
void setFirst(fr*,int *,int i);
char getFirst(fr*,int i);
void setLast(fr*friends, int* counter, int i);
char getLast(fr*friends , int i);
void add_contact(fr*friends,int* counter,int i);
void show_contact(fr*friends ,int* counter, int i);

int main() {

int user_entry=0;
fr friends[5];
int counter=0;
int i=0;
menu(friends, &counter,user_entry,i);
return 0;

}

无效菜单(fr *朋友,int *计数器,int user_entry,int i){

做{

printf("Phone Book Application\n");
printf("1) Add friend\n2) Delete friend\n3) Show a friend\n
               4) Show phonebook\n5)Exit");   

scanf("%d", &user_entry);

if(user_entry==1){
            add_contact(friends,counter,i);
            }
   if(user_entry==4){
          show_contact(friends, counter,i);
           } 
   }while(user_entry!=5);                 

}

void setFirst(fr*friends, int* counter, int i) {

    printf("Enter a first name \n");
    scanf("%s",friends[*counter].First_Name);

}

void setLast(fr*friends, int* counter, int i) {

    printf("Enter a first name \n");
    scanf("%s",friends[*counter].Last_Name);

}

void add_contact(fr*friends,int* counter,int i) {

    setFirst(friends,counter,i); 
    setLast(friends,counter,i);
    (*counter)++;

}

void show_contact(fr*friends,int* counter, int i) {

for( i = 0; i < *counter; i++)
  if (strlen(friends[i].First_Name) && strlen(friends[i].Last_Name) )
    {
            getFirst(friends, i);
            getLast(friends, i);
    }

}

char getFirst(fr*friends , int pos) {

   printf("%s ", friends[pos].First_Name);
   return *friends[pos].First_Name;

}

char getLast(fr*friends , int pos) {

      printf("%s\n", friends[pos].Last_Name);
      return *friends[pos].Last_Name;

}

于 2012-11-02T21:49:23.203 回答