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 26-Oct-2007, 22:56
allenfanwenyuan allenfanwenyuan is offline
Junior Member
 
Join Date: Mar 2007
Posts: 38
allenfanwenyuan is an unknown quantity at this point
Lightbulb

Problem of adding values in pair then print them.


Hi, fellows, I had a problem about adding a student's name and a unit name in pair into a linked list,then print out student's names under the certain unit or print out unit names a student enrolled. I was doing my way, but seems dosen't work, anyone can help me or point out where i should amend. I also stuck to how to write a destructor, any ideas?

Here is my header file .h
CPP / C++ / C Code:
#ifndef H_School
#define H_School

using namespace std;

struct students
{
       string StuName;
       string units;
       struct students *next;
} ;

class School
{
      public:
            School();
    
           ~School();
    
           void addStudentUnit(string studentName, string unitName);
    
           void printUnitsOfStudent(string studentName);
    
           void printStudentsOfUnit(string unitName);
           
      private:
              students *first;
              
    
};

#endif 

Here is implementation file: school.cpp
CPP / C++ / C Code:
#include<iostream>
#include "school.h"
using namespace std;

School::School()
{
       first=NULL;
        
}

School::~School()
{
                 
                 
}

void School::addStudentUnit(string studentName,string unitName)
{
     students *newStu,*last, *p;
   
  
     newStu=new students;
     
     newStu->StuName=studentName;
     newStu->units=unitName;
     newStu->next=NULL;
   
   if(first==NULL)
   {
                  first=newStu;
                  //last=newStu;
   }
   else
   {
       // find last
       p = first;
       last = first;
       while (p != NULL)
       {
             last = p;
             p = p->next;
       }
                last->next=newStu;
                last=newStu;
   }
                
                  
     
               
}

void School::printUnitsOfStudent(string studentName)
{
     students *current;
     current=first;
     if(current->StuName==studentName)
   
         {
    
                         cout<<current->units<<" ";
                         current=current->next;
         }
         cout<<endl;
     
}

void School::printStudentsOfUnit(string unitName)
{
      students *current;
     current=first;
     if(current->units==unitName)
     {
                         cout<<current->StuName<<" ";
                         current=current->next;
     }
}        

and finally, my main program: schoolMain.cpp
CPP / C++ / C Code:
#include <iostream>

#include "school.h"

using namespace std;

string menu();

int main() {
  School school;
  string line = menu();
  while (line != "0") {
    if (line == "1") {
      string studentName, unitName;
      cout << "Type the student name:" << endl;
      getline(cin,studentName);
      cout << "Type the unit name:" << endl;
      getline(cin, unitName);
      school.addStudentUnit(studentName,unitName);
    } else if (line == "2") {
      string studentName;
      cout << "Type the student name:" << endl;
      getline(cin,studentName);
      school.printUnitsOfStudent(studentName);
    } else if (line == "3") {
      string unitName;
      cout << "Type the unit name:" << endl;
      getline(cin,unitName);
      school.printStudentsOfUnit(unitName);
    } // end if
    line = menu();
  } // end while

  system("PAUSE");
  return EXIT_SUCCESS;
}


string menu() {
  string r;

  cout << "0: Exit" << endl;
  cout << "1: Add student/unit pair" << endl;
  cout << "2: Print units of a student" << endl;
  cout << "3: Print students of a unit" << endl;
  cout << "Type the option:" << endl;
  
  getline(cin,r);
  return r;
} // end menu
Last edited by LuciWiz : 03-Nov-2007 at 17:04. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 27-Oct-2007, 08:09
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: problem of adding values in pair then print them.


Quote:
Originally Posted by allenfanwenyuan
...I had a problem...
Look, yes look, at the logic of your print functions.


For, example to print all the students for a given unit shouldn't it go something like the following?
Code:
set current list item to front of list while (current list item is not equal to NULL) { if (the unit of the current list item is equal to the unit for which you are printing names) { print the name for the current list item } current list item = next list item }

And similarly for printing all units of a given student.

Regards,

Dave
  #3  
Old 27-Oct-2007, 08:16
allenfanwenyuan allenfanwenyuan is offline
Junior Member
 
Join Date: Mar 2007
Posts: 38
allenfanwenyuan is an unknown quantity at this point

Re: problem of adding values in pair then print them.


yeah ....Dave ..I ve already worked it out like following :
CPP / C++ / C Code:
void School::printUnitsOfStudent(string studentName)
{
     students *current;
     current=first;
     while(current!=NULL)
     {
           if(studentName==current->StuName)
                  {
                    cout<<current->units<<" ";
                  }
               else
                      {
                                            cout<<"No student found with that name";
                      }
           
          current=current->next;
     }
     cout<<endl;
}

but i am facing another problem, is how to print student's names or units name alphabetically in ascending order. I am thinking now .....but anyway ....thanks Dave
Last edited by LuciWiz : 03-Nov-2007 at 17:06. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #4  
Old 27-Oct-2007, 13:19
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: problem of adding values in pair then print them.


After 37 posts and uncounted edits by the administration, you'd think that by now you'd be able to use CODE tags properly. Is there something about the statement
Quote:
The tagindicator is designed for each language forum. Please replace the value tagindicator with one of the following codes representing the language/markup code you are posting
that you don't understand?

And is
Quote:
Use the Preview Post button before Submitting. You can make sure your post looks the way you want it to look, check spelling, check readability, code is formatted, etc.
not worded for proper understanding?

__________________

Age is unimportant -- except in cheese
  #5  
Old 27-Oct-2007, 14:56
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

Re: problem of adding values in pair then print them.


Quote:
Originally Posted by allenfanwenyuan
but i am facing another problem, is how to print student's names or units name alphabetically in ascending order. I am thinking now .....but anyway ....thanks Dave
Probably easiest to copy them to some STL container, like vector, sort that and print them.

Designing a sort yourself is a bit more complicated. Before you begin to design a sort, learn how to insert nodes and how to remove nodes from the list. And some sorting algorithm would help too, probably.
__________________
Music, programming, endless learning..
  #6  
Old 27-Oct-2007, 17:03
allenfanwenyuan allenfanwenyuan is offline
Junior Member
 
Join Date: Mar 2007
Posts: 38
allenfanwenyuan is an unknown quantity at this point

Re: problem of adding values in pair then print them.


Hi Kimmo,
Can u give me some examples how to copy my code to a STL vector container,then sort and print, cause i have not started to study what STL container is . Cheers !
 
 

Recent GIDBlogObservations of Iraq 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
Fortran problem... Justin Fox Miscellaneous Programming Forum 6 24-Oct-2006 15:30
Convert sentence to morse code earachefl C++ Forum 8 03-May-2006 07:39
problem with switches ChaosTheory C Programming Language 2 31-May-2005 13:38
Strange problem when I try to print a value? 7eleven C Programming Language 2 15-May-2004 10:45
Guestbook error BobbyDouglas Web Design Forum 1 16-Oct-2003 22:17

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

All times are GMT -6. The time now is 17:02.


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