Program to replace letters, multiple times.

Hello,

I've written a program that asks the user what letter to replace, and what to replace it with.

It runs, and works as it should the first time, however it will only ever save one round of swapping. i.e. when I run it again the letter I originally swapped just reverts back to the original.

I have no idea why it does this, and was wondering if anyone could see what I've done wrong.

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
#include <iostream>
#include <fstream>
#include <algorithm>
#include <ctype.h>
#include <cstdio>

using namespace std;

int main()
{
	char RunAgain = 'y';
	do// do while loop to re-run the program 
	{
		ifstream inputFile("swapped.txt");
		ofstream outputFile("final_decrypted.txt");


		char  x;
		char  y;
		char letter;


		cout << "Enter the character that you want to replace" << endl;
		cin >> x;
		cout << "Enter the character that you want to replace it with" << endl;
		cin >> y;



		if (inputFile.is_open())
		{

			while (inputFile.good())
			{
				inputFile.get(letter);
				if (letter == x)
				{
					outputFile << y;
				}

				else
				{
					outputFile << letter;
				}
			}


		cout << "Enter y to run this program again. Enter any other letter to quit.  "; 
		cin >> RunAgain; 
		}
	}

	while (RunAgain == 'y');

	return 0;
}


Alternatively, if anyone knows a simpler way to replace one letter with another in a text file that would be awesome. Any help would be appreciated. Cheers.
Last edited on
Each time your do-while loop iterates, you re-read the contents of swapped.txt. Since you don't update swapped.txt with the results of the swapping (you write them instead to final_decrypted.txt), it will always have the same contents at at did at the start of the first run.
Last edited on
Ok, thanks very much, that makes sense.

So do you think I should just remove the do-while loop, and find another way to give an option to re-run the program?

Or is there a way to fix it but still keeping the do-while loop?
You could read the file into a string once at the beginning of the program before the loop, and do your modifications on the string.
I don't really know how to do that. Thanks though.
1
2
3
ifstream inputFile("swapped.txt");
string data;
inputFile >> data;


boom.
I've tried to add it to my code but it doesn't run. Visual studio shows '>>' as an error.

It tells me that 'No operator matches these operands'.
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
#include <iostream>
#include <fstream>
#include <cstdio>

int main()
{
    const char* const file_name = "swapped.txt" ;
    const char* const temp_file_name = "final_decrypted.txt" ;

    char RunAgain = 'y';

    while( (RunAgain=='y') || (RunAgain=='Y') )
    {
        char x ;
        std::cout << "Enter the character that you want to replace: " ;
        std::cin >> x;

        char y ;
        std::cout << "Enter the character that you want to replace it with: " ;
        std::cin >> y;

        {
            std::ifstream ifile(file_name) ; // open input file for reading
            std::ofstream ofile(temp_file_name) ; // open temp file for writing

            char c ;
            while( ifile.get(c) ) // for every character (including white space)  in ifile
                ofile.put( c == x ? y : c ) ; // write (replaced) character to ofile
        }

        // copy contents of temp file to the original file
        { std::ofstream(file_name) << std::ifstream(temp_file_name).rdbuf() ; }

        std::remove(temp_file_name) ; // delete the temp file

        std::cout << "Enter y to run this program again. Enter any other letter to quit: " ;
    	std::cin >> RunAgain;
    }

    // examine the contents of the modified file
    std::cout << "\n\n\n" << std::ifstream(file_name).rdbuf() ;
}

http://coliru.stacked-crooked.com/a/c92df55dab9443c0
I've run it and it no longer reverts to the original each time, which allows me to save the results of the letter swap. Thanks very much.

I was wondering, what is the meaning/purpose of the .rdbuf() at the end of the ifstream (temp_file_name)?
> what is the meaning/purpose of the .rdbuf()

Read (the opening few paragraphs of) this article first, to understand what a stream buffer is: http://www.angelikalanger.com/IOStreams/Excerpt/excerpt.htm

rdbuf() returns (a pointer to) the stream buffer - in this case a std::filebuf.

When we use << with an output stream and a stream buffer, every character from the stream buffer is written to the output stream. (This is faster than our reading characters one by one, and then writing out each character that was read.)
Topic archived. No new replies allowed.