okay, i usually create beg programs and they often compile. for some reason, this one is not compiling at all. i cant seem to figure out whats wrong.

my objective is to prompt user to open a file named "data.txt"
have HELLO WORLD printed in a scrambled version to a file named "secret.txt"
and also have it printed to the console screen.

but first and foremost, my bigger problem is not having the program compile properly. any have any suggestions?

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

int main()
{
  ifstream fin;
  fin.open("data.txt");
  if (!fin.good()) throw "I/O error";
  
  string fileName;
  cout << "What file do you want to use for input? ";
  getline(cin, fileName);
  fin.open(fileName.c_str());
  
  ofstream fout;
  fout.open("secret.txt", ios::app);
  if (!fin.good()) throw "I/O error";
  
  fout << " " << endl;
  fout.close();
  
  // encode string s by adding 1 to the ASCII code of each character
  string s = "Hello, World"; // an example
  for (int i = 0; i < s.length(); i++) // for each char in the string...
  s[i]++; // bump the ASCII code by 1

  
  while (true)
  {
    if (!fin.good()) break;
    
    string lineFromFile;
    getline(fin, lineFromFile);
    cout << lineFromFile << endl;
  } //  while
  
  fin.close();
  cin.ignore();
  cin.get();
  return 0;
}
Member Avatar for jencas

Any error messages from the compiler? I only get a warning, which can be eliminated by changing

for (int i = 0; i < s.length(); i++) // for each char in the string...

to

for (unsigned int i = 0; i < s.length(); i++) // for each char in the string...
Member Avatar for jencas
while (true)
  {
    if (!fin.good()) break;
    
    string lineFromFile;
    getline(fin, lineFromFile);
    cout << lineFromFile << endl;
  } //  while

Why not:

string lineFromFile;
  while (getline(fin, lineFromFile))
  {
    cout << lineFromFile << endl;
   } //  while

okay, after a lot of here and there. i finally got my previous program to work.

i copy and pasted my current program below. if someone can run and it see what my issue is, itd be truly helpful.

user input: data.txt

my problem now: how to encode the output on console and text file "Secret"

the ASCII coding below, i feel, is in the wrong spot. i've tried every way i could think of to put it elsewhere, but the problem is still the same. any suggestions??

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

int main()
{
  ifstream fin;
  
  string fileName;
  cout << "What file do you want to use for input? ";
  getline(cin, fileName);
  fin.open(fileName.c_str());
  
  ofstream fout;
  fout.open("secret.txt", ios::app);
  if( !fin.is_open() )
    {
    string s = "Hello, World"; // an example
    for (int i = 0; i < s.length(); i++) // for each char in the string...
    s[i]++; // bump the ASCII code by 1                 
    
    fout << "Can't open the file\n";
    fout.close();
    return 1;
    }
  
  string line;
  while (getline(fin, line))
  {
    

    cout << line << endl;
    
    fout << line << endl;
    fout.close();
  } //  while
Member Avatar for jencas
fout.close();

surely should be outside the while loop

any idea how i can use the encoder in this program?! so that my input texts will come out as scrambled letters?

Member Avatar for iamthwee

Yes introduce random numbers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.