Solved

Why wont whis left justify..

Posted on 2003-12-09
8
576 Views
Last Modified: 2010-04-01
Im trying to left justify the output..  The olny thing that does not justify is the firstline..  Im trying to use the cout.setf(ios::left) where i read in file. What am i doing wrong.

Report for: Data.txt


1  1002      Mc Carthy    Tom     d        1952      51
       2  1004      Smith        Sara    r        1980      23
       3  1005      Harrison     Ron     C        1966      37
       4  6000      Stone        Sharon  D        1950      53
       5  8000      La Berta     Cathy   R        1952      51


#if !defined(microsoft)
#include<stdlib.h>
#define cls() system("cls");
#endif

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<string.h>
#include<ctype.h>
#include<fstream.h>
#include<windows.h>


#include "color.h"
#include "fileutil.cpp"
#include "voter.h"
#include "date.h"


int  DisplayMenu(int);
void AddVoter(voter voterlist[], int&);
void EditVoter(voter voterlist[], int);
void PrintReport( voter voterlist[], int);
void SaveFile(voter voterlist[], ofstream&, int);
void SaveReport(voter voterlist[], ofstream&, int);
void GetData( voter voterlist[], int&, ifstream&, int&);
void DeleteVoter(voter voterlist[], int& size);




void main()
{


      const int arraysize = 100;
              int size      = arraysize;       
               int error     = 0;
              int choice    = 0;
             
             


      //Array declaration
      voter voterlist[arraysize];



      //Read and write declaration
      ifstream infile;
      ofstream outfile;

      //Calling funtion to get data to build array
      GetData (voterlist, error, infile, size);
      cls();


      //
      while (choice <=8)
      {
      choice = DisplayMenu(choice);
      if (choice == 1)
      {
                  PrintReport(voterlist, size);
                  getche();
      }
      else if
         (choice == 2)
                  SaveReport(voterlist, outfile, size);
      else if
         (choice == 3)
                  AddVoter(voterlist, size);
      else if
         (choice == 4)
                  EditVoter(voterlist, size);
      else if
         (choice == 5)
                  SaveFile(voterlist, outfile, size);
      else if
         (choice == 6)
                  DeleteVoter(voterlist, size);
      else if
         (choice == 7)
                  sortid(voterlist, size);
      else if
         (choice == 8)
             sortArray ( voterlist, size);
               

      }
            cls();
            cout << "Thanks for using program"
                   << endl;
            getche();

}




int DisplayMenu(int choice)
{
      cls();
    cout << setw(25) << "\n ========== Menu Selection Screen ============= \n";
      cout << setw(25) << "\n   [1] Display voters to display                  ";
      cout << setw(25) << "\n   [2] Save Report to Disk                        ";
      cout << setw(25) << "\n   [3] Add New Voter to List                      ";
      cout << setw(25) << "\n   [4] Change Party of Voter                      ";
      cout << setw(25) << "\n   [5] Make Backup File                           ";
      cout << setw(25) << "\n   [6] Delete Voter                               ";
      cout << setw(25) << "\n   [7] Sort by Voter Id                           ";
      cout << setw(25) << "\n   [8] Sort by Last Name                          ";
      cout << setw(25) << "\n   [9] Exit Program                             \n";
      cout << setw(25) << "\n ============================================== \n"  
             << endl;
      cout << setw(22) << "Enter Selection >  ";
      cin  >> choice;

      return choice;

}


void GetData( voter voterlist[], int& error, ifstream& infile, int& size)
{
      int            age,
                  idnumber,
                  birthyear,
                  currentage  = 0,
                  numvoters   = 0;

      char    lastname[25],
                firstname[25],
                party[2],
                trash[2];

OpenInput( infile, "Give name of Data File:  " );


while (infile.good() )
{
      cout.setf(ios::left);
      infile >> idnumber;
      cout.setf(ios::left);
      infile.getline(trash,2);
      infile.getline(lastname,25,'\t');
      cout.setf(ios::left);
      infile.getline(firstname, 25, '\t');
      cout.setf(ios::left);
      infile.getline(party, 2, '\t');
      // party = toupper(party);
      cout.setf(ios::left);
      infile >> birthyear;
      cout.setf(ios::left);
      age = 2003 - birthyear;
      

 if ((strcmp(party, "r") == 0) || (strcmp(party, "R") == 0)  ||
      ( strcmp(party, "d") == 0) || (strcmp(party, "D") == 0)  ||
      ( strcmp(party, "l") == 0) || (strcmp(party, "L") == 0)  ||
      ( strcmp(party, "c") == 0) || (strcmp(party, "C") == 0))
 {
      if ((idnumber > 1000) && ( idnumber < 10000))
      {
            if ((age > 17) && (age < 126))
            {
            voterlist[numvoters] = voter(idnumber,lastname, firstname, party, birthyear, currentage);
            ++ numvoters;
            
            }
            else
            error = error + 1;
            }
            else
            error = error + 1;
            }
            else
            error = error + 1;

      if (infile.fail())
      break;
      size = numvoters;
}
infile.close();
      
}



