Results 1 to 4 of 4

Thread: Shody code

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    183

    Shody code

    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??

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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:
    Code:
    #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.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Hyperactive Member Emo's Avatar
    Join Date
    Jul 2000
    Posts
    428
    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)
    -=VB6 Enterprise Edition=-
    -=VC++6Enterprise Edition=-
    «¤E³m°O²™¤»

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I was assuming that those went with any surrounding framework code he already had.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width