this the full code, which works like a charm, except for the error when i close the program. (a normal 'send report' / 'do not send report' error).
I've narrowed it down to one line (a variabele being read from a file -> posted in red), but can't seem to figure out what is wrong with it.

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;
// definieren van patientenvariabelen in structuur 
struct TIJDSTIP {
   int dag;
   int operatiekamer;
};
struct ALLE_PATIENTENDATA {
   int nummer;
   char naam[50];
   struct TIJDSTIP tijdstip ;
   int leeftijd;
   int discipline;
};
// definieren van blok-variabelen in structuur 
struct BLOK {
   int blok_dag;
   int ok_nummer;
   int blok_nummer;
   int blok_tijd;
};
 
//------------------------------------------------------------------------------
// de main functie
//------------------------------------------------------------------------------
 
int main(int argc, char *argv[])
{
   int count; 
   char antwoord;
   int aantal_patienten;
 
 
//------------------------------------------------------------------------------
//  de patientenstructuren creëren
//------------------------------------------------------------------------------
 
 
   //opent het bestand "patienten.txt" en kijkt nadien of dat gelukt is
   FILE *bestand_patienten = fopen("patienten.txt", "r");
   if (!(bestand_patienten = fopen ("patienten.txt", "r"))) 
      printf ("Het bestand patienten.txt kon niet geopend worden.");
   //aantal patienten uit bestand lezen en op scherm tonen
   fscanf (bestand_patienten, "%d", &aantal_patienten);
   printf ("Er werden %d patienten ingeschreven.\n\n", aantal_patienten);
   // creëren van patiënten variabelen
   struct ALLE_PATIENTENDATA patient[aantal_patienten];
 
//------------------------------------------------------------------------------
//  patientendata in variabelen steken en eventueel lijst opmaken
//------------------------------------------------------------------------------
 
   //de patientengegevens uit bestand in variabelen omzetten
   for (count = 1; count <= aantal_patienten; count++)
   {
       fscanf (bestand_patienten, "%d", &patient[count].nummer);
       fscanf (bestand_patienten, "%d", &patient[count].discipline);
       fscanf (bestand_patienten, "%d", &patient[count].leeftijd);
       fscanf (bestand_patienten, "%s", &patient[count].naam);
   }
   // Vragen of patientenlijst moet getoond worden
   printf ("Indien u de patientenlijst wilt zien, typ dan (j):\t");
   scanf ("%c", &antwoord);
   if (antwoord == 'j' || antwoord == 'J')
   {
      //patientenlijst tonen
      printf("\n\nNr.\tDisc.\tAge\tNaam\n");
      printf("--------------------------------------------------\n");
      for (count = 1; count <= aantal_patienten; count++)
      {
         printf ("%d\t", patient[count].nummer);
         // discipline in woorden weergeven
         if (patient[count].discipline == 1)
            printf ("hart\t");
         if (patient[count].discipline == 2)
            printf ("longen\t");
         printf ("%d\t", patient[count].leeftijd);
         printf ("%s\n", patient[count].naam);
      }
      fclose (bestand_patienten);
   }  
 
//------------------------------------------------------------------------------
//  einde programma -> output laten staan                                                                          
//------------------------------------------------------------------------------
 
   //zorgt ervoor dat de output blijft staan op het einde
   printf("\n\n");
   system("PAUSE");
   return (0);
}

Fot those who should want to test it: first run this little thingy (code below) which creates random patients (after asking how many) and puts them inside a txt file.

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <stdio.h>
// definieren van patientenvariabelen in structuur 
struct TIJDSTIP {
   int dag;
   int operatiekamer;
};
struct ALLE_PATIENTENDATA {
   int nummer;
   char naam[50];
   struct TIJDSTIP tijdstip ;
   int leeftijd;
   int discipline;
};
 
 
//------------------------------------------------------------------------------
// de main functie
//------------------------------------------------------------------------------
 
