🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #382

Re: HELP ME IF YOU CAN II

#382
From: gert
Date: Sat, 13 Feb 1999 00:00
196 lines
4690 bytes
On Wed, 10 Feb 1999 23:25:59 +0100, "Juraj Lenharcik"
<juraj.lenharcik@fen.baynet.de> wrote:

>ok,
>after some little (other) problems here is it now .
>Can you help me with the function listinsert?
>There is somewhere a problem but I dont`n know where.
>
>
>#include<stdio.h>
>#include<string.h>
>#include<stdlib.h>
>
>
>
>struct person
>{
> char vorname[80];
> char nachname[80];
> int einsaetze;
> double umsatz;
> double average;
> struct person *next;
>};
>
>
>
>void listcreate(struct person *dat, struct person *anf)
>{
> char vor[80], nach[80];
> double umsatz;
>    FILE *fhd;
> fhd=fopen("personal.txt","w");
>
> printf("Bitte geben Sie den Vornamen ein:\n");
> scanf("%s",vor);
> strcpy(dat->vorname,vor);
> fprintf(fhd,"%s",dat->vorname);
>
> printf("Bitte geben Sie den Nachnamen ein:\n");
>    scanf("%s",nach);
> strcpy(dat->nachname,nach);
>    fprintf(fhd," %s",dat->nachname);
>
> dat->einsaetze=1;
>    fprintf(fhd," %d",dat->einsaetze);
>
> printf("Bitte geben Sie den Umsatz ein:\n");
> scanf("%lf",&umsatz);
> dat->umsatz=umsatz;
> fprintf(fhd," %.2lf",dat->umsatz);
>
> dat->average=(dat->umsatz)/(dat->einsaetze);
> fprintf(fhd," %.2lf",dat->average);
>
> dat->next=NULL;
> fclose(fhd);
>}
>
>
>
>
>
>
>void listinsert(struct person *dat, struct person *anf)
>{
>    char vor[80], nach[80];
> double umsatz;
> FILE *fhd;
> fhd=fopen("personal.txt","r");
>
> if(!fhd)
> {
>  printf("Datei konnte nicht ge�ffnet werden!\n");
> }
> else
> {
>  do
>  {
>   fscanf(fhd,"%s %s %d %lf
>%lf",dat->vorname,dat->nachname,&dat->einsaetze,&dat->umsatz,&dat->average);


IMHO here is your problem:
>   dat=dat->next;
dat->next is not a valid pointer. You have to allocate memory for the
next element in your linked list first.
   

>  }while(!feof(fhd));
>
>  printf("Bitte den Vornamen eingeben:\n");
>  scanf("%s",vor);
>  strcpy(dat->vorname,vor);
>
>  printf("Bitte den Nachnamen eingeben:\n");
>  scanf("%s",dat->nachname);
>  strcpy(dat->nachname,nach);
>
>  printf("Bitte den Umsatz eingeben:\n");
>  scanf("%lf",&umsatz);
>  dat->umsatz=umsatz;
>
>  dat->einsaetze=1;
>
>  dat->average=(dat->umsatz)/(dat->einsaetze);
>  dat->next=NULL;
>
>  fclose(fhd);
>
>  FILE *fhd;
>  fhd=fopen("personal.txt","w");
>
>  if(!fhd)
>  {
>   printf("Datei konnte nicht ge�ffnet werden!\n");
>  }
>  else
>  {
>    dat=anf;
>     do
>     {
>      fprintf(fhd,"%s %s %d %.2lf
>%.2lf",dat->vorname,dat->nachname,&dat->einsaetze,&dat->umsatz,&dat->average
>);
>         dat=dat->next;
>     }while((dat->next)!=NULL);
>  }
>  fclose(fhd);
> }
>}
>
>
>void listshow(struct person *dat)
>{
> FILE *fhd;
> fhd=fopen("personal.txt","r");
>
> if(!fhd)
> {
>  printf("Datei konnte nicht ge�ffnet werden!\n");
> }
> else
> {
>  do
>  {
>   fscanf(fhd,"%s %s %d %lf
>%lf",dat->vorname,dat->nachname,&dat->einsaetze,&dat->umsatz,&dat->average);
>      printf("%s %s %d %.2lf
>%.2lf\n",dat->vorname,dat->nachname,dat->einsaetze,dat->umsatz,dat->average)
>;
>  }while(!feof(fhd));
> }
> fclose(fhd);
>}
>
>
>
>
>
>
>void main()
>{
>
> int entscheidung;
>    struct person *anf, *dat;
>
> dat= (struct person*)malloc(sizeof(struct person));
> anf=(struct person*)malloc(sizeof(struct person));
>
> anf=dat;
>
> printf("Bitte ausw�hlen:\n\n");
> printf("1 - Neue Liste erstellen\n");
> printf("2 - Element einf�gen\n");
> printf("3 - Liste zeigen\n");
> printf("0 - ENDE\n");
> printf("\n\n==>\n");
> scanf("%d",&entscheidung);
>
> switch(entscheidung)
> {
> case 1: listcreate(dat,anf);
>       break;
> case 2: dat=anf;
>      listinsert(dat,anf);
>      break;
> case 3: dat=anf;
>      listshow(dat);
>			break;
>	case 0: break;
>	}
>}
>
>
>
Technical Details
Message-ID: <36c53eb3.3905678@news.multiweb.nl>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <79t0g4$19k$1@freenet-b.fen.baynet.de>

Show Full Headers
Complete Article Headers
X-Google-Language: GERMAN,ASCII
X-Google-Thread: f8d38,a90a510e17090bf7
X-Google-Attributes: gidf8d38,public
X-Google-Thread: 114592,a90a510e17090bf7
X-Google-Attributes: gid114592,public
X-Google-Thread: 103c8d,a90a510e17090bf7
X-Google-Attributes: gid103c8d,public
X-Google-Thread: f7a51,a90a510e17090bf7
X-Google-Attributes: gidf7a51,public
From: gert@nospam.net(gert)
Subject: Re: HELP ME IF YOU CAN II
Date: 1999/02/13
Message-ID: <36c53eb3.3905678@news.multiweb.nl>
X-Deja-AN: 443929713
Content-Transfer-Encoding: 8bit
References: <79t0g4$19k$1@freenet-b.fen.baynet.de>
Content-Type: text/plain; charset=ISO-8859-1
Organization: News Service (http://www.news-service.com/)
Mime-Version: 1.0
Newsgroups: alt.comp.lang.learn.c-c++,borland.public.cpp.language,borland.public.cppbuilder.language,microsoft.public.vc.language