Drving me loopy!!!

This is a discussion on Drving me loopy!!! within the C++ Programming forums, part of the General Programming Boards category; Hi I am developing a program that prints a table of student made up deatials. The program below works ok, ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475

    Drving me loopy!!!

    Hi I am developing a program that prints a table of student made up deatials.
    The program below works ok, but when I go to enter a new detail, it lets me do it, but then I go to end the program to display the results and all I get is the last entry I did. I want the program to print all the details the user inputs, not just the most recent one.

    I think the problem is my while loop in main, can anyone see where I am going worng?

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    using std::cin;
    
    #include <string>
    
    using std::string;
    using std::getline;
    
    // create directory class
    class Directory
    {
    public:
        // constructor - sets course name
        Directory ( string name )
        {
           setListName ( name );
        }
        
        // display name of list
        void setListName ( string name )
        {
           listName = name;
        }
        
        string getListName()
        {
           return listName;
        }
        
        void displayMessage()
        {
           cout << "\tSchool list for: " << getListName();
        }
        
        // student name
        string setStudentName ( string stuName )
        {
           cin.ignore();	// prevents this function being skipped
           cout << "\n\nEnter student name: ";
           getline ( cin, stuName );
           
           studentName = stuName;
           return stuName;
        }
        
        string getStudentName()
        {
           return studentName;
        }
        
        // student date of birth
        int setDob ( int bd )
        {
           cout << "\nEnter year of birth: ";
           cin >> bd;
           
           dob = bd;
           return bd;
        }
        
        int getDob()
        {
           return dob;
        }
        
        // student gender
        char setGender ( char gen )
        {
    	  		
           cout << "\nEnter gender, m / f: ";
           cin >> gen;
           
           gender = gen;
           return gen;
        }
        
        char getGender()
        {
           return gender;
        }
        
    private:
        string listName;	// college name
        string studentName;	// student name
        int dob; // student year of birth
        char gender; // student gender
    };
    
    // main function - driver //////////////////////////////////////////////////////
    //
    int main ( void )
    {
       Directory dir ("MILTON COLLEGE");
       dir.displayMessage();
       
       int answer;
       
       cout << "\n\nWill you use the list: -1 to quit: ";
       cin >> answer;
       
       // quit program if no data needed
       if ( answer == -1 )
       {
          return 0;
       }
       
       // loop to input data
       while ( answer != -1 )
       {
          dir.setStudentName("");
          dir.setDob(0);
          dir.setGender('a');
          
          cout << "\nWill you add another name: -1 to show result: ";
          cin >> answer;
       }
       
       // display headings
       cout << "\nSTUDENT NAME" << "\t\t" << "D O B" << "\t\t" << "GENDER" << "\n\n";
       
       // display result of class 
       cout << dir.getStudentName() << "\t\t" << dir.getDob() << "\t\t"
       	  << dir.getGender() << endl;
       
       cin.get();  // freeze console window
       cin.ignore();
    
       return 0;   // indicate program ended sucsessfuly
    }

  2. #2
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    You only have one dir. You need an instance of dir for each student.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDL sending laptop loopy
    By officedog in forum Tech Board
    Replies: 2
    Last Post: 12-03-2008, 09:15 PM
  2. Diamond loopy
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 09-24-2007, 09:10 AM
  3. Loopy Problem!!!
    By icscou in forum C++ Programming
    Replies: 8
    Last Post: 03-19-2002, 08:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21