I'm having issues with writing to a file. more precisely, i'm trying to determine if a set of numbers are in the file. if they are already in it, don't write to the file. if not, write to the file. My problem is my program is writing all numbers, even duplicates. The sort is working. any help would be appreciated.
#include <iostream>
#include <fstream>
#include <algorithm>
usingnamespace std;
//Counter Stats
int STR;
int DEX;
int CON;
int INTEL;
int WIS;
int CHA;
int Strength=0;
int Dexterity=0;
int Constitution=0;
int Inteligence=0;
int Wisdom=0;
int Charisma=0;
//Array to store each set of Stats to Sort prior to writing to file.
int StatArray[5];
ofstream OutFile;
ifstream InFile;
int GenerateDistinctStatArrays()
{
OutFile.open("DistinctStatArray.txt", ios::app);
InFile.open("DistinctStatArray.txt");
//Generates all Stat Block Options from 7 to 18
for ( STR=7; STR<=8; STR++)
{
for ( DEX=7; DEX<=8; DEX++)
{
for ( CON=7; CON<=8; CON++)
{
for ( INTEL=7; INTEL<=8; INTEL++)
{
for ( WIS=7; WIS<=8; WIS++)
{
for ( CHA=7; CHA<=8; CHA++)
{
bool Match=false;
//Storing Stat Array to sort
StatArray[0] = STR;
StatArray[1] = DEX;
StatArray[2] = CON;
StatArray[3] = INTEL;
StatArray[4] = WIS;
StatArray[5] = CHA;
//Sorting temporary Stat Array
sort(StatArray, StatArray + 6 );
while(!InFile.eof())
{
InFile>>Strength>>Dexterity>>Constitution>>Inteligence>>Wisdom>>Charisma;
if(Strength==StatArray[5] and Dexterity==StatArray[4] and Constitution==StatArray[3] and Inteligence==StatArray[2] and Wisdom==StatArray[1] and Charisma==StatArray[0])
{
Match=true;
break;
}
}
if(Match==false)
{
OutFile << StatArray[5] << "\t" << StatArray[4] << "\t" << StatArray[3] << "\t" << StatArray[2] << "\t" << StatArray[1] << "\t" << StatArray[0] << endl;
}
}
}
}
}
}
}
return 0;
}
int main()
{
GenerateDistinctStatArrays();
}
Coder, only going from 7 to 8 for testing. saw no reason to keep generating the 2.9MILLION possible combinations when the duplicate issue arises in the range of 7-8. For my end purposes, it doesn't matter where the numbers are regarding the various attributes. I just want all Distinct combinations of 7-18 across 6 variables. as far as i'm concerned in this project
7 8 9 10 11
11 10 9 8 7
9 11 8 10 7
are all the same. I've just sorted them to make it easier to weed out the duplicates. As for why Strength is tied to StatArray[5], I wanted the biggest number at the front and just didn't spend the time to look at the sort function to figure out how to make it do a reverse sort. I just did it manually when printing to the file.
vsingh,
I'm not sure that I can put all this data into an array. I tried using a struct to do so, but when i tried to make an array of structs with 2.9 Million entries (12^6 entries), my computer laughed at me and said no. I can write them all to a file in about 30 seconds, but thought my method would eliminate a step from writing them all to a file, then going through them to eliminate duplicates. sounded easier to just not write the array to the file if it was already in there. Looking at std::remove_if I'd have to have all my data sorted for it to work. currently i just have it sorted across the 6 variables. It's been a long time (10 years) since my last C++ programming class and things are a bit fuzzy. I know they're possible, just can't remember how to do it. Much like sorting across an array of arrays like this issue would be.
Any other input of guidance would be appreciated on how to accomplish this coding conundrum is most welcome.
well then you can't compare it like that. Instead make another array like StatArray, read and sort the data and then compare the two arrays with equal:
So instead of reading into 6 different variables, read them into an array instead and compare that way? How is it different? Other than by using the std:equal function? Ultimately you're comparing StatArray[0] to StatArray2[0] instead of StatArray[0] to Strength (which would have the same value as StatArray[0]). I'll give it a shot, just don't understand why my way doesn't work. I'm not familiar with all these std:: things either. Never heard of them during my c++ programming class 10 years ago.
The problem is that you aren't resetting the stream position each time through the loop. So instead of comparing each combination against the whole file, you're only comparing it against the rest of the file from where you last left off. Once you reach the end of the file, you'll never find a match.
To return to the beginning of the file you could change this: