Does this look right...

Ask for help with your homework/assignments in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Does this look right...

Postby Nex » Sun Nov 23, 2003 8:26 pm

Well I need to write a program for my class.. I need to read in a file to an array of the voterclass... How does this look.

Code: Select all
#include "fileutil.cpp"

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

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

class Voter
{
   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 FileIn(Voter voterlist[],ifstream&, int);
      void SaveDisk(Voter voterlist[], ofstream& outfile, int arraysize);
      void print();
      ~Voter();
};

//*****************************************************************************
// Default Constructor                                                        *
//*****************************************************************************

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

//*****************************************************************************
// Constructor Arguments                                                      *
//*****************************************************************************

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;
}

//*****************************************************************************
// Copy Constructor                                                           *
//*****************************************************************************

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;
}


//*****************************************************************************
// ReadInData                                                                 *
//*****************************************************************************

void Voter::FileIn(Voter voterlist[], ifstream& infile, int arraysize)
{
   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() )
   {
      for ( int x = 0; x < arraysize; x++)
      {
      infile >> idnumber;
      infile.getline(trash,2);
      infile.getline(lastname,25,'\t');
      infile.getline(firstname, 25, '\t');
      infile.getline(party, 2, '\t');
      infile >> birthyear;
      age = 2003 - birthyear;

      voterlist[numVoters] = Voter(idnumber,lastname, firstname, party, birthyear, currentage);
      ++ numVoters;

      }

      if (infile.fail() )
         break;
   }
   infile.close();

}


//*****************************************************************************
// Destructor                                                               *
//*****************************************************************************

Voter::~Voter()
{

}



void PrintList(Voter voterlist[], int);

void main()
{


const int arraysize = 8;
Voter voterlist[arraysize];

   ifstream infile;
   ofstream outfile;

voterlist[arraysize].FileIn(voterlist,infile, arraysize);
PrintList(voterlist, arraysize);
voterlist[arraysize].SaveDisk(voterlist, outfile, arraysize);




}


void PrintList(Voter voterlist[], int arraysize)
{

   for (int n = 0;n<(arraysize) ;n++)
   {
      voterlist[n].print();
   
   }
}
Nex
 

Postby Alloria » Sun Nov 23, 2003 8:28 pm

forgot to log in before i have posted this.. also any suggestions would be helpful on better programming methods.
Alloria
 
Posts: 12
Joined: Sun Nov 23, 2003 8:26 pm

Postby ege » Mon Nov 24, 2003 6:31 am

Does it do what you want it to do?
ege
 

Postby jgbauman » Mon Nov 24, 2003 7:26 am

Read:
viewforum.php?f=252
viewforum.php?f=266

Also using std::string instead of char arrays would make your code safer.
Or use strncpy() instead of strcpy() (It's part of C99)
Your strcpy() usage ist just begging for buffer-overflows
User avatar
jgbauman
 
Posts: 358
Joined: Sat Sep 27, 2003 9:00 am

Postby mfrank410 » Mon Nov 24, 2003 12:22 pm

Not too shabby for a rookie programmer. The class is nice and neat. I also suggest you use std:: and strncpy().
Mike Frank
Co-Owner / Programmer
Frank & Mundula Consulting
http://www.frankmundulaconsulting.com
mfrank@frankmundulaconsulting.com
mfrank410
 
Posts: 50
Joined: Fri Oct 24, 2003 3:35 pm
Location: Ontario, Canada

Postby omnius » Tue Nov 25, 2003 5:14 pm

jgbauman wrote:Or use strncpy() instead of strcpy() (It's part of C99)
Your strcpy() usage ist just begging for buffer-overflows

I'm not sure why you mention C99 here; strncpy() pre-dates C99.
omnius
 
Posts: 496
Joined: Wed Sep 24, 2003 12:03 pm

Postby jgbauman » Tue Nov 25, 2003 6:13 pm

omnius wrote:
jgbauman wrote:Or use strncpy() instead of strcpy() (It's part of C99)
Your strcpy() usage ist just begging for buffer-overflows

I'm not sure why you mention C99 here; strncpy() pre-dates C99.
I know it's C99 and POSIX, but I'm not sure wether it's part of C89 (I think I should get an older C89 just for completeness of my reference sources).
User avatar
jgbauman
 
Posts: 358
Joined: Sat Sep 27, 2003 9:00 am

Postby omnius » Wed Nov 26, 2003 2:22 am

jgbauman wrote:I know it's C99 and POSIX, but I'm not sure wether it's part of C89 (I think I should get an older C89 just for completeness of my reference sources).

I believe strncpy pre-dates C89, although it originally used a type int for the third parameter rather than size_t.
omnius
 
Posts: 496
Joined: Wed Sep 24, 2003 12:03 pm

Postby WaltP » Wed Nov 26, 2003 11:29 am

omnius wrote:
jgbauman wrote:I know it's C99 and POSIX, but I'm not sure wether it's part of C89 (I think I should get an older C89 just for completeness of my reference sources).

I believe strncpy pre-dates C89, although it originally used a type int for the third parameter rather than size_t.

Yes, strncpy (as other strnxxx() functions) are standrd ANSI C. Its the strixxx() family that are not ANSI -- at least before C99. Don't know about after.
There are only 10 types of people in the world -- those that use binary, and those that don't
WaltP
 
Posts: 1187
Joined: Thu Oct 16, 2003 11:24 am


Return to Homeworks

Who is online

Users browsing this forum: No registered users and 2 guests