This might be a case where the indentation is misleading you. Here is the code indented to match the actual block structure:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
//Create an input file called employee.txt with the following data:
//SS van der Merwe;PJ Ferreira;HW du Plessis;DF Kodisang;AA Papoudopolous;G Mhlanga;TRF Schoeman;LJ Marais-Le Roux;CG Roux;B Nicholaidis;TT Tshabalala;RV Mississipi;
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int
main()
{
ifstream fin;
fin.open("EmployeeNames.txt");
if (fin.fail()) {
cout << " Error in opening Input file...." << endl;
exit(1);
} else {
cout << " Opening Input file ..." << endl;
cout << " Reading from Input file ... please wait... " << endl;
}
ofstream fout;
fout.open("EmailIDs.txt"); //ios::app)
if (fout.fail()) {
cout << "Error in creating output file..." << endl;
exit(1);
} else {
cout << " Creating Email IDs from employee names....please wait." << endl;
}
char nextSymbol, semiColon = ';';
const int limit = 8;
int i = 0, count = 0;
while (fin >> nextSymbol) {
for (i = 0; i <= limit; i++) // to get the first 8 characters
{
cout << nextSymbol;
fout.put(nextSymbol);
i = count;
count++;
}
}
if (i == limit) {
cout << ";";
fout.put(semiColon);
}
if (nextSymbol == isalpha(1) || nextSymbol == isspace(1)) // checking if next char is an empty space or alphanumeric
{
count += 1; // skip it and move to the next
i -= 1; // ensuring limit of 8 characteres is maintained
// copy the next symbol that is not alphanumeric or a space
}
if (nextSymbol == semiColon) // if semi colon is encountered create email ID
{
// create newid
count = limit;
} else {
}
fin.close();
fout.close();
return 0;
}
|
The loop at line 35 reads the input file 1 character at a time.
The loop at lines 36-42 executes for each character. The first time, it prints the first character 8 times. But upon entering the loop the second time, count == 9 because it wasn't reset from the first time. At line 40, i gets set to 9, which means the loop executes just once. This behavior continues for each pass through the loop at line 35: the loop at lines 36-42 executes just once.
Eventually you get to line 45. By now i is some large value, because it kept getting set to count, which kept getting incremented. Whatever it is, it isn't limit.
At line 50 are you trying to see if nextSymbol is an alpha or a space? That would be
if (isalpha(nextSymbol) || isspace(nextSymbol))
Your description of the problem isn't completely clear to me, especially considering the comment at the beginning of the code. Could you post an example of the input and expected output?