Tell me how to do : Correct cin,cout from file.

.Write a program that will correct a C++ program that has errors in which operator, << or
>>, it uses with cin and cout. The program replaces each (incorrect) occurrence of
cin <<
with the corrected version
cin >>
and each (incorrect) occurrence of
cout >>
with the corrected version
cout <<


You don't have to write code,but tell me how to start.I need your help.
Open bad text file
Copy entire thing to new file, replacing "cin <<" with "cin >>" and similarly for cout errors.
Delete original bad text file
rename new one to have name of the deleted one
No but the algorithm,not these basic steps...

I made it but it ridiculously buggy...somehow dangerous...

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <iomanip>
#include <cstring>

using std::exit;
using std::cin;
using std::cout;
using std::ostream;
using std::istream;
using std::ifstream;
using std::ofstream;
using std::endl;
using std::pow;
using std::sqrt;
using std::getline;
using std::string;
using std::ios;
using std::setw;

const void correct(ifstream& fin,ofstream& fout);
//Pre : fs1 & fs2 must exist,fs3 optional
//Post : merge from fs1 and fs2 to fs3,then sort

int main()
{
	ifstream fin;
	ofstream fout;

	cout << "AverageLengthFromFile - By Kudy" << endl;

	fin.open("cincoutbef.txt");

	if(fin.fail())
	{
		cout << "Fin.Fail()";
		exit(1);
	}

	fout.open("cincoutaft.txt");

	if(fout.fail())
	{
		cout << "Fout.Fail()";
		exit(1);
	}

	correct(fin,fout);

	fin.close();
	fout.close();

	return 0;
}

const void correct(ifstream& fin,ofstream& fout)
{
	char next;
	cout << "CinCoutCorrection - By Kudy" << endl;
	fin.get(next);
	while(!fin.eof())
	{
		if(next == 'c')
		{
			fin.get(next);
			if(next == 'i')
			{
				fin.get(next);
				if(next == 'n')
				{
					fin >> next;
					if(next == '<')
					{
						fin >> next;
						if(next == '<')
						{
							fout << "cin >>";
							fin.get(next);
						}
						else
						{
							fout << "cin >" << next;
							fin.get(next);
						}
					}
					else
					{
						fout << "cin " << next;
						fin.get(next);
					}
				}
				else
				{
					fout << "ci" << next;
					fin.get(next);
				}
			}
			else if(next == 'o')
			{
				fin.get(next);
				if(next == 'u')
				{
					fin.get(next);
					if(next == 't')
					{
						fin >> next;
						if(next == '>')
						{
							fin.get(next);
							if(next == '>')
							{
								fout << "cout <<";
								fin.get(next);
							}
							else
							{
								fout << "cout >" << next;
							}
						}
						else
						{
							fout << "cout " << next;
							fin.get(next);
						}
					}
					else
					{
						fout << "cou" << next;
						fin.get(next);
					}
				}
				else
				{
					fout << "co" << next;
					fin.get(next);
				}
			}
			else
			{
				fout << "c" << next;
				fin.get(next);
			}
		}
		else
		{
			fout << next;
			fin.get(next);
		}
	}
}



I'd like to know a better way to do this task.
Last edited on
You could recode your correct routine using a line-based approach (using std::getline and std::string's replace method)

Andy
Topic archived. No new replies allowed.