Thread: Output not good

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

    Output not good

    Hi i have created a program that simulates an elevator. It works ok, but is there a way I can get a better random output than the
    method I have used? Here is my program -- i would also appreciate any light critisism on my idea.

    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <ctime>
    
    // class for elevator bell 
    class Bell
    {
    private:
            int m_ring;
    					
    public:
           Bell( int initring ) { m_ring = initring; }
         ~Bell() {};
         void rang ( void ) { std::cout << "\n\nThe bell rang!"; }
         void noRang ( void ) { std::cout << "\n\nAwaiting bell..."; }
         int setRing( int RING ) { m_ring = RING; }
         int getRing () const { return m_ring; }
    };
    
    // class for time of day
    class Clock
    {
    private:
            int m_time;
    				
    public:
           Clock ( int inittime ) { m_time = inittime; }
          ~Clock() {};
        void announce ( void ) { std::cout << "\n\nEstimating time.."; }
        int setTime ( int TIME ) {  
        TIME = rand() & 0 + 59; m_time = TIME; }
    			               
        int getTime() const { return m_time; }
    };
    
    // class for people 
    class Person
    {
    private:
             int m_pgie;	// people getting in elevator
            int m_pgoe; // people getting off elevator
            int m_psie; // people staying in elevator
    				
    public:
           Person ( int initpgie, int initpgoe, int initpsie ) { m_pgie  =initpgie;  m_pgoe = initpgoe; m_psie = initpsie; }
           ~Person() {};
          void peopleIn ( void ) { std::cout << "\n\nPeople enter elevator"; }
    void peopleStay ( void ) { std::cout << "\n\nPeople staying in elevator"; }void peopleOut ( void ) { std::cout << "\n\nPeople leave elevator"; }
    
    int getPgie() const { return m_pgie; }
     int getPgoe() const { return m_pgoe; }
    int getPsie() const { return m_psie; }
    	     
     int setPgie ( int PGIE ) { 
    	    PGIE = rand() & 0 + 25; m_pgie = PGIE; }
    	           
     int setPgoe ( int PGOE ) { 
                   PGOE = rand() * 0 + 25; m_pgoe = PGOE; }
    	     int setPsie ( int PSIE ) {
      PSIE = rand() & 0 + 25; m_psie = PSIE; }
    };
    						 
    // driver - main function //////////////////////////////////////////////////////
    //
    
    int main ( void )
    {
     // randomise results
      srand(time(0));
     		
     std::cout << "\tELEVATOR PROGRAM\n\n";
     		
     // create objects and assign all default values to zero
     Bell bl(0);
     Clock cl(0);
     Person pp(0,0,0);
     		
     cl.announce();
    cl.setTime(0);
    std::cout << "The time minutes past the hour is: " << cl.getTime();
     		
     bl.noRang();
     std::cout << "\n\nbell has a null value of " << bl.getRing();
     bl.setRing(1);
     bl.rang();
     std::cout << "\n\nThe bell rang, so has a true value of " << bl.getRing();
     		
     pp.peopleIn();
     pp.setPgie(0);
     std::cout << "\n\nAmount of people entered was: " << pp.getPgie() << "\n\n";
     		
     std::cout << "\n\n*****ELEVATOR IN MOTION******\n\n";
     		
     pp.peopleStay();
     pp.setPsie(0);
     std::cout << "\n\nAmount of people who stayed was: " << pp.getPsie() << "\n\n";
     		
     pp.peopleOut();
     pp.setPgoe(0);
     std::cout << "\n\nAmount of people who left was: " << pp.getPgoe() << std::endl;
    
     std::cin.get();
     		
     return 0;
    }
    PS: I appoligise for the way it has appeared on the cboard as in my editor, MSVC++2003 it looks much better than this!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
     int setPgie ( int PGIE ) { 
    	    PGIE = rand() & 0 + 25; m_pgie = PGIE; }
    	           
     int setPgoe ( int PGOE ) { 
                   PGOE = rand() * 0 + 25; m_pgoe = PGOE; }
    	     int setPsie ( int PSIE ) {
      PSIE = rand() & 0 + 25; m_psie = PSIE; }
    Your rand() calls have no effect, because you're multiplying one by 0 and ANDing the others with 0. If you're trying to limit the range of values that rand() returns, use
    Code:
    int random_number_from_0_to_24 = rand() % 25;
    or see Prelude's page at http://eternallyconfuzzled.com/articles/rand.html.

    And why are you passing a parameter to those functions? All they do it re-assign the value of the variable anyway. You could have used a local variable. [edit] Actually, you don't need a temporary variable at all. You could assign the result of rand() directly to the member variables. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  4. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM
  5. sorting output on student info program
    By indigo0086 in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2002, 11:29 AM