String Substitution Problems

Having a bit of trouble with my String Substitution program. It's supposed to open a file, then paste the replacement into another file.

It's supposed to replace the first "first" with "second", every "all" with "some", then the very last "last" with "penultimate".

When trying to compile, I get a TON of errors. Can anyone help me with what I'm doing wrong?

http://pastebin.com/T0JZsTsZ

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 <string>
 using namespace std;

 int main() {
 string data, textOut;


 ifstream dataIn;
 dataIn.open("indata.txt");

 if (dataIn.is_open())
 {
 while (dataIn.good())
 {
 getline(dataIn, data);
 cout << data << endl;
 }
 textOut = data;
 while (dataIn.is_open())
 {
 textOut.replace(textOut.find("first"), 6, "second");
 textOut.replace(textOut.find("all"), 4, "some");
 textOut.replace((textOut.find_last_of("last", 0)), 5, "penultimate");
 }

 dataIn.close();
 }
 else cout << "Unable to open file. Please confirm you have exact file name and location." << endl;


 ofstream fileOutput;
 fileOutput.open("dataout.txt");
 fileOutput << textOut;
 fileOutput.close();

 system("pause");

 return 0;
 }
Last edited on
you need to also upload your data file and edit this forum and mark all your coding than press <> button
and not sure why you would if(dataIn.is_open()) than did dataIn good.
if your data had one string like Monkey you sould print like

//read until blank
while(dataIn >> data)
{
cout << data << endl;
//Monkey
}
if two string than like Monkey Apple
while(dataIn >> data >> data2)
{
cout << data << data2 << endl;
// MonkeyApple
}
if you have data something like ... Monkey.Apple
than too long to explain....
Last edited on
Sorry. My text file reads

" This is the first time I ever ate ice cream. I wish I was the first person who ever ate ice cream.
The very last time I ate ice cream, it gave me a headache. I swore it would be the last time.
All I ever do is eat, eat, eat ... all the time. We all eat too much all the time."

Simple text file.
When trying to compile, I get a TON of errors. Can anyone help me with what I'm doing wrong?

Yes. You're not supplying the TON of errors you get when you try to compile. The first few would be helpful.
namefile is ifstream
Data is string

while (getline(namefile, Data))
{
// Data now has "This is the first time I ever ate ice cream. I wish I was the first person who ever ate ice cream." after finishing putting first setence, while loop move on to next setence. "The very last time I ate ice cream, it gave me a headache. I swore it would be the last time." for second
and goes untill data have nothing or empty sentence.
}

hope this helps
Last edited on
"Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention."

"Unhandled exception at 0x7697C42D in ConsoleApplication6.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0031F6E0."

Then I get a debug error saying "CRT detected the application wrote to memory before start of heap buffer."
The first two repeat often until the debug error rears its ugly head.
These are not errors "when trying to compile." This is behavior exhibited when you run the compiled and linked code. Being accurate when describing your problem means anyone who responds is more likely to address the actual issue(s) you're having.

Your code doesn't make much sense. You continue overwriting the same string in the loop on lines 16 to 20. You do not check if the extraction was successful before doing anything with the string (and, in fact, on the last iteration of the loop it will not be successful.) After the loop you will either have only the last line in the file or an empty string.

std::string::replace requires a valid position argument. You don't make any effort to ensure you have one. std::string::find_last_of doesn't do what you think it does.

The while loop beginning on line 22 will never end.

You only write a single line to the output file.


Topic archived. No new replies allowed.