return 0, before the code is finished ?

Started by John1, April 20, 2020, 07:26:33 PM

Previous topic - Next topic

John1

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 ?

#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;
}

John Z

Hi,

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

Regards

John1

okay, I will think about it.

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

Thanks

John Z

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

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