![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2005
Posts: 5
Rep Power: 0
![]() |
List of classes
Hello!
I have a question... I want to make a list of classes, but I have some doubts first. 1. How can I input new members in this list? I mean, if it is empty at first, how can I input new classes in it? 2. How can I input information in these classes? I mean, what would the syntax for it be? |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 4
![]() |
#include <list>
class C {
public:
C ( int init = 0 );
void reset ( int init );
};
int main()
{
std::list<C> cl;
cl.push_back ( C() ); // Insert one default constructed C at the end
cl.push_front ( C ( 10 ) ); // Insert a C initialized with 10 at the beginning
std::list<C>::iterator pos = cl.begin(); // Get an iterator to the first node
pos->reset ( 20 ); // Call a member through the iterator
cl.begin()->reset ( 40 ); // Call a member straight from cl.begin()
++pos; // Go to the next node
pos->reset ( 80 ); // Call a member on the second node
} |
|
|
|
|
|
#3 |
|
Newbie
Join Date: May 2005
Posts: 5
Rep Power: 0
![]() |
Thanks for the code, it works perfectly
![]() But right now I have a new problem. It's connected to lists of classes & files. What's the problem in this code? I can't get to store the information in the list of classes in a file. It just won't work. ofstream file;
file.open("saves.txt");
it it2=cl.begin();
while (it2!=cl.end())
{ file<<it2->number<<" "<<it2->lname<<" "<<it2->fname<<endl;
}
file.close();I'm trying to store my list's information in the file "saves.txt", but nothing appears in the file... What could be wrong? |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Make sure the while loop is actually executing - put a cout statement in there.
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: May 2005
Posts: 5
Rep Power: 0
![]() |
Gah, I see your point. Kill cats.
![]() Anyway, I forgot to add in the loop one element. ++it2 . That way the pointer will be incremented till it reaches the end value of the list of classes. Only in a perfect world... That's why I'm asking why it doesn't work... Because I'm almost positive the condition / iterator fails to do something... |
|
|
|
|
|
#6 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Can you post the code for it2?
|
|
|
|
|
|
#7 |
|
Newbie
Join Date: May 2005
Posts: 5
Rep Power: 0
![]() |
Here's the whole code, if you need it
![]() #include <list>
#include <fstream>
#include <iostream>
using namespace std;
class C {
public:
void friend add(list<C>);
void friend display(list<C>);
string fname;
string lname;
int number;
};
typedef list<C> List;
typedef List::iterator it;
int main()
{
list<C> cl;
cout<<"\t\t The Yellow Pages"<<endl;
cout<<"\t\t\t A C++ program coded by"<<endl;
cout<<endl<<endl<<endl;
cout<<"This program simulates the famous Yellow Pages. "<<endl;
cout<<"Please input your choice"<<endl;
int choice;
cout<<"\t 1. Input new data."<<endl;
cout<<"\t 2. Display data."<<endl;
cin>>choice;
switch (choice)
{case 1:
add(cl); break;
case 2:
display(cl); break;
default:
cout<<"Wrong input."<<endl;break;
}
cout<<endl;
ofstream file;
file.open("saves.txt");
it it2=cl.begin();
while (it2!=cl.end())
{ it2++; cout<<"bau bau";
}
file.close();
system("pause");
}
void add(list<C> cl)
{char choice;
int i=1;
cl.push_back( C() );
it it1=cl.begin();
do
{cout<<"Enter first name and last name"<<endl;
it1->number=i; i++;
cin>>it1->fname;
cin>>it1->lname;
cout<<it1->lname<<" "<<it1->fname<<" has been added to the list."<<endl;
cout<<"Press E if you want to add another member, or press Q if you wish to quit"<<endl;
cin>>choice;
if (choice!='E' && choice!='e' && choice!='q' && choice!='Q')
do { cout<<"Wrong Input."<<endl;
cin>>choice;} while (choice!='E' && choice!='e' && choice!='q' && choice!='Q');
cl.push_back( C() );
++it1;
}
while (choice!='q' && choice!='Q');
}
void display(list<C> cl)
{it it2=cl.end();
for (it2; it2!=cl.begin(); it2--)
{cout<<it2->number<<". "<<it2->lname<<" "<<it2->fname<<";"<<endl;
};
}Don't mind the display function, I'm still working on it. The part at which I'm having trouble is the one which handles the files. I just can't get the damn thing to work... |
|
|
|
|
|
#8 |
|
Newbie
Join Date: May 2005
Posts: 5
Rep Power: 0
![]() |
Never mind
I finally managed to solve the problem using a very powerful source named: my programming instructor .Thanks anyway for all the help. I owe you all one ![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|