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.
#include <iostream>
#include <fstream>
#include <string>
#include <exception>
usingnamespace 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";
}
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
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).
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
//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