how to ENCODE using ASCII coding

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??


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
#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 
The code to modify each line you read from the file should be between lines 30 and 33. The stuff on lines 19 through 21 only happen if the file was not opened.

Good luck!
I tried moving line 19-21 to line 30-33, and im finally making some progress.

my current edited program is below.

my now issue is that it scrambles the words in "DATA.TXT" rather than opening the file of data.txt and scrambling the message within it.

any suggestions?

-kRistine-


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
#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() )
    {
      fout << "Can't open the file\n";
      fout.close();
      return 1;
    }
  
  string s;
  while (getline(fin, s))
  {
    string s = "(data.txt)"; // an example
    for (unsigned int i = 0; i < s.length(); i++) // for each char in the string...
    s[i]++; // bump the ASCII code by 1 
    
    cout << s << endl;
    fout << s << endl;
  } //  while

  fout.close();
  fin.close();
  cin.ignore();
  cin.get();
  return 0;
}


Last edited on
This program will always append "Can't open the file" to "secret.txt". As u are returning form the program on a condition that will be always true if user enters a file name that does not exist in the directory.


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() )
{
fout << "Can't open the file\n";
fout.close();
return 1;
}


Use already created file for opening a file to read for ifstream object.
and also use
system("pause"); instead of
cin.ignore();
cin.get();

if you want to give a pause in the program to see the output on screen.

No no no! system("pause") is evil!

http://www.cplusplus.com/forum/articles/11153/

And I see no reason not to exit if the user inputs a file that doesn't exist since it is used for input.
Topic archived. No new replies allowed.