pinsickle 17 Junior Poster in Training

I have a weird file i/o or system() error. Here is the deal, I completed an assignment for threaded trees recently. My professor wanted us to turn in own data set we used to test our tree along with the one he provided for us.
I thought I'd be fancy and set it up so when the program was ran the user could choose between the default (professors') data or mine. If my data set was chosen then I would use system () to call another program I wrote that would 'randomize' the data in my tree test file.

When my data is chosen and the 'randomize' function is ran it does what it is suppose to but I always get an extra node in my tree (usually its the path name to where my files are located). Thought it was weird. I wasn't sure if it would be a file i/o error or something to do with me using system.

Here is the code in question but be forewarned it's not my best work (was just interested in seeing how system() worked)

/***************************************
 * swap.cpp is used to randomize       *
 * the data upon request               *
 * the swap.exe is intended to be ran  *
 * inside of ds09.exe.                 *
 **************************************/

#include "master.h"
int DATA_SIZE = 7;

int main() {
struct data {
    char name[30]; // holds customer's name
    char number[12]; // holds customer's phone number
    bool written;    // used to tell if data has been written back to the file
};

data people [DATA_SIZE];
bool allWritten = false;
int whichOne = -1;

cout << "******Entering swap.exe**********\n";

for (int i = 0; i < DATA_SIZE; i++) //initalizing struct
{
    strcpy(people[i].name, "Blank");
    strcpy(people[i].number, "-1");
    people[i].written = false;
}

fstream file ("random.txt", ios::in |ios::out);

if (!file)
{
    cout << "File not found!\n";
    exit (0);
}

for (int i = 0; i < DATA_SIZE; i++)
{
    file >> people[i].name >> people[i].number; //getting data from file 
}

file.clear();
file.seekg(0);
cout << "data prior to order swap:\n";

for (int k = 0; k < DATA_SIZE; k++)
{
    cout << people[k].name << " " << people[k].number << endl;
}
cout << endl;

srand (time(NULL));

do {
    allWritten = true;
    whichOne = rand () % DATA_SIZE;

    if (people[whichOne].written == false) // if has not been written to the file
    {
        file << people[whichOne].name << ' ' << people[whichOne].number << "\n";
        people[whichOne].written = true;
    }
    
    for (int j = 0; j < DATA_SIZE; j++) // loop to check all have been written
    {
        if (people[j].written == false)
        {
            allWritten = false;
        }
    }
}while(allWritten == false);

file.clear();
file.seekg(0);

cout << "new order is\n";
for (int i = 0; i < DATA_SIZE; i++) //overwriting people[ ] with new reordered data
{
    file >> people[i].name >> people[i].number;
}

for (int k = 0; k < DATA_SIZE; k++)
{
    cout << people[k].name << " " << people[k].number << "\n";
}
cout << endl;
file.close();

cout << "\n********Leaving swap.exe*******\n";

return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.