Thread: myBallot() class

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    myBallot() class

    Hello again,
    I have a question related to one of the classes I am building. I asked a similar question in this forum before and I have not gotten any substantial answers so I thought I would try one more time... Presenting my class...
    Code:
    #ifndef BALLOT_H
    #define BALLOT_H
    
    enum Chad {PUNCHED, HANGING, DIMPLED, NONE};
    enum Choice {BUSH, GORE, BUCHANAN, OTHER, MULTIPLE};
    
    class Ballot
    {
     public:
      Ballot();
      void RecordVote(Choice c, Chad ch, int a);
      Choice GetCandidate();// returns the candidate that is marked
      Chad GetMark();// returns the marking on the chad
      int GetAge();// returns the age
      void Display();// displays ballot info (format given below)
     private:
      Choice who;// candidate that is (or seems to be) marked
      Chad mark;// marking on the selected chad
      int age;// age of voter
    };
    
    #endif
    The function I am having particular trouble with is the void Display function... I need to do what this blurb tells me to do...
    Code:
       void Display();		// displays ballot info (format given below)
    
       // For the Display function, output the ballot choice, the Chad marking,
       // and the age of the voter, all on one line.  IF the chad marking is
       // NONE, then this is a "no vote", so display "No Vote" in the
       // ballot choice position (regardless of the actual contents of
       // the candidate variable).
       // Examples:   	Bush		Punched		Age: 37
       // 		  	Gore		Hanging		Age: 82
       // 			No Vote		None		Age: 53
    So far my attempts have been less than successful.. This is my definition of the void Display() function:
    Code:
    void Ballot::Display()
      // In essence, this would be the display for one ballot
      // the format should be like the following:
      // 1)       Bush      Punched       Age: 23
    {
    
      // since this is the display for one ballot card
      // we put the number list in the Tally::Display()
      if (mark == NONE){
        switch(who)
          {
          case BUSH:      cout << setw(7) << "No Vote"; break;
          case GORE:      cout << setw(7) << "No Vote"; break;
          case BUCHANAN:  cout << setw(7) << "No Vote"; break;
          case OTHER:     cout << setw(7) << "No Vote"; break;
          case MULTIPLE:  cout << setw(7) << "No Vote"; break;
          }
        cout << "None";
      }
      else {
        switch(who)
          {
          case BUSH:      cout << setw(7) << "Bush"; break;
          case GORE:      cout << setw(7) << "Gore"; break;
          case BUCHANAN:  cout << setw(7) << "Buchanan"; break;
          case OTHER:     cout << setw(7) << "Other"; break;
          case MULTIPLE:  cout << setw(7) << "Multiple"; break;
          }
        switch(mark)
          {
          case PUNCHED:   cout << "Punched"; break;
          case HANGING:   cout << "Hanging"; break;
          case DIMPLED:   cout << "Dimpled"; break;
          }
      }
      cout << setw(7) << "Age: " << age;
    
    }
    Please let me know why my output does not get as neat as the one in the examples. Thank you.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Overly complicated. Just use a loop:
    Code:
    for( x = 0; x < size; x++ )
    {
        cout << name[x] << "\t" << chadstate[x] << "\t" << contestantage[x] << endl;
    }
    Honestly, you should probably redesign your class. Without seeing more, all I can do is speculate. You probably want something like:
    Code:
    class Contestant
    {
        public:
            string GetName( );
            void SetName( string );
    
            
            int GetAge( );
            void SetName( int );
    
        private:
            string name;
    };
    
    class Ballot
    {
        public:
            //interface
    
        private:
            enum ChadState { Unpunched, Punched, Foo } chad;
            Contestant sucker;
    };
    Something like that. Then I'd use an array or list of contestants. Perhaps a class called "Election" that has the winner, number of votes, etc.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    92
    thank you for your help quzah but that was not what I was looking for. My professor told me that I needed to use the <iomanip.h> header file and I am not supposed to change the ballot.h in any way.
    I was looking for more to do with the setw() function. I know I need them but I am not sure if I am putting them in the right places for it to look like the examples that the professor has given us. Yours might as very well be more efficient but for a junior-level programming I believe the teacher empasizes substance over style. Thanks again.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well all setw does is set the precision (width). You still have to have padding to make spaces between the columns. Set the precision to the width of the bigest word, add some spaces/tabs/padding between colums, and repeat.

    See my first example of using a for loop. With a slight modification you should be fine.

    What you want is:

    [name of person][spaces][ballot state][spaces][age]

    Thus, simply loop through each person, output their name, some padding, the ballot state, some more space, their age, and a new line.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    92
    I am sorry quzah, maybe a more concrete example would help me more. I am very new to these stream manipulators.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    cout << setw( 10 ) << "Hello." << setw( 20 ) << "fweeee..." << "end." << endl;
    cout << setw( 10 ) << "Hello." << setw( 20 ) << "fweeee..." << "end." << endl;
    cout << setw( 10 ) << "Hello." << setw( 20 ) << "fweeee..." << "end." << endl;

    If you have a smaller amount of text, it will pad it with spaces. If you don't, it won't. Try playing with the setw() precision width to get it to line up the way you like.

    It's not a complicated concept. If you want rows of alligned text, simply adjust the paraemters so each line lines up right.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    92
    It seems to be working. Thank you so much quzah for all your help!

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    92
    Hey quzah,
    One last thing and I will get out of your har for good. How would you be able to left-justify it an output? Thanks

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by supaben34
    Hey quzah,
    One last thing and I will get out of your har for good. How would you be able to left-justify it an output? Thanks
    Should be in your book some place. Otherwise, two seconds and a search engine will give you something like this.
    Code:
    cout << setiosflags(ios::left) << "stuff here...";
    Enjoy.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM