-
Problem with linked lists and iterators
Hey everyone, I'm having a problem with an assignment, and I have the basics down, but there is a huge error I'm missing somehow that is driving the compiler nuts.
Anyhow, been working for hours, any suggestions/help?
Assignment instructions:
Write a C++ program named auctionlist.cpp to read a list of this information from an external file named auction.dat and store it in a list using the STL list class. Store the information in ascending order by ID Number. Then display a menu and implement the following options. The program should accept the number of the menu option, execute the option, and then display the menu again. It should continue this process until a value of zero is entered for the option.
1.
Print a listing of all the information in a table format with one set of headings.
2.
Print prompts and accept information for a new item from the keyboard and add the item to the list in the proper place. Accept the items exactly in the order that they are listed above.
3.
Accept an ID Number and delete the item with that ID Number from the list. Print an error message if no item with that ID Number is on the list.
4.
Accept an Owner ID from the keyboard and print the ID Number, Description, and Current Bid for all items of that owner.
5.
Accept a date from the keyboard and print the ID Number, Description, and Current Bid for all items closing on that date.
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <list>
using namespace std;
// method declarations
struct Auction
{
string IDnum;
string owner;
string category;
string description;
string closedate;
double startbid;
double currentbid;
};
void display(); // displays menu choices
void menu( int &); // displays menu and accepts choice
void readitem( list<Auction> & ); // adds new item to list
void removeitem( list<Auction> &); // removes an item from list
void printlist( list<Auction> & ); // displays list
void readfromfile( list<Auction> &); // reads original data from file
void printbyowner ( list<Auction> &);
void printbydate ( list<Auction> &);
void wait();
// allows the user to read screen
int main()
{
list<Auction> mylist; // original linked list
int choice; // menu option
readfromfile( mylist ); // read original data from file
menu(choice); // display menu
while( choice !=0 ) // repeat until choice is zero
{
switch(choice)
{
case 1: printlist(mylist); // display list
break;
case 2: readitem(mylist); // add item to list
break;
case 3: removeitem(mylist); // remove item from list
break;
case 4: printbyowner(mylist);
break;
case 5: printbydate(mylist);
} // Ends Switch
menu(choice); // display menu again
} // Ends While
return 0;
} // Ends Main Program
// Function Definitions
// read new item from keyboard and add to templist
void readitem( list<Auction> & templist)
{
list<Auction>::iterator p;
bool found = false;
cin.ignore();
Auction item;
cout << endl << "Enter An Identification Number: ";
getline(cin,item.IDnum);
p = templist.begin();
while( p != templist.end() && !found )
if( item.IDnum < (*p).IDnum )
found = true;
else
p++;
cout << endl << "Enter the Owner's ID number: ";
getline(cin,item.owner);
cout << endl << "Enter the Category: ";
getline(cin, item.category);
cout << endl << "Enter the Closing Date: ";
getline(cin, item.closedate);
cout << endl << "Enter the Current Bid: ";
cin >> item.currentbid;
cout << endl << "Enter the Description: ";
getline(cin, item.description);
cout << endl << "Enter the starting bid: ";
cin >> item.startbid;
templist.insert(p,item.IDnum);
templist.insert(p,item.owner);
templist.insert(p,item.category);
templist.insert(p,item.closedate);
templist.insert(p,item.currentbid);
templist.insert(p,item.description);
templist.insert(p,item.startbid);
cout << endl << endl << item.IDnum << " was added to our list.";
wait();
}
// print contents of plist
void printlist( list<Auction> & plist)
{
list<Auction>::iterator p;
cout << endl << "List Contains " << plist.size() << " Items: " << endl;
p = plist.begin();
while( p != plist.end() )
{
cout << (*p).IDnum << endl;
cout << (*p).owner << endl;
cout << (*p).category << endl;
cout << (*p).closedate << endl;
cout << (*p).currentbid << endl;
cout << (*p).description << endl;
cout << (*p).startbid << endl;
p++;
}
getchar();
wait();
}
// accepts item name from keyboard and removes from list
void printbyowner( list<Auction> & ownlist)
{
list<Auction>::iterator p;
Auction temp;
bool found = false;
cout << endl << endl << "Enter Owner Number: ";
cin.ignore();
getline(cin,temp.owner);
p = ownlist.begin();
while ( p != ownlist.end() && !found)
temp = *p;
if(temp.owner == (*p).owner)
found = true;
else
p++;
if (found)
while( p != ownlist.end() )
{
cout << (*p).IDnum << endl;
cout << (*p).owner << endl;
cout << (*p).category << endl;
cout << (*p).closedate << endl;
cout << (*p).currentbid << endl;
cout << (*p).description << endl;
cout << (*p).startbid << endl;
p++;
}
wait();
}
void removeitem( list<Auction> & newlist)
{
list<Auction>::iterator p;
Auction item;
bool found = false;
cout << endl << endl << "Enter Number To Remove: ";
cin.ignore();
getline(cin,item.IDnum);
p = newlist.begin();
while( p != newlist.end() && !found )
if( item.IDnum == (*p).IDnum )
found = true;
else
p++;
if( found )
{
newlist.erase(p);
cout << endl << endl << item.IDnum << " was removed from our list";
}
else
cout << item.IDnum << " is not on our list";
wait();
}
void printbydate( list<Auction> & datelist)
{
list<Auction>::iterator p;
Auction item;
bool found = false;
cout << endl << endl << "Enter Owner Number: ";
cin.ignore();
getline(cin,item.closedate);
p = datelist.begin();
while ( p != datelist.end() && !found)
if(item.closedate == (*p).closedate)
found = true;
else
p++;
if (found)
while( p != datelist.end() )
{
cout << (*p).IDnum << endl;
cout << (*p).owner << endl;
cout << (*p).category << endl;
cout << (*p).closedate << endl;
cout << (*p).currentbid << endl;
cout << (*p).description << endl;
cout << (*p).startbid << endl;
p++;
}
wait();
}
// displays menu choices
void display()
{
cout << " 1. Enter New Information" << endl << endl;
cout << " 2. Print the List" << endl << endl;
cout << " 3. Remove An Item From The List" << endl << endl;
cout << " 4. View Auction Items by Owner" << endl << endl;
cout << " 5. View Auctions Ending on Certain Date" << endl << endl;
}
// performs menu operations
void menu( int &c)
{
system("clear");
do
{
system("clear");
cout << endl << endl;
display();
cout << " Enter Choice: ";
cin >> c;
} while( c < 0 || c > 5 );
}
// reads data from file and places on list
void readfromfile( list<Auction> & newlist)
{
// Code to open file and read data
ifstream fil;
bool found;
Auction item;
list<Auction>::iterator p;
fil.open("auction.dat");
if( fil.fail() )
{
cout << "Data File Not Available " << endl;
exit(1);
}
while( !fil.eof() )
{
getline(fil,item.IDnum);
getline(fil,item.category);
getline(fil,item.closedate);
getline(fil,item.description);
getline(fil,item.owner);
fil >> item.currentbid;
fil >> item.startbid;
p = newlist.begin();
found = false;
while( p != newlist.end() && !found )
if( item.IDnum <= (*p).IDnum )
found = true;
else
p++;
newlist.insert(p,item.IDnum);
newlist.insert(p,item.owner);
newlist.insert(p,item.category);
newlist.insert(p,item.closedate);
newlist.insert(p,item.currentbid);
newlist.insert(p,item.description);
newlist.insert(p,item.startbid);
newlist.insert(p,item.IDnum);
}
fil.close();
}
// pauses screen to allow user to view screen contents
void wait()
{
//char pause;
cout << endl << endl << "Press Enter Key To Continue ";
//cin >> pause;
getchar();
}
-
Hi mate,
here is the first problem:
you declared templist as a list<Auction> templist and you are trying to add the members of the Auction class to the list!
templist.insert(p,item.IDnum);
templist.insert(p,item.owner);
templist.insert(p,item.category);
templist.insert(p,item.closedate);
templist.insert(p,item.currentbid);
templist.insert(p,item.description);
templist.insert(p,item.startbid);
Try something of this form instead:
templist.insert(p,item);
The Elements of your list are Objects of the type Auction, don't forget.
It compiled all right when I changed this in two places...
Cheers,
Dieter
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks