GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Mar-2009, 16:55
lucksjr lucksjr is offline
New Member
 
Join Date: Mar 2009
Posts: 1
lucksjr is on a distinguished road

Need help with linked lists


hello I'm trying to create a linked list and traverse it but seem to be doing something wrong

the program specifications are:

Write a complete C++ program that creates and queries a linked list of people who have served as United States presidents and/or vice presidents. Name your program file pgm4.cpp. Your program should do the following:
1. Define a struct called PublicOfficial to hold the following data about each president or vice president.
Name
State of birth
Office held (1 = President; 2 = Vice President; 3 = both President and Vice President
President Order (a number from 1 - 44 indicating whether he was the 1st, 2nd, 3rd, etc.)
Year first term as President began
Year last term as President ended
Vice President Order (a number from 1 - 44 indicating whether he was the 1st, 2nd, 3rd, etc.)
Year first term as Vice President began
Year last term as Vice President ended
Pointer to the next PublicOfficial in the list

2. In the main function, declare a pointer to serve as the head of a linked list of PublicOfficial structs. Also declare an ofstream variable and open a report file (pgm4.rpt) for writing out the results of queries about the data in the linked list.

3. Call a function named buildList, passing the head pointer by reference. This function will handle reading in all of the data from the file pgm4.dat and storing it in a linked list. First create the ifstream variable, open the input file and check for the success of that operation. Then read in the data for each public official until you reach the end of the file. For each person in the data file:
- Dynamically create a PublicOfficial node to store that person's data.
- Read in the person's Name, State of birth and Office held code. If the Office held code is 1, read in the next three lines and store the data in the President fields. Set the Vice President fields to zero. If the code is 2, read in the next three lines and store the data in the Vice President fields, setting the President fields to zero. If the code is 3 there will be six lines of data to read in, three with the President data followed by three with the Vice President data.
- Call a function named addOfficial to add the new official to the end of the linked list. Pass the pointer to the head of the list and the pointer to the new official to this function.
- Close the input file. All queries should be done using the linked list created from the data in this file.

4. Create a loop in the main function to get the user's selection from the menu of options shown below.

President/Vice President Queries
1. Presidents
2. Vice presidents
3. All who served in both offices
4. Presidents serving in 1700s
5. Presidents serving in 1800s
6. Presidents serving in 1900s
7. Presidents serving in 2000s
8. Presidents from a given state
9. Presidents serving less than full term
10. Exit program

heres the code I have so far:
CPP / C++ / C Code:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

struct publicOfficial
{
        string name;
        int dob,
        officeHeld,
        prezOrder,
        prezStart,
        prezEnd,
        viceOrder,
        viceStart,
        viceEnd;
        publicOfficial* nextOffPtr;
}

//**********************************
//*********Function Prototypes******
//**********************************

void buildList (publicOfficial* &);
void addOfficial (publicOfficial*, publicOfficial*);

int main ()
{

publicOfficial* headOfficialPtr = NULL;
publicOfficial* newOfficialPtr;
newOfficialPtr = new publicOfficial;

    ofstream resultdata;

    resultdata.open("pgm4.rpt");

//**********************************
//*****Function Calls***************
//**********************************

while (!numdata.eof())
{
buildList (headOfficialPtr);
addOfficial (headOfficialPtr, newOfficialPtr);
}

//**********************************
//*******Function Definitions*******
//**********************************

void buildList (publicOfficial* &headOfficialPtr)
{

    ifstream numdata;
    numdata.open("pgm4.dat");

    if ( !numdata )
    {
        cout << "Error opening file" << endl;
        return 0;
    }

   publicOfficial* newOfficialPtr;
   newOfficialPtr = new official;

   getline(cin, hesdOfficialPtr->publicOfficialname);
   getline(cin, headOfficialPtr->publicOfficialdob);
   getline(cin, headOfficialPtr->publicOfficialofficHeld);

        if (officialPtr->publicOfficialofficeHeld == 1)
        {
                   cin >> headOfficialPtr->publicOfficialprezOrder;
                   cin >> heaOfficialPtr->publicOfficialprezStart;
                   cin >> headOfficialPtr->publicOfficialprezEnd;
                   headOfficialPtr->publicOfficialviceOrder = 0;
                   headOfficialPtr->publicOfficialviceStart = 0;
                   headOfficialPtr->publicOfficialviceEnd = 0;
        }

                else if (headOfficialPtr->publicOfficialofficeHeld == 2)
                {
                         headOfficialPtr->publicOfficialprezOrder = 0;
                         headOfficialPtr->publicOfficialprezStart = 0;
                         heaOfficialPtr->publicOfficialprezEnd = 0;
                         cin >> headOfficialPtr->publicOfficialviceOrder;
                         cin >> headOfficialPtr->publicOfficialviceStart;
                         cin >> headOfficialPtr->publicOfficialviceEnd;
                }
                        else
                        {
                                cin >> headOfficialPtr->publicOfficialprezOrder;
                                cin >> headOfficialPtr->publicOfficialprezStart;
                                cin >> headOfficialPtr->publicOfficialprezEnd;
                                cin >> headOfficialPtr->publicOfficialviceOrder;
                                cin >> headOfficialPtr->publicOfficialviceStart;
                                cin >> headOfficialPtr->publicOfficialviceEnd;
                        }
}

void addOfficial (publicOfficial* &headOfficialPtr, publicOfficial* newOfficialPtr)
{
        publicOfficial* officialPtr;

        if (!headOfficialPtr)
        {
                headOfficialPtr = newOfficialPtr;
        }
                else
                {
                        officialPtr = headOfficialPtr;
                        while (officialPtr->nextOffPtr)

                                officialPtr = officialPtr->nextOffPtr;

                        officialPtr->nextOffPtr = newOfficialPtr;
                }
}
Last edited by admin : 03-Mar-2009 at 00:54. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 02-Mar-2009, 19:09
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Need help w/ linked lists


Quote:
Originally Posted by lucksjr
1. Define a struct called PublicOfficial

---snip---

heres the code I have so far:
CPP / C++ / C Code:
#include <iostream>
struct publicOfficial

PublicOfficial is not the same as publicOfficial. You have foundational problems that are more significant than the issues surrounding linked lists. Until you fix some of your foundational issues, you're not ready for linked lists.


MxB
  #3  
Old 03-Mar-2009, 01:44
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Need help with linked lists


Also, How could you write so much code without testing along the way?

Start with your structure declaration and a skeleton main().
Compile and fix... repeat as needed
Then begin to add your buildList() function starting with the file open only.
Don't forget to close you open files and delete you new's.

Add a little at a time, test, fix, repeat. BUILD Your Code.
Then you can address a single problem instead of many.
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked Lists Database... TS3BxL C++ Forum 10 19-Mar-2008 23:37
triangle (polygon), drawing, sizing, and rotation programme using linked lists... promsan C Programming Language 12 14-May-2007 15:03
Linked Lists problem kracivi C Programming Language 1 01-May-2007 09:40
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12
Linked lists vortz83 C++ Forum 3 20-Mar-2004 07:56

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:00.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.