ehm it isn't a hello world program, but I get a windows error message. Here is the code maybe it will be helpfull. I admit that the code is a bit messy and th comment s are in dutch ( my native language) as are the variables. The code has to open 2 files with in 1 city-codes and in the other city names. Then the program has to compare them and give for every city code the number of city names that are complement to each other.
Code:
#include <iostream>
#include <fstream>
#include <string>
//#include <ofstream>
using namespace std;
const int MAX=3, MAX_P=15;
int main()
{
int aantal_codes, teller=0, k=0;
char stop;
//de codes opslaan en openen
ifstream code;
code.open("code.in");
if (! code.is_open())
{
cerr << "Fout: bestand niet kunnen openen."<<endl;
cin>>stop;
exit(1);
}
code >> aantal_codes;
cout<< "er zijn zoveel codes: "<<aantal_codes<<endl;
char code1[MAX][aantal_codes];
int aantal[aantal_codes], aantal_chars[aantal_codes];
teller=0;
while (aantal_codes != teller)
{
k=0;
char *cstr ;
string code2;
code>>code2;
aantal_chars[teller]=code2.size();
cstr = new char [code2.size()+1];
strcpy (cstr, code2.c_str());
while (k != code2.size())
{
code1[k][teller]= cstr[k];
k++;
}
teller++;
}
//de plaatsen openen en opslaan in array
int aantal_plaatsen;
//het ophalen uit het bestand
ifstream plaats;
plaats.open("plaats.in");
if (! plaats.is_open())
{
cerr << "Fout: bestand niet kunnen openen."<<endl;
cin>>stop;
exit(1);
}
plaats >> aantal_plaatsen;
cout<< "er zijn zoveel plaatsen: "<<aantal_plaatsen<<endl;
//opslaan in een array
char plaats1[MAX_P][aantal_plaatsen];
int aantal_p[aantal_plaatsen], aantal_chars_p[aantal_plaatsen];
teller=0;
while (aantal_plaatsen != teller)
{
k=0;
char *cstr_p;
string code_p;
plaats>>code_p;
aantal_chars_p[teller]=code_p.size();
cstr_p = new char [code_p.size()+1];
strcpy (cstr_p, code_p.c_str());
while (k != code_p.size())
{
plaats1[k][teller]= cstr_p[k];
k++;
}
teller++;
}
//cout<<aantal_chars[1];
//array om de score voor de codes bij te houden
int score_codes[aantal_codes];
//het vergelijken van de plaatsnamen met de codes
teller=0;
int tel_letter=2;
//cout<<"oke1 ";
while (teller!= aantal_codes)
{
score_codes[teller]=0;
//cout<<"oke2 "<<endl;
int tel=0, tel_n=0, tel_k=0, rem=0;
while (tel!=aantal_plaatsen)
{
//cout<<"oke3 ";
int l=aantal_chars_p[tel];
//cout<<l;
if (code1[0][teller]==plaats1[0][tel])
{//cout<<"oke4 ";
if (aantal_chars[teller]==1)
{score_codes[teller]++;/*cout<<"oke6 ";*/}
else if((aantal_chars[teller]==2 && code1[1][teller]==plaats1[1][tel])||code1[1][teller]==plaats1[aantal_chars_p[teller]][tel])
{score_codes[teller]++;/*cout<<"oke7 "*/;}
else if(aantal_chars[teller]==3 && code1[1][teller]==plaats1[1][tel])
{
/*cout<<"oke15 "*/;tel_letter=2;
while (tel_letter!=aantal_chars_p[tel] && rem==0)
{
//cout<<"oke5 ";
if (plaats1[tel_letter][tel]==code1[2][teller])
{score_codes[teller]++;rem=1;tel_letter++;}
else
{tel_letter++;}
}
rem=0;
}
else if(aantal_chars[teller]==3 && code1[2][teller]==plaats1[l-1][tel])
{
tel_letter=1;/*cout<<"oke9 ";*/
while (tel_k!=aantal_chars_p[tel]-1 && rem==0)
{
//cout<<"oke10";
if (plaats1[tel_letter][teller]==code1[1][teller])
{score_codes[teller]++;rem=1;}
else
{tel_letter++; tel_k++;}
}
rem=0;
}
}tel++;
}teller++;
}
//de codes weergeven
teller=0;
while (aantal_codes != teller)
{
k=0;
while (k != aantal_chars[teller])
{
cout<<code1[k][teller];
k++;
}
cout<<endl;
teller++;
}
//de plaatsen weergeven
teller=0;
while (aantal_plaatsen != teller)
{
k=0;
while (k != aantal_chars_p[teller])
{
cout<<plaats1[k][teller];
k++;
}
cout<<endl;
teller++;
}
//code_scores weergeven
teller=0;
while (aantal_codes != teller)
{
cout<<score_codes[teller];
cout<<endl;
teller++;
}
ofstream score;
score.open("code.uit");
if (! score.is_open())
{
cerr << "Fout: bestand niet kunnen openen."<<endl;
cin>>stop;
exit(1);
}
teller=0;
while (aantal_codes != teller)
{
score<<score_codes[teller];
teller++;
}
cout<<"code.uit gemaakt en vol met gegevens";
cin>>stop;
return(0);
}
If you have some helpfull tips that will help me to make my code more readable and/or faster/more flexible don't hold back.
thanks for all