Problems Replacing a word in a String Array

I'm having some problems getting the this code to work. It's supposed to take a text file, load it into the buffer in paragraphs, assigned to a vector, convert the vector to an array, and then replace a specific word in those paragraphs.

But the problem I'm having is array[0] gives a value but array [1] doesnt or [2], possibly they are assigned in memory to array[0]? My final goal to get this program to work would be to convert the paragraphs stored in the array into char type so I can replace the specific words and cout to the user. Could someone help me?

Here is the code so far:
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
// TestVectorTrek2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <ciso646>
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;


typedef vector <string> paragraph_t;
istream& operator >> ( istream& ins, paragraph_t& paragraph )
  {
  string line;
  paragraph.clear();
  while (getline( ins, line ) and !line.empty())
		{
	
		paragraph.push_back( line );
		}
	return ins;
  }

int _tmain(int argc, _TCHAR* argv[])

	{
		string buffer;
		vector<string>  theArray;

		ifstream IN_file("wordsparagraphs.txt", ios::in);

		while(! IN_file.eof())
		{
			getline(IN_file, buffer); // add the line to the end of the array
			
			theArray.push_back(buffer);
		}

		{
		
			{
				string *scr;
				scr = new string [theArray.size()];
				for(int i= 0; i< theArray.size(); i++)
				scr[i] = theArray[i];//Copy the vector to the string
			
			}
		}
	system ("pause");
	return 0;

	}
convert the vector to an array


The vector is an array.

It's supposed to take a text file, load it into the buffer in paragraphs, assigned to a vector,


Doesn't make sense. getline reads each line of the file. Each string in your vector will be one line from the text file up to the carriage return unless you specify something to override the default delimiter which is '\n'. You could reach each sentence using '.' as the delimiter assuming that the text in the file is grammatically correct.

Don't use EOF in your loop. the program has a serious flaw. Read this. You want to put the getline call inside the while loop. It returns the stream so that you can implement a check of whether the call failed or succeeded.
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5

would be to convert the paragraphs stored in the array into char type so I can replace the specific words and cout to the user.


You don't need to convert anything. The vector is an array of string objects and each string is already encapsulating a char array which can be easily modified using the std::string member functions.
Thank you, I used some of your suggestions and they improved the program. But for some reason I would still have three lines of text output for array block [0] while nothing out output to console for array block [1] or [2]. And then at array block [3] I would get another 3 lines of text. It's like the array was chunking together at every 3rd block. I thought since an array has one less container then a vector that could be my problem.

What I ended up doing was manually copying the text into code separating paragraphs as case statements. And using switch/case in a do while loop to get the paragraphs I want to output to console.
Topic archived. No new replies allowed.