Thread: help please with c++ invovling structures...

  1. #1
    Registered User
    Join Date
    May 2007
    Location
    The US
    Posts
    7

    Unhappy help please with c++ invovling structures...

    Ok so basically this is what I'm supposed to do:


    Write a program that will display and gather info on seven patients.

    Use a file already given that contains the last name, first name and age for each person.

    Read into arrays and initialize blood pressure to 0 and high pressure to false.

    Print out the full name and age of each patient in alphabetical order by last name.

    Next enter the blood pressure numbers and set the high pressure flag for each person. Assume the high pressure flag will be true if the systolic value is greater than 159.

    The print out the full name, age and blood pressure for each patient. Finally print out just the full names of all the patients with high blood pressure.




    I know it's alot but I figured I might as well put it all in. Heres what I have so far and keep in mind right now I'm just trying to get the general idea of the program down not all of the specific ways to print info out.

    Code:
    #include<iostream>
    #include<fstream>
    
    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(patient ln[], int x);
    //Sorts the names by last name in alphabetical order.
    
    ifstream fin;
    
    int main()
    {
        const int NUMBER = 7;
        patient patients[NUMBER];
        bool sorted;
        
        greeting();
        fin.open("F:\\C++\\Disk\\DataDisk\\PATIENTS.dat");
        if(fin.fail())
        {
            cout << "Error opening file for input!\n";
            system("pause");
            exit(1);
        }
    
        
        for(int i=0; i < NUMBER; i++)   
            fin >> patients[i].lastName >> patients[i].firstName >> patients[i].age;
        
        sortAlpha(patients, NUMBER);
    	  
        for(int i=0; i <NUMBER; i++)
        {    
            cout << "The patients name is: " << patients[i].lastName << ", " 
                 << patients[i].firstName << "\nAge: " << patients[i].age << endl;
            patients[i].highPressure = false;
            patients[i].bloodPressure.diastolic = 0;
            patients[i].bloodPressure.systolic = 0;
        }
    
        
        for(int i=0; i < NUMBER; i++)
        {
            cout << "Please enter the blood pressure numbers (systolic then " 
                 << "diastolic) for \n" << patients[i].firstName << " " 
                 << patients[i].lastName << ": ";
            cin >> patients[i].bloodPressure.systolic 
                 >> patients[i].bloodPressure.diastolic;
            if(patients[i].bloodPressure.systolic > 159)
                 patients[i].highPressure = true;
            cout << patients[i].firstName << " " << patients[i].lastName << " who "
                 << "is " << patients[i].age << " years old, has a blood pressure "
                 << "of " << patients[i].bloodPressure.systolic << "/" 
                 << patients[i].bloodPressure.diastolic << endl;
        }
        
        cout << "The patients with high blood pressure are: \n";
        
        for(int i=0; i < NUMBER; i++)
        {
            if(patients[i].highPressure == true)
                 cout << patients[i].firstName << " " << patients[i].lastName 
                      << endl;
        }
        
        
        fin.close();
        
        system("pause");
        return 0;
    }
    
    void greeting()
    {
        cout << "This program will display and gather info for seven patients.\n\n";
    }
    
    void sortAlpha(patient ln[], int x)
    {
        bool sorted;
        
        do
        {
            sorted = true;
    		  for(int i = 0; i < x; i++)
               if(patient.ln[i] > patient.ln[i + 1])              //parse error here saying "parse error before '.' token"
    		     {
                  swap(patient.ln[i], patient.ln[i + 1]);       //and another one here saying the same thing
                  sorted = false;
               }
    	  }
    	  while(!sorted);
    }


    As you can probably see I'm having issues with the alphabetically sorting things. Please help, any suggestions at all would be greatly appreciated. I just fixed stuff so now this is the newer version of the program. Now all I need is the sort function to work... please help! I can't seem to get it to work no matter what I try...


    Only two errors now as parse errors. I commented where they are in the program.
    Last edited by blueskylvr7; 05-31-2007 at 06:39 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    exit() is in <cstdlib>.

    This is probably your main issue:
    Code:
    if(patients[i].highPressure = true)
    You want == there, for comparison instead of assignment.

    Your compiler should warn you about statements like this if you enable warnings. For Dev-C++, add -W -Wall to the extra compiler parameters textbox in the Compiler Options dialog box.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    May 2007
    Location
    The US
    Posts
    7

    ok..

    Ok so I made it say "==" and now none of the patients names at all print out...? Help please!

  4. #4
    Registered User
    Join Date
    May 2007
    Location
    The US
    Posts
    7

    nope

    Oh wait.. nevermind I got it! Thank you!!! Now all I need is the alphabetical sorting... Anyone care to help with that please?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    struct patient
    {
        string lastName;
        string firstName;
        int age;
        pressure bloodPressure;
        bool highPressure;
    };
    
    
        for(int i=0; i < NUMBER; i++)
        {
            fin >> patients[i].lastName >> patients[i].firstName >> patients[i].age;
            sortAlpha(patients[i].lastName, NUMBER, patients[i].age, patients[i].firstName);
            cout << "The patients name is: " << patients[i].lastName << ", " 
                 << patients[i].firstName << endl
                 << "The patients age is: " << patients[i].age << endl;
            patients[i].highPressure = false;
            patients[i].bloodPressure.diastolic = 0;
            patients[i].bloodPressure.systolic = 0;
        }
    
    void sortAlpha(string ln[], int x, int a[], string fn)
    You're passing a single string to sortAlpha, but it's expecting an array of strings.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    May 2007
    Location
    The US
    Posts
    7
    can you explain a little further I'm not sure I quite get it...

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You're getting closer.
    Code:
    sortAlpha(patients.lastName, NUMBER);
    Because of the way you have your data structures set up, in order for sortAlpha to be able to access the lastName of every patient in the patients[] array, you need to pass it the patients array. Something like this:
    Code:
    void sortAlpha(patient ln[], int x)
    {
        bool sorted;
        
        do
        {
            sorted = true;
    		  for(int i = 0; i < x; i++)
            {
               if(ln.lastName[i] > ln.lastName[i + 1])
    		     {
                  swap(ln.lastName[i], ln.lastName[i + 1]);
                  sorted = false;
               }
            }
    	  }
    	  while(!sorted);
    }
    And you would probably rename ln[] to something else.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    May 2007
    Location
    The US
    Posts
    7

    ok

    so I fixed it again and now there is only two errors! They are parse errors I'll explain them and edit my post again. So close!

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    D'oh, I meant ln[i].lastName, not ln.lastName[i]. Oops. My fault.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    May 2007
    Location
    The US
    Posts
    7

    hmm

    Ok so I fixed that but now whenever I try to run the program it stops and like comes up with an outside error even though it compiled fine and says it has to close the program. Whats that mean..?

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You probably got a segmentation fault. That means that your program accessed memory that it is not allowed to. A common cause of a "seg fault" is a buffer overrun, or running off of the end of an array.

    In your case I think this is the problem:
    Code:
    		  for(int i = 0; i < x; i++)
               if(patient.ln[i] > patient.ln[i + 1])
    The i+1 there will be accessing beyond the end of the patient[] array. You need the for loop condition to be "i<x-1", not "i<x".

    You could have a look at this bubble sort tutorial, though your code looks better than the code provided: http://www.cprogramming.com/tutorial.../sorting1.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    May 2007
    Location
    The US
    Posts
    7

    wow

    IT WORKED!!! Thank you soo much. You helped me bunches I'm so exicted to finally have this done! YAY!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  3. Classes and Structures.
    By jrahhali in forum C++ Programming
    Replies: 6
    Last Post: 03-28-2004, 05:03 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM