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;
}
|