wrong spelling??

it complies with no error but after the user input of file name I get this error

" terminate called after throwing an instance of 'char const*' "

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
int main()
{
  //Data
  string usersFile; //stores user input
  int i; //loop index
  string codedMsg;

  // get user file name
  cout <<"Input file name for endcoding agent. " <<endl <<endl;
  getline(cin, usersFile);

  // open input file
  ifstream fin; //
  fin.open (usersFile.c_str()); //
  if (!fin.good()) throw "I/O error"; //

  // open output file
  ofstream fout;
  fout.open ("secret.txt");
  if (!fout.good()) throw "I/O error" ; //

  //input line
  while (fin.good())
  {
    getline(fin, usersFile);

    // encode string s by adding 1 to the ASCII code of each character
    for (i = 0; i < codedMsg.length(); i=i+1)
    {
    codedMsg[i] = codedMsg[i] + 1; // bump the ASCII code by 1
    }//for i 
  
    //the result
    cout << codedMsg <<endl;
    fout << codedMsg <<endl;
  }

  //finished
  cout <<"ALL MESSAGES ENCODED " <<endl;

  //close output
  fout.close();

  //close input
  fin.close();

}//main 
One of your throws must have happened, probably the input file.
If you are spelling the input filename correctly, then maybe it's not in the correct directory.

It should also be noted that codedMsg never gets assigned anything, so it is always empty.

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

int main()
{
  string line;

  // get user file name
  cout << "Input file name for endcoding agent.\n\n";
  getline(cin, line);

  // open input file
  ifstream fin(line);
  if (!fin) throw invalid_argument("Cannot open input file");

  // open output file
  ofstream fout("secret.txt");
  if (!fout) throw invalid_argument("Cannot open output file");

  //input line
  while (getline(fin, line))
  {
    for (size_t i = 0; i < line.length(); ++i)
        ++line[i];
    cout << line << '\n';
    fout << line << '\n';
  }

  cout << "ALL MESSAGES ENCODED\n";
}

Last edited on
how do I make sure it is in the right directory because I am spelling it correctly

and for the codedMsg, this is better right?

//input line
while (fin.good())
{
getline(fin, usersFile);

//encode string s by adding 1 to the ASCII code of each character
for (i = 0; i < usersFile.length(); i=i+1)
{
codedMsg[i] = usersFile[i] + 1; // bump the ASCII code by 1
}//for i
Last edited on
how do I make sure it is in the right directory

When it's in the right directory, it will work. :-)

and for the codedMsg, this is better right?

Not quite. codedMsg has a length of 0 since it is empty. So you can't access codedMsg[i] since they don't exist. Just change all the 'usersFile' (which doesn't make sense anyway) to 'codedMsg' in that snippet.

And you need to put the getline in the while condition to properly control the loop (as I've shown).
Last edited on
LMAO ur right having usersFile there did make no sense, i didn't get what you meant at first but when it clicked, it clicked.

but where did
"size_t" come from and what does it do
and why the "++line[1];
size_t is an alias for one of the basic unsigned integer types (such as unsigned long).
It is commonly used for indices.

++line[i] adds 1 (increments) the character just like line[i] = line[i] + 1.
Have you really not seen ++ before? It's in the name C++! ;-)
It's commonly used in for loops to increment the index (as I did in the example code).
im still new and learning the terminology of things, with your help I am now able to compile and even open the output file, but the program ends after creating the output file and before encoding any lines
Post your current program (in code tags, of course).
//Data
string usersFile; //stores user input
int i; //loop index
string codedMsg;

// get user file name
cout <<"Input file name for endcoding agent. " <<endl <<endl;
getline(cin, usersFile);

// open input file
ifstream fin; //
fin.open(usersFile.c_str()); //
if (!fin) throw invalid_argument("Cannot open input file"); //

cout <<"Input file connected " <<endl; (DOESNT OUTPUT)
cout <<"Creating output file " <<endl; (DOESNT OUTPUT)

// open output file
ofstream fout;
fout.open ("secret.txt"); (DOES CREATE)
if (!fout) throw invalid_argument("Cannot open output file"); //

cout <<"Output file created " <<endl; (DOESNT OUTPUT)
cout <<"Starting encoding " <<endl; (DOESNT OUTPUT)

//input line
while (true)
{
if (!fin.good()) break;
getline(fin, codedMsg)

// encode string s by adding 1 to the ASCII code of each character
for (i = 0; i < codedMsg.length(); i=i+1)
{
codedMsg[i] = codedMsg[i] + 1; // bump the ASCII code by 1

//output the results
cout << codedMsg <<endl;
fout << codedMsg <<endl;
}//for i

//finished
cout <<"ALL MESSAGES ENCODED " <<endl;

//close output
fout.close();

//close input
fin.close();
Why don't you use more meaningful error messages?
1
2
//http://www.cplusplus.com/reference/cstring/strerror/
if (!fout) throw invalid_argument(strerror(errno)); 
thank you all for the help, i got it to work as it should be
Topic archived. No new replies allowed.