| | |
annoying error when closing working program
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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.
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.
thx
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.
C Syntax (Toggle Plain Text)
#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
Last edited by flageolet; Jan 29th, 2007 at 12:35 pm.
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 don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
I'm confused. You claim
which to me means it works without error. Then the sentence continues
which seems to mean is doesn't work. You then continue
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:
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:
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).
•
•
•
•
I've narrowed it down to one line (a variabele being read from a file -> posted in red),
Very confusing.

But the problem is easily explained...
In this code:
cpp Syntax (Toggle Plain Text)
// 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); }
Also see this to see why the following is a bad idea:
c Syntax (Toggle Plain Text)
system("PAUSE");
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
> #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
the standard loop for the array is
> 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++ ) Last edited by ~s.o.s~; Jan 29th, 2007 at 2:45 pm.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
•
•
•
•
> #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.
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:
![]() |
Similar Threads
- Error loading C:\WINNT\Downloaded Program Files\bridge.dll (Viruses, Spyware and other Nasties)
- PHP 5.0 Finally Released! (PHP)
Other Threads in the C Forum
- Previous Thread: Problem when handling with processes!
- Next Thread: malloc() and free(): segmentation fault unavoidable?
Views: 1077 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C
* api append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createcopyoffile createprocess() csyntax database directory drawing dynamic executable execv feet fgets file floatingpointvalidation fork frequency function getlogicaldrivestrin givemetehcodez global graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked reversing scheduling segmentationfault send single socketprogramming spoonfeeding standard strchr string student suggestions system test testautomation testing unix urboc user whythiscodecausesegmentationfault win32api windowsapi






