ok i got past that part i posted about before, now im having some more trouble. i can add 2 patients easily to the file. then i try to display them. it displays the first one just fine, but the second it tries to display the first value in the [1]th element of the array, i get an error message. that part of my code is marked with a /*error*/ just before it. easy to find plz help this is due monday.

heres my code

#include "iostream"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "process.h"
#define maxpatients 8
using namespace std;

void main();



struct namestruct
{
	char firstName[32];
	char middleI;
	char lastName[32];
};

struct addressstruct
{
	char streetAddress[32];
	char city[32];
	char state[3];
	int zipcode;
};

struct locationstruct
{
	int numFloor;
	int numRoom;
	unsigned char subLocation;
};

struct geninfostruct
{
	addressstruct address;
	char phoneNumber[12];
};

struct insuranceinfostruct
{
	char nameCarrier[32];
	geninfostruct insurancegeninfo;
	float deductable;
};

struct datestruct
{
	int day;
	int month;
	int year;
};

struct patientstruct
{
	namestruct namepatient;
	int age;
	geninfostruct geninfopatient;
	insuranceinfostruct insurence;
	datestruct dischargedate;
	locationstruct locationpatient;
	char information[1024];
};



char menu()
{
	char option1;
	do
	{
	system("cls");
	printf("press a coresponding key and then press enter to choose an option:\n1. open a file\n2. write database to file\n3. display patients\n4. find a patient\n5. add a patient\n6. delete a patient\n7. move a patient\n8.print out patients by discharge date\n");
	option1 = getchar();
	}
	while (option1<'1' || option1 > '7');
	
	fflush(stdin);
	
	return option1;
}

