Input/Output problems and question

Hello guys, I've been trying for hours to make this program where it will create a txt file called secret.txt and whatever you write in the console it will print out to that file. But I also added a code where whatever letter you write it will add +1 to it in the ascii. So if you write "Baseball" it would print out to the console and the secret.txt file as "Cbtfcbmm" know what I mean? I've tried and tried, but cant seem to get it to work. Here is my code so far.

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

int main()
{
    string fileName;
    cout << "What file would you like to open?" << endl;
    getline (cin, fileName);
    fin.open(fileName.c_str());
   
    if (!fin.good()) throw "I/O Error";
    
    string encode;
    cout << "What text would you like to encode?" << endl;
    cin >> encode;
    fin.close();
    
    
    for (int i = 0; i < encode.length(); i++)
    encode[i]++;
    
    
    
    cout << "Encoded text: " << encode << endl;
    ofstream fout;
    fout.open(fileName.c_str());
    if (!fout.good()) throw "I/O Error";
    fout.close();
    
}
In your for-loop, you are treating 'encode' as an array. To pick out each character like you want, you need:
 
encode.at(i)++;


EDIT: You'd be correct if you had created a char array and stored the message in it.
Last edited on
Topic archived. No new replies allowed.