void AddVoter(voter voterlist[], int& size)
{
      cls();
      size++;
      voterlist[size-1].Add();
      
}



void EditVoter(voter voterlist[], int size)
{
      int choice;
      PrintReport(voterlist, size);
      
      cout<<"Which voter do you wish to edit: ";
      cin>>choice;
      choice--;

      voterlist[choice].Edit();
}      



void PrintReport(voter voterlist[], int size)
{
      cls();
      int label = 1;

      cout << "Report for: Data.txt"
            << endl
             << endl;

      cout << "Label"                << setw(10)
             << "Voter Id"   << setw(11)
             << "Last Name"  << setw(12)
             << "First Name" << setw(7)
             << "Party"             << setw(11)
             << "Birthyear"  << setw(5)
             << "Age"                  
             <<       endl;

      cout << "====="           << setw(10)
             << "========"   << setw(11)
             << "========="  << setw(12)
             << "==========" << setw(7)
             << "====="      << setw(11)
             << "========="  << setw(5)
             << "==="
             <<  endl;

      for (int n = 0;n < size  ;n++)
      {
            
             voterlist[n].print(label);      
            label++;
      }
      cout << endl;
}



void SaveFile(voter voterlist[], ofstream& outfile, int size)
{
      cls();
      OpenOutput(outfile);

      for (int n = 0;n < size ;n++)
      {
      
      voterlist[n].disksave(outfile);
      }
      outfile.close();
}




void SaveReport(voter voterlist[], ofstream& outfile, int size)
{
      cls();
      OpenOutput(outfile);
      

      outfile << "Report for: Data.txt"
                << endl
                << endl;
            
      outfile << "Id Number"   << setw(11)
                  << "Last Name"   << setw(15)
                  << "First Name"  << setw(12)
                  << "Party Id"    << setw(12)
                  << "Bith Year"   <<  setw(13)
                  << "Current Age" << endl;

      outfile << "========="   << setw(11)
                  << "========="   << setw(15)
                  << "=========="  << setw(12)
                  << "========"    << setw(12)
                  << "=========="  << setw(13)
                  << "===========" << endl;

      for (int n = 0;n < size ;n++)
      {
            voterlist[n].disksave(outfile);
      }
      outfile.close();
}



void DeleteVoter(voter voterlist[], int& size)
{
    int choice;
    PrintReport(voterlist, size);
   
     cout<<"Which voter do you wish to delete: ";
    cin>>choice;
for (int i=choice - 1; i< size - 1; i++) {
       voterlist[i] = voterlist[i+1];
   }

   size--;                  
      return;
}




0
Comment
Question by:Nickie24
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 1

Expert Comment

by:meow00
ID: 9908475
I am not an expert .... and I just have some extra questions regarding the above code.
the "vote.h" , "color.h" etc. are they somewhere ? would u mind posting them as well ? or are they embedded in the microsoft window system ? (sorry ... i am not familiar with microsoft .....)

meow
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9908509
meow00 has a point: the problem is in the print routine for voter. I think I know what it is but the code would be nice to see.

It would also be nice to see the complete output from your PrintReport routine including the header (to see how things line up).

Hope to see more, -bcl
0
 
LVL 1

Expert Comment

by:Gratch06
ID: 9908527
Bug is in the print function of the voter class.  I'm betting the issue is that you aren't using setw(...) before you enter the function, so the first element doesn't have its width specified, while all the other fields/lines following it do.

     for (int n = 0;n < size  ;n++)
    {
         
          voterlist[n].print(label);    
          label++;
    }
    cout << endl;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:Gratch06
ID: 9908535
doh...forgot to include the important line.  here goes again ;)

cout << setw(5);
for (int n = 0; n size; n++)
{
    voterlist[n].print(label);
    label++;
}
0
 

Author Comment

by:Nickie24
ID: 9908790
here is voter.h, no need for me to post color.h since its just used for chaging color of text



class voter
{
      
friend void sortid(voter v1[], int size);
friend void sortArray (voter voterlist[], int);
friend ostream& operator  << (ostream&, const voter& v1);
friend ifstream& operator >> (ifstream&, voter& v1);

private:      
      int  idnumber;
      char lastname [25];
      char firstname[25];
      char party         [2];
      int  birthyear;
      int  currentage;


public:
      voter();
      voter(int,char[], char[], char[], int, int);
      voter(const voter&);
      void Add();
      void Edit();
      void operator = (const voter&);
      void print(int);
      void disksave(ofstream& outfile);
      ~voter();
};

