NO

Author Topic: return 0, before the code is finished ?  (Read 1433 times)

John1

  • Guest
return 0, before the code is finished ?
« on: April 20, 2020, 07:26:33 PM »
Hello,

It's for testing.

I have a perhaps little problem. The Program I execute does not finish all the code, but stops with return 0 after a loop.

Probably structs would be better, but nonetheless I tried arrays.

What could be the cause ?

Code: [Select]
#include <stdio.h>
#include <stdlib.h>

FILE *fp;
int anzahl[3];
double preisFeld[3];
char bezeichnung[3][30];

double summeRechnung(int *anz, double *pric, int z) {
 double r1;
 r1 =+ anz[z] * pric[z];
 //printf("Summe: %.2f \n", r1);
 return r1;
}

void datenEinlesen() {
 for(int i = 0; i < 3; i++) {
  printf("Eingabe von 3 Werten: \n");
  printf("Anzahl von Wert %d: \n", i+1);
  scanf("%d",&anzahl[i]);
  printf("Preis von Wert %d: \n", i+1);
  scanf("%lf", &preisFeld[i]);
  fflush(stdin);
  printf("Bezeichnung von Wert %d: \n", i+1);
  scanf("%s",bezeichnung[i]); // here it stops with return 0 ?
 }
 //for(int i = 0; i < 3; i++) {printf("Some content: %s \n", bezeichnung[i]);}
 datei_s(anzahl, preisFeld, bezeichnung);
}

void datei_s(int *a, double *p, char *b) {
 fp = fopen("datei.txt", "ab");
 if(fp = NULL) {
  printf("Dateifehler\n");
  return 1;
 }

 double r2;

 for(int index = 0; index < 3; index++) {
  r2 = summeRechnung(a, p, index);
  fprintf(fp, "%d\n", a[index]);
  fprintf(fp, "%8.2f\n", r2);
  fprintf(fp, "%c\n", b[index]);
 }
}

int main() {
 datenEinlesen();
 //datei_l();
 return 0;
}

Offline John Z

  • Member
  • *
  • Posts: 790
Re: return 0, before the code is finished ?
« Reply #1 on: April 20, 2020, 11:26:38 PM »
Hi,

Think about this statement:
 if(fp = NULL) {

Regards

John1

  • Guest
Re: return 0, before the code is finished ?
« Reply #2 on: May 05, 2020, 09:45:19 PM »
okay, I will think about it.

Sorry, the Problem is that it executes, but no content in the File.

Thanks

Offline John Z

  • Member
  • *
  • Posts: 790
Re: return 0, before the code is finished ?
« Reply #3 on: May 06, 2020, 02:29:45 AM »
I think the problem is you really meant to write

void datei_s(int *a, double *p, char *b) {
 fp = fopen("datei.txt", "ab");
 if(fp ==NULL) {
  printf("Dateifehler\n");
  return 1;
 }

Hope this helps.

Regards

John1

  • Guest
Re: return 0, before the code is finished ?
« Reply #4 on: May 11, 2020, 12:38:53 PM »
Hello JohnZ,

You are right, it is a syntax error, due the =.

Just now I have some Problems with the Content in the File, but it is another topic.

Thanks a lot

Kind regards