Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 7th, 2005, 9:04 AM   #1
Dark_Shinobi
Newbie
 
Join Date: May 2005
Posts: 5
Rep Power: 0 Dark_Shinobi is on a distinguished road
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?
Dark_Shinobi is offline   Reply With Quote
Old May 7th, 2005, 9:26 AM   #2
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
#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
}
Eggbert is offline   Reply With Quote
Old May 21st, 2005, 9:32 AM   #3
Dark_Shinobi
Newbie
 
Join Date: May 2005
Posts: 5
Rep Power: 0 Dark_Shinobi is on a distinguished road
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?
Dark_Shinobi is offline   Reply With Quote
Old May 21st, 2005, 12:25 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Make sure the while loop is actually executing - put a cout statement in there.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 21st, 2005, 3:20 PM   #5
Dark_Shinobi
Newbie
 
Join Date: May 2005
Posts: 5
Rep Power: 0 Dark_Shinobi is on a distinguished road
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...
Dark_Shinobi is offline   Reply With Quote
Old May 21st, 2005, 6:10 PM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Can you post the code for it2?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 22nd, 2005, 1:37 PM   #7
Dark_Shinobi
Newbie
 
Join Date: May 2005
Posts: 5
Rep Power: 0 Dark_Shinobi is on a distinguished road
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...
Dark_Shinobi is offline   Reply With Quote
Old May 23rd, 2005, 8:41 AM   #8
Dark_Shinobi
Newbie
 
Join Date: May 2005
Posts: 5
Rep Power: 0 Dark_Shinobi is on a distinguished road
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
Dark_Shinobi is offline   Reply With Quote
Old May 23rd, 2005, 3:50 PM   #9
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Congrats.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:51 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC