首页 > 解决方案 > 为什么“代码”与“名称”相同?

问题描述

长颈鹿的名字和代码打印一次后由用户修改。我为每个输入不同的值,但“代码”与“名称”相同。

我相信我知道如何解决这个错误,但我想知道如何在不对代码进行太多修改或添加的情况下解决这个问题。我认为这是我对指针的使用,因为这是我第一次接触它们。

看一看:

typedef struct {

  int age;
  double height;
  char *name;

}giraffe;

void renameGiraffe(giraffe *g, char *na){

  g->name = na;

}

void recodeGiraffe(char * codes[], int n, char * code){

  codes[n] = code;

}


int main()
{
  giraffe north; giraffe south; giraffe west;
  north.name = "Fred";
  south.name = "Bill";
  west.name = "Kate";

  char in1[] = "";
  giraffe* exhibit[] = {&north, &south, &west};
  char* codes[] = {"GN","GS","GW"};

  for(int i = 0; i < (sizeof(exhibit)/sizeof(exhibit[0])); i++)
  {
    printf("The giraffe named %s has the code %s\n", exhibit[i]->name, codes[i]);
  }

  printf("Let's recode a giraffe. Which giraffe would you like to recode?\n\n");
  scanf("%s", in1);

  if(strcmp("north", in1)== 0)
  {
    printf("what is the new code for north?\n");
    scanf("%s", in1);
    recodeGiraffe(codes, 0, in1);
    printf("North has been recoded. The new code for north is %s\n", in1);
  }

  printf("Let's rename the north giraffe. What's the new name?\n");
  scanf("%s",in1);
  renameGiraffe(&north, in1);

printf("Reprinting the list of giraffes now:\n\n");

for(int i = 0; i < (sizeof(exhibit)/sizeof(exhibit[0])); i++)
  {
    printf("The giraffe named %s has the code %s\n", exhibit[i]->name, codes[i]);
  }

  return 0;
}

我的输出:

The giraffe named Fred has the code GN // Ignore the other two giraffes.
The giraffe named Bill has the code GS
The giraffe named Kate has the code GW
Let's recode a giraffe. Which giraffe would you like to recode?

north

What is the new code for north?

NOR

North has been recoded. The new code for north is NOR

Let's rename the north giraffe. What's the new name?

FREDDY

Reprinting the list of giraffes now:

The giraffe named FREDDY has the code FREDDY //The code should be NOR. 
The giraffe named Bill has the code GS
The giraffe named Kate has the code GW

标签: cpointers

解决方案


您正在读取新名称,in1然后将name作为指针的字段分配给指向in1. 然后,您将代码读入n1,这是该name字段所指向的内容,因此name已更改。

name与其使用and的指针,不如使用codes足够大的数组来保存任何预期的字符串,然后直接读入这些字符串。

typedef struct {

  int age;
  double height;
  char name[100];

}giraffe;

int main()
{
  giraffe north; giraffe south; giraffe west;
  strcpy(north.name, "Fred");
  strcpy(south.name, "Bill");
  strcpy(west.name, "Kate");

  char in1[100] = "";
  giraffe* exhibit[] = {&north, &south, &west};
  char codes[][100] = {"GN","GS","GW"};

  for(int i = 0; i < (sizeof(exhibit)/sizeof(exhibit[0])); i++)
  {
    printf("The giraffe named %s has the code %s\n", exhibit[i]->name, codes[i]);
  }

  printf("Let's recode a giraffe. Which giraffe would you like to recode?\n\n");
  scanf("%s", in1);

  if(strcmp("north", in1)== 0)
  {
    printf("what is the new code for north?\n");
    scanf("%s", codes[0]);
    printf("North has been recoded. The new code for north is %s\n", codes[0]));
  }

  printf("Let's rename the north giraffe. What's the new name?\n");
  scanf("%s",north.name);

  printf("Reprinting the list of giraffes now:\n\n");

  for(int i = 0; i < (sizeof(exhibit)/sizeof(exhibit[0])); i++)
  {
    printf("The giraffe named %s has the code %s\n", exhibit[i]->name, codes[i]);
  }

  return 0;
}

推荐阅读