void addpatient(patientstruct (*patientstructpointer)[maxpatients], int (*count)=0)
{
	patientstruct patientstruct1[maxpatients];
	int numread;
	char yesno;
	FILE *fp;
	fp=fopen("c:\\patientdirectory.txt","r");
	if(fp==NULL)
	{
		printf("file not found. create one?y/n");
		fflush(stdin);
		do
		{
//			scanf_s("%c",&yesno);
			cin>>yesno;
			switch(yesno)
			{	
				case 'y':
					break;
				case 'n':
					main();
					break;
				default:
					printf("please enter y for yes or n for no and press enter");
					break;
			}
		}while(yesno!='y');
	}
	else
	{
		while (!feof(fp))
		{
			numread = fread((patientstructpointer)[*count],sizeof(*patientstructpointer),1,fp);
			(*count)++;
		}
	}


		





	system("cls");

	fflush(stdin);
	printf("\nEnter the patient's first name\n");
	gets_s((*patientstructpointer)[*count].namepatient.firstName);
	fflush(stdin);
	printf("\nEnter the patient's middle initial\n");
	(*patientstructpointer)[*count].namepatient.middleI=getchar();
	fflush(stdin);
	printf("\nEnter the patient's last name\n");
	gets_s((*patientstructpointer)[*count].namepatient.firstName);
	fflush(stdin);
	printf("\nEnter the patient's age\n");
	scanf_s("%d",&(*patientstructpointer)[*count].age);
	fflush(stdin);
	printf("\nEnter the patient's state of residence\n");
	gets_s((*patientstructpointer)[*count].geninfopatient.address.state);
	printf("\nEnter the patient's city of residence\n");
	gets_s((*patientstructpointer)[*count].geninfopatient.address.city);
	fflush(stdin);
	printf("\nEnter the patient's zipcode of residence\n");
	scanf_s("%d",&(*patientstructpointer)[*count].geninfopatient.address.zipcode);
	fflush(stdin);
	printf("\nEnter the patient's street address of residence\n");
	gets_s((*patientstructpointer)[*count].geninfopatient.address.streetAddress);
	fflush(stdin);
	printf("\nEnter the patient's phone number\n");
	gets_s((*patientstructpointer)[*count].geninfopatient.phoneNumber);
	fflush(stdin);
	printf("\nEnter the patient's insurance carrier's name\n");
	gets_s((*patientstructpointer)[*count].insurence.nameCarrier);
	fflush(stdin);
	printf("\nEnter the patient's insurance carrier's state\n");
	gets_s((*patientstructpointer)[*count].insurence.insurancegeninfo.address.state);
	fflush(stdin);
	printf("\nEnter the patient's insurance carrier's city\n");
	gets_s((*patientstructpointer)[*count].insurence.insurancegeninfo.address.city);
	fflush(stdin);
	printf("\nEnter the patient's insurance carrier's zipcode\n");
	scanf_s("%d",&(*patientstructpointer)[*count].insurence.insurancegeninfo.address.zipcode);
	fflush(stdin);
	printf("\nEnter the patient's insurance carrier's street address\n");
	gets_s((*patientstructpointer)[*count].insurence.insurancegeninfo.address.streetAddress);
	fflush(stdin);
	printf("\nEnter the patient's insurance carrier's deductable\n");
	scanf_s("%f",&(*patientstructpointer)[*count].insurence.deductable);
	fflush(stdin);
	printf("\nEnter the patient's year of discharge\n");
	scanf_s("%d",&(*patientstructpointer)[*count].dischargedate.year);
	fflush(stdin);
	printf("\nEnter the patient's month of discharge\n");
	scanf_s("%d",&(*patientstructpointer)[*count].dischargedate.month);
	fflush(stdin);
	printf("\nEnter the patient's day of discharge\n");
	scanf_s("%d",&(*patientstructpointer)[*count].dischargedate.day);
	fflush(stdin);
	printf("\nEnter the patient's floor number\n");
	scanf_s("%d",&(*patientstructpointer)[*count].locationpatient.numFloor);
	fflush(stdin);
	printf("\nEnter the patient's room number\n");
	scanf_s("%d",&(*patientstructpointer)[*count].locationpatient.numRoom);
	fflush(stdin);
	printf("\nEnter the patient's location number\n");
//location
	((*patientstructpointer)[*count].locationpatient.subLocation)=1;
	int locationnumber;
	scanf_s("%d",&locationnumber);
		switch(locationnumber)
		{	
		case 1:
			break;
		case '2':
			((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<1;
			break;
		case '3':
			((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<2;
			break;
		case '4':
			((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<3;
			break;
		case '5':
			((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<4;
			break;
		case '6':
			((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<5;
			break;
		case '7':
			((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<6;
			break;
		case '8':
			((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<7;
			break;
		default:
			printf("you're fired");
		}
	fflush(stdin);
	printf("\nEnter the patient's description (less than 1023 characters please)\n");
	gets_s((*patientstructpointer)[*count].information);
	fp=fopen("c:\\patientdirectory.txt","a");
/**/fwrite ((patientstructpointer)[*count],sizeof((*patientstructpointer)),1,fp);
}





void displaypatients()
{
	FILE *fp;
	fp=fopen("c:\\patientdirectory.txt","r");
	patientstruct (*patientstructpointer)[maxpatients];
	patientstruct (patientstruct1)[maxpatients];
	(patientstructpointer)=&(patientstruct1);
	int numread;
	int *count;
	int asa;
	count=&asa;
	asa=0;
	while (!feof(fp))
	{
		numread = fread((patientstructpointer)[*count],sizeof(*patientstructpointer),1,fp);
		fflush(stdin);
		printf("\npatient's first name\n");
/*ERROR*/		puts((*patientstructpointer)[*count].namepatient.firstName);
		fflush(stdin);
		printf("\npatient's middle initial\n");
		printf("%c",(*patientstructpointer)[*count].namepatient.middleI);
		fflush(stdin);
		printf("\npatient's last name\n");
		puts((*patientstructpointer)[*count].namepatient.firstName);
		fflush(stdin);
		printf("\npatient's age\n");
		printf("%d",(*patientstructpointer)[*count].age);
		fflush(stdin);
		printf("\npatient's state of residence\n");
		puts((*patientstructpointer)[*count].geninfopatient.address.state);
		printf("\npatient's city of residence\n");
		puts((*patientstructpointer)[*count].geninfopatient.address.city);
		fflush(stdin);
		printf("\npatient's zipcode of residence\n");
		printf("%d",(*patientstructpointer)[*count].geninfopatient.address.zipcode);
		fflush(stdin);
		printf("\npatient's street address of residence\n");
		puts((*patientstructpointer)[*count].geninfopatient.address.streetAddress);
		fflush(stdin);
		printf("\npatient's phone number\n");
		puts((*patientstructpointer)[*count].geninfopatient.phoneNumber);
		fflush(stdin);
		printf("\npatient's insurance carrier's name\n");
		puts((*patientstructpointer)[*count].insurence.nameCarrier);
		fflush(stdin);
		printf("\npatient's insurance carrier's state\n");
		puts((*patientstructpointer)[*count].insurence.insurancegeninfo.address.state);
		fflush(stdin);
		printf("\npatient's insurance carrier's city\n");
		puts((*patientstructpointer)[*count].insurence.insurancegeninfo.address.city);
		fflush(stdin);
		printf("\npatient's insurance carrier's zipcode\n");
		printf("%d",(*patientstructpointer)[*count].insurence.insurancegeninfo.address.zipcode);
		fflush(stdin);
		printf("\npatient's insurance carrier's street address\n");
		puts((*patientstructpointer)[*count].insurence.insurancegeninfo.address.streetAddress);
		fflush(stdin);
		printf("\npatient's insurance carrier's deductable\n");
		printf("%f",(*patientstructpointer)[*count].insurence.deductable);
		fflush(stdin);
		printf("\npatient's year of discharge\n");
		printf("%d",(*patientstructpointer)[*count].dischargedate.year);
		fflush(stdin);
		printf("\npatient's month of discharge\n");
		printf("%d",(*patientstructpointer)[*count].dischargedate.month);
		fflush(stdin);
		printf("\npatient's day of discharge\n");
		printf("%d",(*patientstructpointer)[*count].dischargedate.day);
		fflush(stdin);
		printf("\npatient's floor number\n");
		printf("%d",(*patientstructpointer)[*count].locationpatient.numFloor);
		fflush(stdin);
		printf("\npatient's room number\n");
		printf("%d",(*patientstructpointer)[*count].locationpatient.numRoom);
		fflush(stdin);
		printf("\npatient's location number\n");
//location
		int locationnumber;
		switch((*patientstructpointer)[*count].locationpatient.subLocation)
		{	
			case 1:
				locationnumber=1;
				break;
			case (1<<1):
				locationnumber=2;
				break;
			case (1<<2):
				locationnumber=3;
				break;
			case (1<<3):
				locationnumber=4;
				break;
			case (1<<4):
				locationnumber=5;
				break;
			case (1<<5):
				locationnumber=6;
				break;
			case (1<<6):
				locationnumber=7;
				break;
			case (1<<7):
				locationnumber=8;
				break;
			default:
				locationnumber=0;
				printf("you're fired");
			}
			printf("%d",locationnumber);
		fflush(stdin);
		printf("\npatient's description (less than 1023 characters please)\n");
		puts((*patientstructpointer)[*count].information);
		(*count)++;
	}

		printf("\npress any key to continue\n");
		getchar();
}
	
	





void main()
{
	patientstruct patientstruct1[maxpatients];
	char entry;
	int count = 0;
	do
	{
		entry = menu();
		switch(entry)
		{		
		case '1':

			break;
		case '2':

			break;
		case '3':
			displaypatients();
			break;
		case '4':

			break;
		case '5':
			addpatient(&patientstruct1,&count);
			break;
		case '6':

			break;
		case '7':

			break;
		case '8':

			break;
		default:
			printf("you're fired");
		}
	}
	while (entry != '7');	






for(;;);
}

Recommended Answers

All 7 Replies

>>fflush(stdin);
fflush is not defined for input streams, only output, so the above may or may not work.

Is this supposed to be a C or a C++ program. Looks like C, but you have tossed in iostream for some unknown reason.

Whay did you write all that unnecessary pointer stuff ?? All it does is obfuscate your program.

Your use of gets_s() is incorrect. It's supposed to have two parameters, not one. The second parameter is the size of the input buffer.

The while loop starting on line 114 is incorrect. Here's how it should be coded: Also check out how the sizeof operator is coded in the line below.

while (fread(&patientstructpointer[*count],sizeof(patientstructpointer[0]),1,fp) > 0)
		{
			(*count)++;
		}

Here is how that whole function should have been coded to simplify all those pointers. I left in all those fflush(stdin) lines because I was too lazy to remove them. I also didn't change all those gets_s() function calls for the same reason.

void addpatient(patientstruct patientstructpointer[maxpatients], int (*count)=0)
{
//	patientstruct patientstruct1[maxpatients];
	int numread;
	char yesno;
	FILE *fp;
	fp=fopen("c:\\patientdirectory.txt","r");
	if(fp==NULL)
	{
		printf("file not found. create one?y/n");
		fflush(stdin);
		do
		{
//			scanf_s("%c",&yesno);
			cin>>yesno;
			switch(yesno)
			{	
				case 'y':
					break;
				case 'n':
					main();
					break;
				default:
					printf("please enter y for yes or n for no and press enter");
					break;
			}
		}while(yesno!='y');
	}
	else
	{
		while (fread(&patientstructpointer[*count],
            sizeof(patientstructpointer[0]),1,fp) > 0)
		{
			(*count)++;
		}
	}


		





	system("cls");

	printf("\nEnter the patient's first name\n");
	gets_s(patientstructpointer[*count].namepatient.firstName);
	printf("\nEnter the patient's middle initial\n");
	patientstructpointer[*count].namepatient.middleI = getchar();
	printf("\nEnter the patient's last name\n");
	gets_s(patientstructpointer[*count].namepatient.firstName);
	printf("\nEnter the patient's age\n");
	scanf_s("%d",&patientstructpointer[*count].age);
	fflush(stdin);
	printf("\nEnter the patient's state of residence\n");
	gets_s(patientstructpointer[*count].geninfopatient.address.state);
	printf("\nEnter the patient's city of residence\n");
	gets_s(patientstructpointer[*count].geninfopatient.address.city);
	fflush(stdin);
	printf("\nEnter the patient's zipcode of residence\n");
	scanf_s("%d",&patientstructpointer[*count].geninfopatient.address.zipcode);
	fflush(stdin);
	printf("\nEnter the patient's street address of residence\n");
	gets_s(patientstructpointer[*count].geninfopatient.address.streetAddress);
	fflush(stdin);
	printf("\nEnter the patient's phone number\n");
	gets_s(patientstructpointer[*count].geninfopatient.phoneNumber);
	fflush(stdin);
	printf("\nEnter the patient's insurance carrier's name\n");
	gets_s(patientstructpointer[*count].insurence.nameCarrier);
	fflush(stdin);
	printf("\nEnter the patient's insurance carrier's state\n");
	gets_s(patientstructpointer[*count].insurence.insurancegeninfo.address.state);
	fflush(stdin);
	printf("\nEnter the patient's insurance carrier's city\n");
	gets_s(patientstructpointer[*count].insurence.insurancegeninfo.address.city);
	fflush(stdin);
	printf("\nEnter the patient's insurance carrier's zipcode\n");
	scanf_s("%d",&patientstructpointer[*count].insurence.insurancegeninfo.address.zipcode);
	fflush(stdin);
	printf("\nEnter the patient's insurance carrier's street address\n");
	gets_s(patientstructpointer[*count].insurence.insurancegeninfo.address.streetAddress);
	fflush(stdin);
	printf("\nEnter the patient's insurance carrier's deductable\n");
	scanf_s("%f",&patientstructpointer[*count].insurence.deductable);
	fflush(stdin);
	printf("\nEnter the patient's year of discharge\n");
	scanf_s("%d",&patientstructpointer[*count].dischargedate.year);
	fflush(stdin);
	printf("\nEnter the patient's month of discharge\n");
	scanf_s("%d",&patientstructpointer[*count].dischargedate.month);
	fflush(stdin);
	printf("\nEnter the patient's day of discharge\n");
	scanf_s("%d",&patientstructpointer[*count].dischargedate.day);
	fflush(stdin);
	printf("\nEnter the patient's floor number\n");
	scanf_s("%d",&patientstructpointer[*count].locationpatient.numFloor);
	fflush(stdin);
	printf("\nEnter the patient's room number\n");
	scanf_s("%d",&patientstructpointer[*count].locationpatient.numRoom);
	fflush(stdin);
	printf("\nEnter the patient's location number\n");
//location
	(patientstructpointer[*count].locationpatient.subLocation)=1;
	int locationnumber;
	scanf_s("%d",&locationnumber);
		switch(locationnumber)
		{	
		case 1:
			break;
		case '2':
			(patientstructpointer[*count].locationpatient.subLocation)=(patientstructpointer[*count].locationpatient.subLocation)<<1;
			break;
		case '3':
			(patientstructpointer[*count].locationpatient.subLocation)=(patientstructpointer[*count].locationpatient.subLocation)<<2;
			break;
		case '4':
			(patientstructpointer[*count].locationpatient.subLocation)=(patientstructpointer[*count].locationpatient.subLocation)<<3;
			break;
		case '5':
			(patientstructpointer[*count].locationpatient.subLocation)=(patientstructpointer[*count].locationpatient.subLocation)<<4;
			break;
		case '6':
			(patientstructpointer[*count].locationpatient.subLocation)=(patientstructpointer[*count].locationpatient.subLocation)<<5;
			break;
		case '7':
			(patientstructpointer[*count].locationpatient.subLocation)=(patientstructpointer[*count].locationpatient.subLocation)<<6;
			break;
		case '8':
			(patientstructpointer[*count].locationpatient.subLocation)=(patientstructpointer[*count].locationpatient.subLocation)<<7;
			break;
		default:
			printf("you're fired");
		}
	fflush(stdin);
	printf("\nEnter the patient's description (less than 1023 characters please)\n");
	gets_s(patientstructpointer[*count].information);
	fp=fopen("c:\\patientdirectory.txt","a");
  fwrite (&patientstructpointer[*count],sizeof(patientstructpointer[0]),1,fp);
}

>>fflush(stdin);
fflush is not defined for input streams, only output, so the above may or may not work.

Is this supposed to be a C or a C++ program. Looks like C, but you have tossed in iostream for some unknown reason.

Whay did you write all that unnecessary pointer stuff ?? All it does is obfuscate your program.

Your use of gets_s() is incorrect. It's supposed to have two parameters, not one. The second parameter is the size of the input buffer.

The while loop starting on line 114 is incorrect. Here's how it should be coded: Also check out how the sizeof operator is coded in the line below.

while (fread(&patientstructpointer[*count],sizeof(patientstructpointer[0]),1,fp) > 0)
		{
			(*count)++;
		}

well the fflushes and gets SEEM to work because i can write and read one patient just fine, but not 2. maybe there is a default value for the 2nd parameter in gets? the c professor doesnt mind if we use a little c++. the pointers i know are confusing, but im just following his general format for passing arguments by address.

also, and this is especially interesting, if i do what u said to do in line 114, there is no change, but if i do the same thing to line 246, the exe skips right over that entire section of the program

I think you misunderstood how to pass arrays to functions. Arrays are always passed by reference (pointer). In the code snippet below the array is passed from main() to function foo() by reference. So any changes made to the array in foo() will be reflected in the array in main().

void foo(int array[])
{

}

int main()
{
   int array[25];

   foo(array);
}

>>well the fflushes and gets SEEM to work because i can write and read one patient just fine
Well, your program crashed on me. In response to the question to enter the state, try entering "Illinois". Programs need to be made idot-proof, and you programs lets idots crash the program.

the only options that half-work right now are 5 and 3, and u need to enter addapatient 5 before u can displaypatients 3, and u can only add one patient at this time, if u add 2, the program freezes right when it starts to read that 2nd patient.
also, i can get a hex reader that works, but i can read some of the giberish in notepad, and there is twice as much data there if i have added 2 patients rather than addign just 1 patient

yo someone just gave me a hint but they were a little unspecific, he said "it probably didnt clean the buffer between READING the 1st file and inputing the SECOND file"
so how do i do that? FFLUSH(STDIN);? system("cls");?

its also interesting that "numread" returns a zero on the first patient and a 1 on the 2nd patient

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.