voter::voter()
{
      idnumber       =   0;
      lastname [0]   = '\0';
      firstname[0]   = '\0';
      party    [0]   = '\0';
      birthyear      =   0;
      currentage     =   0;
}

voter::voter(int id,char ln[], char fn[], char pid[], int year, int by)
{
      idnumber   = id;
      strcpy(lastname, ln);
      strcpy(firstname, fn);
      strcpy(party, pid);
      birthyear  = year;
      currentage = 2003 - birthyear;
}

voter::voter(const voter& v1)
{
      idnumber   = v1.idnumber;
      strcpy(lastname, v1.lastname);
      strcpy(firstname, v1.firstname);
      strcpy(party, v1.party);
      birthyear  = v1.birthyear;
      currentage = 2003 - v1.birthyear;
}

void voter::operator = (const voter& v1)
{
      idnumber   = v1.idnumber;
      strcpy(lastname, v1.lastname);
      strcpy(firstname, v1.firstname);
      strcpy(party, v1.party);
      birthyear  = v1.birthyear;
      currentage = 2003 - v1.birthyear;
}

void voter::Add()
{

      cls();
      int age =0;

      char trash[2];
      
      cout<<"Enter your voter id number: ";
      cin >> idnumber;

      while ((idnumber < 1000) || // Loop that validates voter
              ( idnumber > 10000))  // id number.

      {      
            cls();
            cout << "Valid ID number is between 1000 and 10000"
                   << endl << endl;

            cout << "Please enter a valid voter id number > ";
            cin  >>  idnumber;
            cout << endl;
      }

      cout<<"Enter your last name: ";
      cin.getline(trash,2);
      cin.getline(lastname, 25);

      cout<<"Enter your first name: ";
      cin.getline(firstname, 25);

      cout<<"Enter your party id letter: ";
      cin>>party;

      cout<<"Enter your birth year: ";
      cin>>birthyear;

      while((birthyear < 1878) || (birthyear > 1985))
      {
      
            cls();
            age = 2003 - birthyear;
      
      
            cout << " You must be atleast 18 yrs. old to vote!!"
                   << endl;
            cout << " Your current age: " << age << endl;



            cout << "\n Please enter a valid birth year: ";
            cin  >> birthyear;
      }

      currentage = 2003 - birthyear;      
}

void voter::Edit()
{
      cout<<"What is the voters new Party: ";
      cin>>party;

}

void voter::print(int label)
{
      cout << label            << setw(10)
           << idnumber      << setw(13)
             << lastname      << setw(8)  
             << firstname      << setw(9)
             << party            << setw(10)
             << birthyear      << setw(8)
             << currentage  << setw(8)
             << endl;
}

void voter::disksave(ofstream& outfile)
{
outfile  << setw(6)  << idnumber
             << setw(14) << lastname  
             << setw(12) << firstname
             << setw(12) << party
             << setw(12) << birthyear
             << setw(12) << currentage  
             << setw(12)
         << endl;
}

ostream& operator << (ostream& vout, const voter& v1)
{
      
      vout << v1.idnumber  << setw(13)
             << v1.lastname      << setw(8)  
             << v1.firstname      << setw(9)
             << v1.party            << setw(10)
             << v1.birthyear      << setw(8)
             << v1.currentage  << setw(8)
             << endl;

return vout;
      
}

ifstream& operator >> (ifstream& vin, voter& v1)
{

      


      return vin;


}



voter::~voter()
{



}

0
 
LVL 11

Accepted Solution

by:
bcladd earned 125 total points
ID: 9908842
As predicted, you use setw() AFTER you print an item. setw refers to the NEXT thing printed. You probably want to reverse each pair of items in print.

  cout << label          << setw(10)
          << idnumber     << setw(13)
 
becomes

  cout  << setw(10) << label
          << setw(13) << idnumber    
 
and so on.

-bcl
0
 
LVL 1

Expert Comment

by:meow00
ID: 9908884
Hi Experts,

    Excuse me again ! I happen to have 2 more questions here ..... I couldn't find any function called : OpenInput() and getche() in standard C++ book. Are they in some "window.h" file ? or certain special functions for microsoft ? since I have never seen them in Unix system .... I am wondering that whether  I  have missed any important things in my study or not .... Thanks a lot !

meow .....
0
 

Author Comment

by:Nickie24
ID: 9908946
getche() just holds the screen -- its from conio.h
openinput  -- is from the filesutil file i made. used to read in data and write data to files.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question