Help with arrays and filestreams

Hey! I'm a beginner in C++ and have been trying to get this code working, to no avail. It is suppose to read in text from a file and replace as many letters as you want with other letters that you also choose. However, it isn't working and every time I solve one problem another arises. Any help as to how to fix this would be amazing. Thanks!

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <fstream.h>
using namespace std;

int main ()
{

	ofstream outfile;
	ifstream infile;
	
	int scramAmount, i;
	char fileTrans;
	char temp;
	
	cout << "How many letters do you wish to scramble?" << endl;
	cin >> scramAmount; 
	
	char letter[2][scramAmount];
	
	for (i = 0; i < scramAmount; i++)
	{
		cout << "Letter to scramble: ";
		cin >> letter[0][i];
		cout << "Replace with: ";
		cin >> letter[1][i];
	}
	
	//copies text from the original file into a backup
	infile.open("scramble.txt");
	outfile.open("scrambleStore.txt");
	
	while(!infile.eof())
	{
		infile.get(fileTrans);
		if(!infile.eof())
		{
			outfile.put(fileTrans);
		}
	}
	
	infile.close();
	outfile.close();
	
	infile.open("scrambleStore.txt");
	outfile.open("scramble.txt");
	while(!infile.eof())
	{
		infile.get(fileTrans);
		
		for(i = 0; i < scramAmount; i++)
		{	
			if(fileTrans == letter[0][i])
			{
				temp = letter[1][i];
			}
			else 
			{
				temp = fileTrans;
			}
		}
		outfile.put(temp);
	}
	infile.close();
	outfile.close();

    return 0;
}
Last edited on
Your char array letter should throw an error when you try to compile
That is because scramtest is not a const value. Therefor it will throw an error because you are not dynamically allocating the array.
I see, and I'd like to say that solved my problem but nothing changed. I had no errors when compiling previously. I'm not exactly sure why I'm not getting an error, but I think my problem is regarding when I transfer filed back into the document. I think something is off my loops, but I can't figure out what.
whatever compiler you are using should not have let you use an int to define the size of an array.
outfile.put(temp);change this to outfile << temp;
Arrays aren't suitable for tasks where the number of the objects is only known at runtime, you should have started with vectors. However, this particular task is begging for map (you're *mapping* characters to other characters).

try:

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 <iostream>
#include <fstream>
#include <map>
using namespace std;
int main ()
{
        cout << "How many letters do you wish to scramble?\n";
        int scramAmount;
        cin >> scramAmount; 

        map<char, char> letter;
        for (int i = 0; i < scramAmount; ++i)
        {
            cout << "Letter to scramble: ";
            char c;
            cin >> c;

            cout << "Replace with: ";
            cin >> letter[c];
        }
    
        //copies text from the original file into a backup
        {
             ifstream infile("scramble.txt");
             ofstream outfile("scrambleStore.txt");
             outfile << infile.rdbuf();
        }

        // copies from the backup into original, scrambling
        ifstream infile("scrambleStore.txt");
        ofstream outfile("scramble.txt");
        char fileTrans;
        while(infile.get(fileTrans))
        {
            if(letter.find(fileTrans) != letter.end())
                outfile.put(letter[fileTrans]);
            else
                outfile.put(fileTrans);
        }
}

Thanks for all the help. I'm stilling having one or two problems, but I can work them out. Thanks again!
Topic archived. No new replies allowed.