counting and output, with arrays and functions
Ok, yet another question from me. I am having troubles now with my program counting runes from a text file (I will show a example of that file) and, if I uncomment the line calling my output function, it will output what ever is in the array, but when it hits the bottom I get a "TodesRunes.exe has encountered a problem and needs to close. We are sorry for the inconvenience."
Also, since it does output before it crashes, I noticed that it is not counting the items from the file.
The line of T's was to see if it was dying before or after that.
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void loadfile(ifstream& file);
void counter(int aiRunes1[], string sRuneNames[], int iNumOfRunes, ifstream& file);
void output(int aiRunes1[], string sRuneNames[], int iNumOfRunes);
int main ()
{
cout<<"Welcome to TodesRunes 1.1!"<<endl;
const int iNumOfRunes = 33;
int aiRunes1[iNumOfRunes] = {0};
string sRuneNames[iNumOfRunes] = {"El", "Eld", "Tir", "Nef", "Eth", "Ith",
"Tal", "Ral", "Ort", "Thul", "Amn", "Sol",
"Shael", "Dol", "Hel", "Io", "Lum", "Ko",
"Fal", "Lem", "Pul", "Um", "Mal", "Ist",
"Gul", "Vex", "Ohm", "Lo", "Sur", "Ber",
"Jah", "Cham", "Zod"};
ifstream file; //ATMA dump
loadfile(file); //loads the ATMA dump
counter(aiRunes1, sRuneNames, iNumOfRunes, file);
output(aiRunes1, sRuneNames, iNumOfRunes);
cout<<"TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT";
cin.get();
}
void loadfile(ifstream& file)
{
file.open( "runes.txt");
if (!file.is_open())
file.open( "rune.txt");;
while(!file.is_open())
{
string filename;
cout<<"TodesRunes could not find your ATMA dump,"<<endl
<<"please specify the path to the txt file."<<endl;
getline(cin,filename);
file.open(filename.c_str());
}
}
void counter(int aiRunes1[], string sRuneNames[], int iNumOfRunes, ifstream& file)
{
int iCounter = 0;
string sLine;
while (getline( file , sLine ))
{
if (sLine.empty() || (sLine == "\r")) continue;
int colon_pos = sLine.find(':');
string sRune = sLine.substr(colon_pos+2,sLine.size()-colon_pos-7);
while(iCounter<=iNumOfRunes)
{
if(sRune==sRuneNames[iCounter])
{
aiRunes1[iCounter]++;
iCounter=iNumOfRunes+1;
}
else
{
iCounter++;
}
}
}
}
void output(int aiRunes1[], string sRuneNames[], int iNumOfRunes)
{
int iCounter = 0;
while(iCounter<=iNumOfRunes)
{
cout<<sRuneNames[iCounter]<<": "<<aiRunes1[iCounter]<<endl;
iCounter++;
}
}
runes.txt
Code:
12: Tir Rune
13: Eth Rune
14: El Rune
15: Eth Rune
16: Amn Rune
17: Ral Rune
18: Tir Rune
19: El Rune
20: Ral Rune
21: El Rune
22: Ort Rune
23: Eth Rune
The file I am using is actually ~26mb, in my last version of the program (before the rewrite) it took ~5 seconds to load the file in my program and parse it, this program it looks like it is done instantly.