Sunday, July 12, 2009

C++ help! I really need help here. im desperate.?

ok so heres what i have to do... Write a program that will display and gather info on seven patients. Make use of an array of seven structures. Print the full name and age of each patient in alphabetical order by last name. Then enter each blood pressure and set the high pressure flag (true if greater then 159) Then print just the names of the patients with high blood pressure.





For some reason I can't get the alphabetical thing to work or the blood pressure flags. Please help! I'll put in the entire lab I have so far. And I'll put in the data that is taken in.





Lab:


#include%26lt;iostream%26gt;


#include%26lt;fstream%26gt;





using namespace std;





struct pressure


{


int systolic;


int diastolic;


};





struct patient


{


string lastName;


string firstName;


int age;


pressure bloodPressure;


bool highPressure;


};





void greeting();


//Displays greeting.





void sortAlpha(string n[], int x, int a, string fn[]);


//Sorts the names by last name in alphabetical order.

C++ help! I really need help here. im desperate.?
In the future, I recommend you post your programming questions to cboard.cprogramming.com or forums.devshed.com for two reasons. First, the people there are knowledgeable and professional. The second reason is that you can actually post formatted code there. It's impossible on YA!. I spent a minute cleaning up your code, and looked through it. By no means is it a complete review, but I will say your code is a mess. Here are some problems.





Starting from the top of your code.





Did you forget to #include %26lt;string%26gt; ?





ifstream fin is a global variable. Stop using global variables. They have their use, but this isn't one of them. Learn to write clean programs early on.





In main(): bool sorted is pointless. You don't need it in main. Stop creating variables that you don't use.





Use of exit: It's effective to use return. Besides, to use exit you need include cstdlib which you haven't done, and which you really don't need to. Because you can return from main instead of using exit.





Your use of system pause. Fine. But you should realize that it is a kludge. Command line programs are intended to be run from the command line. The only reason you need System pause is because you aren't running them from the command line.





Calling sortAlpha in the first for loop. Huh? How can you be calling sortAlpha 7 times? Remove it from the for loop. You only need to sort once you have *all* the patient data.





Your sorting function is broken. First, understand what data you need to sort. It's an array of patient structures. Not three arrays. See the difference? You are sorting **one** array of patient structure. Your sort function sucks. You're okay with using algorithm's swap, but not qsort? Why don't you just use sort then (http://www.cplusplus.com/reference/algor... ). You need to write a custom comparison function. The function takes int two patient structures, compares the last name of each, and returns true if the first patient goes before the second.
Reply:well....why arent u using strcmp( ) inbuild function to do so?which is dfined under string.h ...tats the way alphabets are sorted wid ease...so wat u can do is make use of ascii values of the alphabets,since more than one person can hav same second name so u can use key as sum of ascii values of first 3 alpahbet of second name,,,,that can b gud way to sort names...i hope u an get help fom these tips..
Reply:I haven't programmed in C++ in years... I hope I'm right. I'd recommend working with it in steps. 1st, read and display the file. 2nd, add sorting, etc.





- I don't see why you have to call the sortAlpha routine while reading text, and you are sorting all elements ("NUMBER") instead of only the ones that have been read!





- You have to pass to SortAlpha the address of the array, or the 1st element. You're passing the address of a specific element, in this case, the value you just read [i].
Reply:Why would you put the sortAlpha inside for? Try to call the sorting function before the loop like this:





// put the sort call here before modifying each data;


// I don't know if you use it properly





for(int i=0; i %26lt; NUMBER; i++)


{


fin %26gt;%26gt; patients[i].lastName %26gt;%26gt; patients[i].firstName %26gt;%26gt; patients[i].age;





cout %26lt;%26lt; "The patients name is: " %26lt;%26lt; patients[i].lastName %26lt;%26lt; ", "


%26lt;%26lt; patients[i].firstName %26lt;%26lt; endl


%26lt;%26lt; "The patients age is: " %26lt;%26lt; patients[i].age %26lt;%26lt; endl;


patients[i].highPressure = false;


patients[i].bloodPressure.dias... = 0;


patients[i].bloodPressure.syst... = 0;


}





I hope this will help. :)
Reply:I may be missing something here, but couldnt you achieve what you need by thinking a bit simpler and creating a relational database using Access or similar. If you input the right info you can extract what you need through queries and from those set up your reports in an aesthetically pleasing format.


No comments:

Post a Comment