Runtime error when i run

My code compiles and runs but if fileName != "" I recieve a runtime error. Anyone have any clues as to why?

Here is my code:

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
string a = "fileContainingEmails.txt";
string b = "copyPasteMyEmails.txt";

//open input file
ifstream fin;
string fileName;
cout<<"Enter input filename [default: fileContainingEmails.txt]: ";
getline(cin,fileName);
fin.open(fileName.c_str());
if (!fin.good()) throw, "I/O error";

//open output file
ofstream fout;
string fileName1;
cout<<"Enter input filename [default: copyPasteMyEmails.txt]: ";
getline(cin, fileName1);
fout.open(fileName.c_str());
if (!fin.good()) throw, "I/O error";

//if for output
if (fileName!="")
{
cout<<fileName<<" will be used for output."<<endl;
}
else
cout<<a<<" will be used for output."<<endl;




return 0;
}






Last edited on
You have an extra comma after both 'throw' instructions, which makes the compiler believe you want to throw nothing.
In addition, I would recommend that you actually catch the exceptions you throw by adding a try {...} catch (const char* str) {...} block around your code.

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
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  try
  {
    string a = "fileContainingEmails.txt";
    string b = "copyPasteMyEmails.txt";

//open input file
    ifstream fin;
    string fileName;
    cout << "Enter input filename [default: fileContainingEmails.txt]: ";
    getline (cin, fileName);
    fin.open (fileName.c_str ());
    if (!fin.good ()) throw "I/O error";

//open output file
    ofstream fout;
    string fileName1;
    cout << "Enter input filename [default: copyPasteMyEmails.txt]: ";
    getline (cin, fileName1);
    fout.open (fileName.c_str ());
    if (!fin.good ()) throw "I/O error";

//if for output
    if (fileName != "") cout << fileName << " will be used for output." << endl;
    else cout << a << " will be used for output." << endl;

  }
  catch (char const *str)
  {
    cout << str << std::endl;
  }
  return 0;

}
Topic archived. No new replies allowed.