Program compiles and runs, but crashes at output!

Just about have this finished, but the end result should take the input file, parse the emails, and create or aappend to an existing file the list of emails from from the initial document. They also need to be alphabetized, and checked for duplicates - regardless of case. Everything seemed to work fine until I added the coding to create the output file. What gives? I've noticed that it will still print the results to the console, but it crashes before creating the new output file, so my guess is that the issue lies somewhere near the very end!

Also, I realize that if I knew how to use a debugger, life would be easier! I'll have to pick that up over the summer for sure!

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

#include <cctype>

class toUpper {public: char operator()(char c) const {return toupper(c);}};

bool isValidEmailCharacter(char c)
{
  if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <='9') ||
      (c == '.') || (c == '-') || (c == '+'))
    return true;
  else
    return false;
}

int main()
{
  // declare constants
  const string defaultInput = "fileContainingEmails.txt";
  const string defaultOutput = "copyPasteMyEmails.txt";

  // declare variables
  string inputFileName;
  string outputFileName;

  ifstream fin;
  ofstream fout;

  // array list
  const int MAX_EMAILS = 1000;
  int nEmails = 0;
  string email[MAX_EMAILS];

  string line;
  int s = 0;
  int e = 0;
  bool hasDot = false;
  bool emailTaken = false;
  string emailX;
  string emailA;
  string emailB;
  string temp;  

  // prompt for input filename
  cout << "Enter input filename [default: " << defaultInput << " ]: ";
  getline(cin, inputFileName);

  if (inputFileName == "")
  {
    inputFileName = defaultInput;

    // prompt for output filename
    cout << "Enter output filename [default: " << defaultOutput << " ]: ";
    getline(cin, outputFileName);

    if (outputFileName == "")
      outputFileName = defaultOutput;
  }
  else
  {
    cout << "Enter output filename [default: " << inputFileName << " ]: ";
    getline(cin, outputFileName);

    if (outputFileName == "")
      outputFileName = inputFileName;    
  }

  cout << endl;
  cout << "Input file = " << inputFileName << endl;
  cout << "Output file = " << outputFileName << endl << endl;

  fin.open(inputFileName.c_str());
  if (!fin.good()) throw "I/O error";

  while (fin.good())
  {
    getline(fin, line);
    
    for (int i = 0; i < line.length(); i++)
    {
      if (line[i] == '@')
      {
        s = i;
            
        while (s > 0 && isValidEmailCharacter(line[s-1]))
        {
          s--;
        }

        e = i;
        hasDot = false;

        while (e < line.length() && isValidEmailCharacter(line[e+1]))
        {
          if (line[e] == '.')

          hasDot = true;
          e++;
        }

        if (s < i && e > i && hasDot)
        {
          emailX = line.substr(s, e-s +1);
 
          emailTaken = false;

          emailA = emailX;
          transform(emailA.begin(), emailA.end(), emailA.begin(), toUpper());

          for (int i = 0; i < nEmails; i++)
          {
            emailB = email[i];
            transform(emailB.begin(), emailB.end(), emailB.begin(), toUpper());
            
            if (emailA == emailB) emailTaken = true;
          }

          if (!emailTaken && nEmails < MAX_EMAILS)
          {
            email[nEmails++] = emailX;

            cout << emailX << endl;
          }
        }
      } 
    }
  }
  
  cout << endl;
  
  fin.close();

  for (int i = 0; i < nEmails; i++)
  {
    for (int j = 0; j , nEmails; j++)
    {
      emailA = email[i];
      emailB = email[j];

      transform(emailA.begin(), emailA.end(), emailA.begin(), toUpper());
      transform(emailB.begin(), emailB.end(), emailB.begin(), toUpper());

      if (emailA < emailB)
      {
        temp = email[i];
        email[i] = email[j];
        email[j] = temp;
      }
    }
  }

  if (nEmails > 0)
  {
    fout.open(outputFileName.c_str());
    if (!fout.good()) throw "I/O error";

    for (int i = 0; i < nEmails; i++)
    {
      fout << email[i];
      if ((i+1) < nEmails)
        fout << "; ";
    }

    fout.close();
  }
          

  return 0;
}
Last edited on
nevermind, i figured it out! mistake was at line 140! XD
Topic archived. No new replies allowed.