hi

these are some variables:

struct TIJDSTIP {
   int dag;
   int beginuur;
   int operatiekamer;
};
struct ALLE_PATIENTENDATA {
   int nummer;
   char naam[20];
   struct TIJDSTIP tijdstip ;
   int leeftijd;
   int discipline;
};

first I only fill: nummer, naam, leeftijd and discipline (so the main strucure) using scanf.

example:

scanf ("%d", &patient[count].discipline);

this gives a windows error (using devc++). you know the kind: send report / do not send report.

However, if I remove the second strucure (TIJDSTIP) or define all the variables in it -> everything works just fine.
example:

struct ALLE_PATIENTENDATA {
   int nummer;
   char naam[20];
   int leeftijd;
   int discipline;
};

My question:
Why does the undefined structure TIJDSTIP cause a problem using scanf with the structure ALLE_PATIENTENDATA, when I'm not even using the variables in TIJDSTIP yet?

thx

Recommended Answers

All 3 Replies

How about posting some code.. ? I mean the running code so that we can try it out.

How about posting some code.. ? I mean the running code so that we can try it out.

Except that I doubt you can read German. :cheesy:

Dutch actually :).

working code (comments translated):

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <vector>
#include <cctype>
#define MAX(A,B)  ((A)>(B)?(A):(B)) 
#define MIN(A,B)  ((A)>(B)?(B):(A)) 
/* defining structures*/
struct TIJDSTIP {
   int dag;
   int beginuur;
   int operatiekamer;
};
struct ALLE_PATIENTENDATA {
   int nummer;
   char naam[20];
   struct TIJDSTIP tijdstip ;
   int leeftijd;
   int discipline;
};
 
using namespace std;
int main(int argc, char *argv[])
{
int aantal_patienten = 0;
int count;
 
 
// asking number of patients
printf ("Hoeveel patienten dienen gepland te worden?\n");
// check if input is numerical
while ( true ) 
{
   if ( cin >> aantal_patienten )
      break;
   // leegmaken van de verkeerde input
   cin.clear();
   cin.ignore( 1000, '\n' );
   cout << "Geef aub een getal in.\n";
}
printf ("\n");
// create patients var.
struct ALLE_PATIENTENDATA patient[aantal_patienten];
[B]//define non used structure -> see question[/B]
for (count= 1; count <= aantal_patienten; count++)
{
    patient[count].tijdstip.dag = 1;
    patient[count].tijdstip.beginuur = 1;
    patient[count].tijdstip.operatiekamer = 1;
}
//open file, check succes
FILE *bestand_patienten = fopen("patienten.txt", "w");
if (!(bestand_patienten = fopen ("patienten.txt", "w"))) 
   printf ("Het bestand patienten.txt kon niet geopend worden.");
// add number of patients to file
fprintf(bestand_patienten, "%d\n", aantal_patienten);
// add patients variables to file
for (count = 1 ; count <= aantal_patienten; count++)
{
   // number
   patient[count].nummer = count;
   // name
   printf ("Hoe heet patient %d?\n", patient[count].nummer);
   scanf ("%s", &patient[count].naam);
   // age
   printf ("Hoe oud is patient %d met de naam %s.\n", patient[count].nummer, patient[count].naam);
   // check if numerical 
   while ( true ) 
   {
      if ( cin >> patient[count].leeftijd )
         break;
      // empty wrong input
      cin.clear();
      cin.ignore( 1000, '\n' );
      printf ("Geef aub een getal in.\n");
   }
   // operationtype (must be 1 or 2 (for the moment))
   do 
   {
      printf ("Welk soort operatie dient %s te ondergaan?\n", patient[count].naam); 
      printf ("Typ 1 voor hart en 2 voor longen.\n");
      scanf ("%d", &patient[count].discipline);
   } while (patient[count].discipline < 1 || patient[count].discipline > 2 );
   printf ("\n");
   // write to file
   fprintf(bestand_patienten, "%d\t", patient[count].nummer);
   fprintf(bestand_patienten, "%s\t", patient[count].naam);
   fprintf(bestand_patienten, "%d\t", patient[count].leeftijd);
   fprintf(bestand_patienten, "%d\n", patient[count].discipline);
}
//close file
printf("\n\n");
fclose (bestand_patienten);
 
return (0);
}
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.