User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 396,966 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,985 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 278 | Replies: 3
Reply
Join Date: May 2008
Posts: 16
Reputation: GigaCorp is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
GigaCorp GigaCorp is offline Offline
Newbie Poster

another short code question

  #1  
May 10th, 2008
i ended up doing it woithout that function. nov ive got a bigger issue,im trying to make a database of patients and i am working on writing 2 of 8 of the functions in the menu. displaypatients is supposed to display all the info for the patients 1 by 1, and addpatient is supposed to add a patient to an array of patientstructs in the file. i currently can add one patient, then display him, that works. but if i try to add another patient, the first gets all messed up. i believe the fault lies with my fwrite function wich is marked with a /**/ before it:


#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)
{
	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","w");
/**/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;
	numread = fread((patientstructpointer)[*count],sizeof(*patientstructpointer),1,fp);
	fflush(stdin);
	printf("\npatient's first name\n");
	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);
	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(;;);
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2008
Posts: 295
Reputation: tesuji is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 41
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: another short code question

  #2  
May 10th, 2008
Line
  1. fp=fopen("c:\\patientdirectory.txt","r");
opens patientdirectory in read mode only. You may add + or b+ to open it for appending a text file ("r+") or binary file ("rb+").

You must have gotten an error message when trying to write data on file opend with "r" mode using fwrite.
Reply With Quote  
Join Date: May 2008
Posts: 16
Reputation: GigaCorp is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
GigaCorp GigaCorp is offline Offline
Newbie Poster

Re: another short code question

  #3  
May 10th, 2008
fp=fopen("c:\\patientdirectory.txt","w");
/**/fwrite((patientstructpointer)[*count],sizeof(*patientstructpointer),1,fp);
what are you talking about? i opened it for writing the very line above!
Reply With Quote  
Join Date: Jan 2008
Posts: 1,449
Reputation: VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough 
Rep Power: 6
Solved Threads: 185
VernonDozier VernonDozier is offline Offline
Nearly a Posting Virtuoso

Re: another short code question

  #4  
