1. You will write a program that searches through a text file and finds every in
ID: 3716826 • Letter: 1
Question
1. You will write a program that searches through a text file and finds every instance of the name "James Bond" and replaces it with "OPERATIVE" in your new output file. 2. Your program will also find all dollar values and add them up for the accountants. Dollar values will be replaced with "###" in your new output file. You may assume that all dollar values start with $ and are decimals. You may also assume that there is no other instance of $ in the file. 3. You may not use any "using namespace" statements in your code, including namespace std. Notes: You will obviously use the find) function in this program. If what you are looking for isn't in that string, you will get a 1 rather than the appropriate value. You will need to test for this and avoid the program-breaking errors that happen when you try to do something to the -1 position in a string. -You will be reading in from a file but you will write your new "sanitized" version of the report to a new file named CleanOutput_Firstname_Lastname.txt I've completed the program on my end and I used 2 functions that we haven't covered in class. These are not uncommon functions and are not complicated. I've included a couple of examples below and they are covered extensively on the internet. Replace Example.PNG replaces a portion of the string with another string provided by you Stod Example.PNG converts a string to a double. ALL VALUES W?LL BE BETWEEN 1000.00 and 9999.99. Hint: This means that the minimum and maximum value retrieved from the file will always be 7 chars long. So, the string you are converting will only need to hold 7 chars.Explanation / Answer
Code:
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include<stdlib.h>
using namespace std;
int main()
{
char name[30];
string line;
cout << "Enter the file name:";
cin >> name; size_t found; string word;
ofstream fout("CleanOutput_Firstname_Lastname.txt");
ifstream fin(name);
if (!fin)
{
cout << "Error opening file "; return 0;
}
int total = 0;
while (getline(fin,line))
{
found = line.find("James Bond");
while(found != string::npos)
{
line.replace(found,10,"OPERATIVE");
found = line.find("James Bond");
}
fout << line << endl;
stringstream ss(line);
while (ss >> word)
{
if (word[0] == '$')
{
string temp = "";
for (int i = 1; i<word.size(); i++)
temp = temp + word[i];
total = total + atoi(temp.c_str());
}
}
}
fout.close();
cout << "Total :" << total << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.