I need help with a homework assignment. The assignment is to take a paragraph from an infile and change the words French into Freedom. Also when the phrase "an arbitrary end point" is seen I need to end the paragraph. And also if an integer if present the integer needs to be replaced with correct # symbols. i.e. 3 = ###. I then need to output the new paragraph to the screen. Here is my code as of now:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>

using namespace std;

int main (int argc, char *argv[])
{
string strInput;
string sentence;

ifstream inFile;
inFile.open("data.txt");

inFile>>strInput;
while (inFile)
{
sentence = sentence +strInput + " ";
inFile >> strInput;

if (sentence == "French")
string sentence.replace ("French", "Freedom");
if sentence == "an arbitrary end point")
string sentence.replace ("an arbitrary end point", '\n');
}

cout << sentence;

system ("PAUSE");
return EXIT_SUCCESS;
}

Just to get you started, the way you are using the replace member of std::string is not how it was intended to be used. Replace cannot substitute dissimilar sized items. For example, you cannot replace "hello" with "hi".

Instead, you might replace it with "hiya!."

The syntax is: ourString.replace( n, n_from_n, our_string )

Or in the case of:

string s = "Why Hello";
s.replace(4,5, "hiya!");

cout << s << endl;
Output: Why hiya!

There are other ways of using replace, but they involve iterators and it is a little more complex, however overall the behavior is the same.

So instead of this, you probably want to us std::string::remove() and std::string::insert(). This will allow you to remove a piece of the string, and then where it was, insert a different string. In this case it doesn't matter the size of the two strings.

Here's a small example code that replaces all of the letter A's with 'foo'

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s = "a b c d e f g a b c a b a c d e a g";

	for( unsigned match = 0; 
             match < s.length(); 
             match = s.find("a", match) )
	{
		s.erase(match,1);
		s.insert(match, "foo");
	}

	cout << s << endl;
}

Good luck!

A couple of things at first glance.

When you use the istream operator to read the data from your paragraph, then you will be reading in one word at a time. So if you are looking for a phrase "an arbitrary end point" , then that will not work, the way you are doing it. You could read in an entire line at a time from your file, using getline(), and then search for the words you need in that line. You should break out of your while loop if you reach the end of the paragraph.

Also you need to replace any integers in the line with #. So that means you will need to scan the line for any numbers and replace them. You can check for any numbers in your line by looping through and using isdigit() (header <ctypes.h>), and then replace them with the correct number of #'s. I am not sure if you can use stringstream to parse it out or not, but I'll check.

This article has been dead for over six months. Start a new discussion instead.