May 10th, 2008
Originally Posted by GigaCorp View Post
what are you talking about? i opened it for writing the very line above!

  1. void addpatient(patientstruct (*patientstructpointer)[maxpatients], int (*count)=0)
  2. {
  3. int numread;
  4. char yesno;
  5. FILE *fp;
  6. fp=fopen("c:\\patientdirectory.txt","r");
  7. if(fp==NULL)
  8. {
  9. printf("file not found. create one?y/n");
  10. fflush(stdin);
  11. do
  12. {
  13. // scanf_s("%c",&yesno);
  14. cin>>yesno;
  15. switch(yesno)
  16. {
  17. case 'y':
  18. break;
  19. case 'n':
  20. main();
  21. break;
  22. default:
  23. printf("please enter y for yes or n for no and press enter");
  24. break;
  25. }
  26. }while(yesno!='y');
  27. }
  28. else
  29. {
  30. while (!feof(fp))
  31. {
  32. numread = fread((patientstructpointer)[*count],sizeof(*patientstructpointer),1,fp);
  33. (*count)++;
  34. }
  35. }
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44. system("cls");
  45.  
  46. fflush(stdin);
  47. printf("\nEnter the patient's first name\n");
  48. gets_s((*patientstructpointer)[*count].namepatient.firstName);
  49. fflush(stdin);
  50. printf("\nEnter the patient's middle initial\n");
  51. (*patientstructpointer)[*count].namepatient.middleI=getchar();
  52. fflush(stdin);
  53. printf("\nEnter the patient's last name\n");
  54. gets_s((*patientstructpointer)[*count].namepatient.firstName);
  55. fflush(stdin);
  56. printf("\nEnter the patient's age\n");
  57. scanf_s("%d",&(*patientstructpointer)[*count].age);
  58. fflush(stdin);
  59. printf("\nEnter the patient's state of residence\n");
  60. gets_s((*patientstructpointer)[*count].geninfopatient.address.state);
  61. printf("\nEnter the patient's city of residence\n");
  62. gets_s((*patientstructpointer)[*count].geninfopatient.address.city);
  63. fflush(stdin);
  64. printf("\nEnter the patient's zipcode of residence\n");
  65. scanf_s("%d",&(*patientstructpointer)[*count].geninfopatient.address.zipcode);
  66. fflush(stdin);
  67. printf("\nEnter the patient's street address of residence\n");
  68. gets_s((*patientstructpointer)[*count].geninfopatient.address.streetAddress);
  69. fflush(stdin);
  70. printf("\nEnter the patient's phone number\n");
  71. gets_s((*patientstructpointer)[*count].geninfopatient.phoneNumber);
  72. fflush(stdin);
  73. printf("\nEnter the patient's insurance carrier's name\n");
  74. gets_s((*patientstructpointer)[*count].insurence.nameCarrier);
  75. fflush(stdin);
  76. printf("\nEnter the patient's insurance carrier's state\n");
  77. gets_s((*patientstructpointer)[*count].insurence.insurancegeninfo.address.state);
  78. fflush(stdin);
  79. printf("\nEnter the patient's insurance carrier's city\n");
  80. gets_s((*patientstructpointer)[*count].insurence.insurancegeninfo.address.city);
  81. fflush(stdin);
  82. printf("\nEnter the patient's insurance carrier's zipcode\n");
  83. scanf_s("%d",&(*patientstructpointer)[*count].insurence.insurancegeninfo.address.zipcode);
  84. fflush(stdin);
  85. printf("\nEnter the patient's insurance carrier's street address\n");
  86. gets_s((*patientstructpointer)[*count].insurence.insurancegeninfo.address.streetAddress);
  87. fflush(stdin);
  88. printf("\nEnter the patient's insurance carrier's deductable\n");
  89. scanf_s("%f",&(*patientstructpointer)[*count].insurence.deductable);
  90. fflush(stdin);
  91. printf("\nEnter the patient's year of discharge\n");
  92. scanf_s("%d",&(*patientstructpointer)[*count].dischargedate.year);
  93. fflush(stdin);
  94. printf("\nEnter the patient's month of discharge\n");
  95. scanf_s("%d",&(*patientstructpointer)[*count].dischargedate.month);
  96. fflush(stdin);
  97. printf("\nEnter the patient's day of discharge\n");
  98. scanf_s("%d",&(*patientstructpointer)[*count].dischargedate.day);
  99. fflush(stdin);
  100. printf("\nEnter the patient's floor number\n");
  101. scanf_s("%d",&(*patientstructpointer)[*count].locationpatient.numFloor);
  102. fflush(stdin);
  103. printf("\nEnter the patient's room number\n");
  104. scanf_s("%d",&(*patientstructpointer)[*count].locationpatient.numRoom);
  105. fflush(stdin);
  106. printf("\nEnter the patient's location number\n");
  107. //location
  108. ((*patientstructpointer)[*count].locationpatient.subLocation)=1;
  109. int locationnumber;
  110. scanf_s("%d",&locationnumber);
  111. switch(locationnumber)
  112. {
  113. case 1:
  114. break;
  115. case '2':
  116. ((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<1;
  117. break;
  118. case '3':
  119. ((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<2;
  120. break;
  121. case '4':
  122. ((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<3;
  123. break;
  124. case '5':
  125. ((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<4;
  126. break;
  127. case '6':
  128. ((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<5;
  129. break;
  130. case '7':
  131. ((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<6;
  132. break;
  133. case '8':
  134. ((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<7;
  135. break;
  136. default:
  137. printf("you're fired");
  138. }
  139. fflush(stdin);
  140. printf("\nEnter the patient's description (less than 1023 characters please)\n");
  141. gets_s((*patientstructpointer)[*count].information);
  142. fp=fopen("c:\\patientdirectory.txt","w");
  143. /**/fwrite ((patientstructpointer)[*count],sizeof(*patientstructpointer),1,fp);
  144. }

He's talking about line 6. You open fp on line 6 with the read flag, , then reopen it on line 142 with the write flag. Do you you ever close it in between? I don't know whether that matters or not since I'm not very familiar with C. I've had that kind of thing cause me trouble in C++ though. You might want to repost in the C forum because this looks like C code.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 8:31 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC