annoying error when closing working program

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2007
Posts: 27
Reputation: flageolet is an unknown quantity at this point 
Solved Threads: 0
flageolet's Avatar
flageolet flageolet is offline Offline
Light Poster

annoying error when closing working program

 
0
  #1
Jan 29th, 2007
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.

  1.  
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <stdio.h>
  6. // definieren van patientenvariabelen in structuur
  7. struct TIJDSTIP {
  8. int dag;
  9. int operatiekamer;
  10. };
  11. struct ALLE_PATIENTENDATA {
  12. int nummer;
  13. char naam[50];
  14. struct TIJDSTIP tijdstip ;
  15. int leeftijd;
  16. int discipline;
  17. };
  18.  
  19.  
  20. //------------------------------------------------------------------------------
  21. // de main functie
  22. //------------------------------------------------------------------------------
  23.  
  24. int main(int argc, char *argv[])
  25. {
  26. int count;
  27. int aantal;
  28. int lengte_naam;
  29. int count_naam;
  30. int nummer_letter_naam;
  31.  
  32.  
  33. //------------------------------------------------------------------------------
  34. // vragen hoeveel patienten moeten aangemaakt worden en het bestand prepareren
  35. //------------------------------------------------------------------------------
  36.  
  37. // vragen hoeveel patienten er moeten aangemaakt worden
  38. printf ("Hoeveel patienten wilt u genereren?\n");
  39. scanf ("%d", &aantal);
  40. //bestand openen en aantal patienten vermelden
  41. FILE *bestand_patienten = fopen("patienten.txt", "w");
  42. fprintf (bestand_patienten, "%d\n", aantal);
  43.  
  44.  
  45. //------------------------------------------------------------------------------
  46. // random patienten in .txt genereren
  47. //------------------------------------------------------------------------------
  48.  
  49. srand (time(NULL));
  50. for (count = 1; count <= aantal; count++)
  51. {
  52. // nummer
  53. fprintf (bestand_patienten, "%d\t", count);
  54. // discipline
  55. fprintf (bestand_patienten, "%d\t", rand() % 2 + 1);
  56. // leeftijd
  57. fprintf (bestand_patienten, "%d\t", rand() % 80 + 18);
  58. //naam
  59. lengte_naam = rand() % 6 + 5;
  60. for (count_naam = 1; count_naam <= lengte_naam; count_naam++)
  61. {
  62. nummer_letter_naam = rand() % 26 + 1;
  63. switch (nummer_letter_naam)
  64. {
  65. case 1 : fprintf(bestand_patienten, "a");
  66. break;
  67. case 2 : fprintf(bestand_patienten, "b");
  68. break;
  69. case 3 : fprintf(bestand_patienten, "c");
  70. break;
  71. case 4 : fprintf(bestand_patienten, "d");
  72. break;
  73. case 5 : fprintf(bestand_patienten, "e");
  74. break;
  75. case 6 : fprintf(bestand_patienten, "f");
  76. break;
  77. case 7 : fprintf(bestand_patienten, "g");
  78. break;
  79. case 8 : fprintf(bestand_patienten, "h");
  80. break;
  81. case 9 : fprintf(bestand_patienten, "i");
  82. break;
  83. case 10 : fprintf(bestand_patienten, "j");
  84. break;
  85. case 11 : fprintf(bestand_patienten, "k");
  86. break;
  87. case 12 : fprintf(bestand_patienten, "l");
  88. break;
  89. case 13 : fprintf(bestand_patienten, "m");
  90. break;
  91. case 14 : fprintf(bestand_patienten, "n");
  92. break;
  93. case 15 : fprintf(bestand_patienten, "o");
  94. break;
  95. case 16 : fprintf(bestand_patienten, "p");
  96. break;
  97. case 17 : fprintf(bestand_patienten, "q");
  98. break;
  99. case 18 : fprintf(bestand_patienten, "r");
  100. break;
  101. case 19 : fprintf(bestand_patienten, "s");
  102. break;
  103. case 20 : fprintf(bestand_patienten, "t");
  104. break;
  105. case 21 : fprintf(bestand_patienten, "u");
  106. break;
  107. case 22 : fprintf(bestand_patienten, "v");
  108. break;
  109. case 23 : fprintf(bestand_patienten, "w");
  110. break;
  111. case 24 : fprintf(bestand_patienten, "x");
  112. break;
  113. case 25 : fprintf(bestand_patienten, "y");
  114. break;
  115. case 26 : fprintf(bestand_patienten, "z");
  116. break;
  117. default : break;
  118. }
  119. }
  120. fprintf(bestand_patienten, "\n");
  121. }
  122.  
  123.  
  124. //------------------------------------------------------------------------------
  125. // einde programma -> output laten staan
  126. //------------------------------------------------------------------------------
  127. fclose (bestand_patienten);
  128.  
  129. //zorgt ervoor dat de output blijft staan op het einde
  130. printf("\n\n");
  131. system("PAUSE");
  132. return (0);
  133. }

thx
Last edited by flageolet; Jan 29th, 2007 at 12:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,652
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: annoying error when closing working program

 
0
  #2
Jan 29th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,131
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: annoying error when closing working program

 
0
  #3
Jan 29th, 2007
I'm confused. You claim
Originally Posted by flageolet View Post
this the full code, which works like a charm...
which to me means it works without error. Then the sentence continues

Originally Posted by flageolet View Post
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

Originally Posted by flageolet View Post
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:
  1. // creëren van patiënten variabelen
  2. struct ALLE_PATIENTENDATA patient[aantal_patienten];
  3.  
  4. //------------------------------------------------------------------------------
  5. // patientendata in variabelen steken en eventueel lijst opmaken
  6. //------------------------------------------------------------------------------
  7.  
  8. //de patientengegevens uit bestand in variabelen omzetten
  9. for (count = 1; count <= aantal_patienten; count++)
  10. {
  11. fscanf (bestand_patienten, "%d", &patient[count].nummer);
  12. fscanf (bestand_patienten, "%d", &patient[count].discipline);
  13. fscanf (bestand_patienten, "%d", &patient[count].leeftijd);
  14. fscanf (bestand_patienten, "%s", &patient[count].naam);
  15. }
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:
  1. 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
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: annoying error when closing working program

 
0
  #4
Jan 29th, 2007
> #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++ )
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 27
Reputation: flageolet is an unknown quantity at this point 
Solved Threads: 0
flageolet's Avatar
flageolet flageolet is offline Offline
Light Poster

Re: annoying error when closing working program

 
0
  #5
Jan 29th, 2007
okay: room for improvement
Last edited by flageolet; Jan 29th, 2007 at 2:31 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,652
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: annoying error when closing working program

 
0
  #6
Jan 29th, 2007
Originally Posted by Salem View Post
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....
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 27
Reputation: flageolet is an unknown quantity at this point 
Solved Threads: 0
flageolet's Avatar
flageolet flageolet is offline Offline
Light Poster

Re: annoying error when closing working program

 
0
  #7
Jan 29th, 2007
Originally Posted by Salem View Post
> #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.

Originally Posted by Salem View Post
>
> 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:
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: annoying error when closing working program

 
0
  #8
Jan 29th, 2007
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1077 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC