Replacing C with C++

Question from professor:
Write a C++ program that will create a new output file that is a replica of the input file but has all occurrences of C replaced with C++. The names of the input and output files are read from user.
Note: in this example we are processing files that consist of alpha numeric values and the numbers are not used in arithmetic operations. Also, we are not ignoring spaces.
Therefore,
Input: input file name and location read from user
Output: output file name and location read from user
Design: read one character at time, check it, if it is C write C++ to the new file, if not write it to the new file without changing it.
Use get function not cin.
Make sure to include explanations and reasons.

I'm not quite sure how to properly use the "getline" how she wants. This is what I have, what should I change to make this program work:

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
  //Lab: 17
//Name: Kristina Olson
//Class: CS155-01
#include <fstream>//library for files
#include <iostream>
#include <cstdlib>//for exits
using namespace std;
int main()
{
	string infile, outfile;
	ifstream fin;
	ofstream fout;
	std::cout << "Enter input file name-no double quotes required." << endl;
	cin >> infile;
	std::cout << "Enter output file name" << endl;
	cin >> outfile;
	fin.open(infile);//opens file
	fout.open(outfile);//opens file
	if (fin.fail())//for if file fails
	{
		cout << "Input failed to open." << endl;
		exit(1);
	}
	if (fout.fail())//for if file fails
	{
		cout << "Output failed to open." << endl;
		exit(1);
	}
	char next;
	fin.get(next);
	while (!fin.eof())//checks to see if C or C++
	{
		if (next == 'C')
			fout << "C++";
		else fout << next;
		fin.get(next);
	}
	fin.close();//closes file
	fout.close();//closes file
	return 0;
}
Use get function not cin ... I'm not quite sure how to properly use the "getline" how she wants


Given the inconsistencies in the OP, I shall be assuming you/your instructor meant getline() and not get(). With just get() words like "Complex" in the input file would appear as "C++omplex" in the output file and the two things one needs to be aware of in this program are: (a) words like "Complex" should NOT become "C++omplex" but (b) strings like "some programming languages (such as C)" should become "some programming languages (such as C++)". With that in mind, here's the program:

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

void find_and_replace(string& source, string const& to_replace, string const& replace_with)
{
    for(unsigned int i = 0; (i = source.find(to_replace, i)) != string::npos;)
    {
            if(i <= source.size() - 1 && ((source[i+1] == ' ')||source[i+1] == ')'))

                source.replace(i, to_replace.length(), replace_with);
                i += replace_with.length();
    }
}

int main()
{
    fstream infile("F:\\test.txt");
    ofstream outfile("F:\\test0.txt");

    if(infile.is_open())
    {
        string line;
        while (getline(infile, line))
        {
            find_and_replace(line, "C", "C++");
            outfile<<line;
        }
    }
}

Input File
C was originally developed by Dennis Ritchie between 1969 and 1973 at Bell Labs, and used to re-implement the Unix operating system. It has since become one of the most widely used programming languages of all time, with C compilers from various vendors available for the majority of existing computer architectures and operating C systems. Complex C has been standardized by the American National Standards Institute (ANSI) since 1989 (see ANSI C) and subsequently by the International Organization for Standardization (ISO).
Output File
C++ was originally developed by Dennis Ritchie between 1969 and 1973 at Bell Labs, and used to re-implement the Unix operating system. It has since become one of the most widely used programming languages of all time, with C++ compilers from various vendors available for the majority of existing computer architectures and operating C++ systems. Complex C++ has been standardized by the American National Standards Institute (ANSI) since 1989 (see ANSI C++) and subsequently by the International Organization for Standardization (ISO).
Given the inconsistencies in the OP, I shall be assuming you/your instructor meant getline() and not get().

That seems a an unlikely interpretation given that the OP states,
Design: read one character at time,

The code in the opening post appears to work, though I'd suggest the loop here
1
2
3
4
5
6
7
8
9
    char next;
    fin.get(next);
    while (!fin.eof())//checks to see if C or C++
    {
        if (next == 'C')
            fout << "C++";
        else fout << next;
            fin.get(next);
    }
could be better written as
1
2
3
4
5
6
7
8
    char next;
    while (fin.get(next))  // replaces C with C++
    { 
        if (next == 'C')
            fout << "C++";
        else 
            fout << next;
    }


It's best to avoid looping on eof(), though in this case it may work.

Having said that, the code does in fact work as specified. I'm not sure there are any issues to be fixed.
@Chervil: try running the OP code with my input text, Complex becomes C++omplex, etc. As long as the instructor insists on using get() and reading one char at a time there's no way around it and it just becomes yet another poorly designed assignment
One option would be to use peek() and replace it only if the next char fulfills a certain condition -for example space.

1
2
3
4
5
6
7
8
9
10
11
    char next;
    while (fin.get(next))  // replaces C with C++
    { 
        if (next == 'C' && fin.peek() == ' ' )
           fout << "C++";
        else 
            fout << next;
    }

	
Good, just one small edit:

1
2
3

if (next == 'C' && (fin.peek() == '  '  || fin.peek() == ')')


To take into a/c text like: "some programming languages (such as C)"
closed account (E0p9LyTq)
@gunnerfunner,

You are "forgetting" about a lot of punctuation/non-alphanumeric characters that needs to be accounted for.

It might be easier to use the C library <cctype> isblank()/isspace() and ispunct() functions when checking what the next character will be.
Yes, you're right ... good point
Thank you everyone for your input. I understand what I thought I was doing wrong and was able to fix my mistake.
Topic archived. No new replies allowed.