PDA

Click to See Complete Forum and Search --> : Shody code


bob323
Jul 15th, 2001, 12:58 PM
I've just written some rather shody code (but it does the job) and was wondering if anyone could suggest some improvements to it, I find this helps me get better!

The program should open the file and read in as many names, and sins as are available inside.
To do this dynamically I created a person class with two private variables, name, and sin and created a pointer to realloc. There are a few things that I am unhappy with in this code though, for one when I reallocate I do it at the beginning of the loop therein always creating one unneeded person, in addition I also have to do a second getline at the end of every loop or else it won't read properly.

//create a person
Person *People = new Person [1];
long int sin;

//read in the first line
while(infile.getline(line, 128)){
//reallocate space for next person
realloc(People, sizeof(Person));
//set the name using the first line
People[person].SetName(line);
//collect the sin
infile >> sin;
People[person].SetSin(sin);
//output all details
People[person].PrintPerson();
//unknown necessary??
infile.getline(line, 128, '\n');
//increment the array counter
person++;
}

The file looks like this:
Bob Johal
12345678
...

Any suggestions??

parksie
Jul 15th, 2001, 03:55 PM
You cannot use realloc with new (unless you use placement new but that's not a good idea). A better alternative would be to use vector:#include <iostream>
#include <vector>

using namespace std;

void somecode() {
vector<Person> vPeople;
long sin;

while(infile.getline(line, 128)) {
Person them;
them.SetName(line);
infile >> sin;
them.SetSin(sin);
them.PrintPerson();
infile.getline(line, 128, '\n');

vPeople.push_back(them);
}
}Not sure if this will compile, I just coded it up right now.

Emo
Jul 15th, 2001, 04:09 PM
When I compiled it, VC++ game me this:

--------------------Configuration: test - Win32 Debug--------------------
Compiling...
Exo.cpp
C:\Windows\Temp\test\Exo.cpp(7) : error C2065: 'Person' : undeclared identifier
C:\Windows\Temp\test\Exo.cpp(7) : error C2955: 'vector' : use of class template requires template argument list
c:\stuff\vb 6\vc98\include\vector(244) : see declaration of 'vector'
C:\Windows\Temp\test\Exo.cpp(7) : error C2133: 'vPeople' : unknown size
C:\Windows\Temp\test\Exo.cpp(7) : error C2512: 'vector' : no appropriate default constructor available
C:\Windows\Temp\test\Exo.cpp(7) : error C2262: 'vPeople' : cannot be destroyed
C:\Windows\Temp\test\Exo.cpp(10) : error C2065: 'infile' : undeclared identifier
C:\Windows\Temp\test\Exo.cpp(10) : error C2228: left of '.getline' must have class/struct/union type
C:\Windows\Temp\test\Exo.cpp(10) : error C2065: 'line' : undeclared identifier
C:\Windows\Temp\test\Exo.cpp(10) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.

Exo.obj - 9 error(s), 0 warning(s)

parksie
Jul 15th, 2001, 06:18 PM
I was assuming that those went with any surrounding framework code he already had.