int main(int argc, char *argv[])
{
   int count;
   int aantal;
   int lengte_naam;
   int count_naam;
   int nummer_letter_naam;
 
 
//------------------------------------------------------------------------------
// vragen hoeveel patienten moeten aangemaakt worden en het bestand prepareren
//------------------------------------------------------------------------------
 
   // vragen hoeveel patienten er moeten aangemaakt worden
   printf ("Hoeveel patienten wilt u genereren?\n");
   scanf ("%d", &aantal);
   //bestand openen en aantal patienten vermelden
   FILE *bestand_patienten = fopen("patienten.txt", "w");
   fprintf (bestand_patienten, "%d\n", aantal);
 
 
//------------------------------------------------------------------------------
// random patienten in .txt genereren
//------------------------------------------------------------------------------
 
   srand (time(NULL));
   for (count = 1; count <= aantal; count++)
   {  
      // nummer 
      fprintf (bestand_patienten, "%d\t", count);
      // discipline
      fprintf (bestand_patienten, "%d\t", rand() % 2 + 1);
      // leeftijd
      fprintf (bestand_patienten, "%d\t", rand() % 80 + 18);
      //naam
      lengte_naam = rand() % 6 + 5;
      for (count_naam = 1; count_naam <= lengte_naam; count_naam++)
      {
         nummer_letter_naam = rand() % 26 + 1; 
         switch (nummer_letter_naam) 
         {
            case 1  : fprintf(bestand_patienten, "a");
                      break;
            case 2  : fprintf(bestand_patienten, "b");
                      break;
            case 3  : fprintf(bestand_patienten, "c");
                      break;
            case 4  : fprintf(bestand_patienten, "d");
                      break;
            case 5  : fprintf(bestand_patienten, "e");
                      break;
            case 6  : fprintf(bestand_patienten, "f");
                      break;
            case 7  : fprintf(bestand_patienten, "g");
                      break;
            case 8  : fprintf(bestand_patienten, "h");
                      break;
            case 9  : fprintf(bestand_patienten, "i");
                      break;
            case 10  : fprintf(bestand_patienten, "j");
                      break;
            case 11  : fprintf(bestand_patienten, "k");
                      break;
            case 12  : fprintf(bestand_patienten, "l");
                      break;
            case 13  : fprintf(bestand_patienten, "m");
                      break;
            case 14  : fprintf(bestand_patienten, "n");
                      break;
            case 15  : fprintf(bestand_patienten, "o");
                      break;
            case 16  : fprintf(bestand_patienten, "p");
                      break;
            case 17  : fprintf(bestand_patienten, "q");
                      break;
            case 18  : fprintf(bestand_patienten, "r");
                      break;
            case 19  : fprintf(bestand_patienten, "s");
                      break;
            case 20  : fprintf(bestand_patienten, "t");
                      break;
            case 21  : fprintf(bestand_patienten, "u");
                      break;
            case 22  : fprintf(bestand_patienten, "v");
                      break;
            case 23  : fprintf(bestand_patienten, "w");
                      break;
            case 24  : fprintf(bestand_patienten, "x");
                      break;
            case 25  : fprintf(bestand_patienten, "y");
                      break;
            case 26  : fprintf(bestand_patienten, "z");
                      break;
            default : break;
         }
      }
      fprintf(bestand_patienten, "\n"); 
   } 
 
 
//------------------------------------------------------------------------------
//  einde programma -> output laten staan                                                                          
//------------------------------------------------------------------------------
   fclose (bestand_patienten);
 
   //zorgt ervoor dat de output blijft staan op het einde
   printf("\n\n");
   system("PAUSE");
   return (0);
}

thx

Recommended Answers

All 7 Replies

It would have been better if you would have just attached the txt file along with the post. It would save us the trouble of running one extra program esp so when the program is in a foreign langauge.. ;)

I'm confused. You claim

this the full code, which works like a charm...

which to me means it works without error. Then the sentence continues

except for the error when i close the program. (a normal 'send report' / 'do not send report' error).

which seems to mean is doesn't work. You then continue

I've narrowed it down to one line (a variabele being read from a file -> posted in red),

which is a line in the middle of the program, meaning the problem aborts long before the end of the program.

Very confusing. ;)

But the problem is easily explained...

In this code:

// creëren van patiënten variabelen
   struct ALLE_PATIENTENDATA patient[aantal_patienten];
 
//------------------------------------------------------------------------------
//  patientendata in variabelen steken en eventueel lijst opmaken
//------------------------------------------------------------------------------
 
   //de patientengegevens uit bestand in variabelen omzetten
   for (count = 1; count <= aantal_patienten; count++)
   {
       fscanf (bestand_patienten, "%d", &patient[count].nummer);
       fscanf (bestand_patienten, "%d", &patient[count].discipline);
       fscanf (bestand_patienten, "%d", &patient[count].leeftijd);
       fscanf (bestand_patienten, "%s", &patient[count].naam);
   }

assume aantal_patienten is 5. That means patient is an array from 0 to 4. Your loop goes from 1 to 5 so you load patient[5] which doesn't exist. You've just blown you memory and crashed your program.

Also see this to see why the following is a bad idea:

system("PAUSE");

> #include <stdio.h>
> using namespace std;
So which is it - C or C++ ?
Pick a language and stick with it. If you try and walk down the middle of the road with C/C++, you'll just end up being road kill.

> struct ALLE_PATIENTENDATA patient[aantal_patienten];
Variable length array declarations are illegal in both languages. You must be relying on some compiler specific extension.

> for (count = 1; count <= aantal_patienten; count++)
You're also running off the end of the array, and trashing who knows what into the bargain (this is your crash, well one of them at least).
Given array[N]; the standard loop for the array is for ( i = 0 ; i < N ; i++ )

okay: room for improvement

Variable length array declarations are illegal in both languages. You must be relying on some compiler specific extension.

I guess they are valid according to the C99 standard ( 6.7.5 ), but only when they have automatic storage duration....

> #include <stdio.h>
> using namespace std;
So which is it - C or C++ ?
Pick a language and stick with it. If you try and walk down the middle of the road with C/C++, you'll just end up being road kill.

We don't want that :cheesy: , i already removed all the c++, I think -> forgot the namespace. Thx.

>
> struct ALLE_PATIENTENDATA patient[aantal_patienten];
Variable length array declarations are illegal in both languages. You must be relying on some compiler specific extension.

I am using devc++ and when I declare 500 structures: struct ALLE_PATIENTENDATA patient[500], I have no problems filling a lot more structures. Don't ask me why. :lol:

Add these options to the compiler (in the project settings)
-W -Wall -ansi -pedantic -O2
that will make sure you don't use anything which isn't part of standard C

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.