首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将值添加到结构中,在C中添加错误

将值添加到结构中,在C中添加错误
EN

Stack Overflow用户
提问于 2019-02-20 02:21:06
回答 2查看 98关注 0票数 0

我是编程新手,所以我正在试着写一个小程序,我可以显示汽车信息,也可以将汽车添加到我的“库”中,现在我的out come for 1。显示汽车如下所示:

代码语言:javascript
复制
  ID          BRAND         PICS 
  bbb188     BMW    1   2   3
  AAA-999     VOLVO    4   5   6
  CCC-999     CITROEN    1   2   3

但是在我添加了一辆新车之后,PICS没有显示。因此,如果我要添加AAA-111沃尔沃1。这是结果:

代码语言:javascript
复制
 bbb188     BMW    1   2   3
 AAA-999     VOLVO    4   5   6
 CCC-999     CITROEN    1   2   3
 AAA-111     VOLVO    -398253632   3   3

我只是得到了图片的随机数,总是有3个值。有没有人能帮我做这个,请教我怎么做。

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#define MAX 1000
#define IDSIZE 20
#define BRANDSIZE 50
#define PICSIZE 10

typedef struct{
  char id[IDSIZE+1];
  char brand[BRANDSIZE+1];
  int *pic;
} Car;


void printCar(Car *pCar,int imagecount)
{
  printf(" %s ",pCar->id);
  printf("    %s ",pCar->brand);
  for(int i=0; i<imagecount; i++){
    printf("   %d",pCar->pic[i]);
  }
  printf("\n");
}

Car initCar(char itsId[],char itsBrand[],int *itsPic, int imagecount)
{
  Car newCar;
  strcpy(newCar.id, itsId);
  strcpy(newCar.brand, itsBrand);
  newCar.pic = itsPic;

  return newCar;
}

void PrintList(Car aLista[],int imagecount, int carcount)
{
  for(int i = 0; i<imagecount; i++)
    printCar(&aLista[i],carcount);
}

void AddCar(Car aList[], int *pAt, Car theCar) 
{
  aList[(*pAt)++]=theCar;    
}

Car NewCar(Car minapatienter[], int patientCount)
{
  Car newCar;

  gets(newCar.id);
  printf("type in  ID \n"); 
  gets(newCar.id); 
  printf("type in brand\n");
  gets(newCar.brand);

  bool imageInputDone = false; 
  int imageCount=0;
  while(imageInputDone == false)
  {
    printf("type in image reference \n");
    int newImage; 
    scanf("%d",&newImage);

    newCar.pic = &newImage; 
    imageCount++;
    printf("vill du \n1.Add another image reference \n2.exit\n");
    int input;
    scanf("%d", &input);
    printf("input: %i\n",input);
    switch(input)
    {
        case 1: 
            printf("Adding one more image\n");
            break;
        case 2: 
            printf("Leaving loop\n");
            imageInputDone = true; 
            break;
        default:
            while (input<1 || input<2)
              ;
            printf("Input correct number\n");
            break;
    }

    return newCar; 
  }
}

int main(void)
{
  int carCount=0;
  int imagecount=0;
  Car myCar[MAX]; 
  int input;

  int test[3]={1,2,3};
  int test2[3]={4,5,6};

  myCar[0]= initCar("bbb188","BMW", test, 3);
  myCar[1] = initCar("AAA-999","VOLVO", test2, 3);
  myCar[2] = initCar("CCC-999", "CITROEN", test,3);

  carCount=3;
  imagecount=3;

  do {
    printf("1. Show cars \n2. Add car \n");
    scanf("%d",&input);
    switch(input)
    {
      case 1:
        printf("ID          BRAND         PICS \n");
        PrintList(myCar,carCount, imagecount);
        break; 
      case 2: 
        AddCar(myCar,&carCount,NewCar(myCar,carCount));
        printf("ID          BRAND         PICS \n");
        PrintList(myCar,carCount, imagecount);
    }    //break; 
  } while (input < '1'|| input < '2');

  return 0;
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54772637

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档