Array of Strings Implementation and Push elements

Program reads string x, pushes x to array

Page 1 of 1

1 Replies - 1180 Views - Last Post: 22 April 2010 - 10:40 PM Rate Topic: -----

#1 Mawhonic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-April 10

Array of Strings Implementation and Push elements

Posted 22 April 2010 - 10:23 PM

Alright, so I've got this program description in which I am meant to read from a file and then categorize the collected data from that file into, well, categories, using whatever data structure I want. So, I thought I'd use an array of strings since the words retrieved from the file are saved under string x.

Basically, it reads the file, then retrieves a word, saves it as string x, displays x, and then moves onto the next word and that word is then saved as string x and displayed.

But what I'd like to do is inbetween the times that a word is saved as string x and then it moves on, how can I push that string x into an array of strings element so that the data of that word is not lost but saved in the array for later?

Here's what I have now:

int main() 
{

string x;
string* array = new String[x];

string y;
ifstream inFile;

	inFile.open("data1.txt");
	if (!inFile) 
	{
		cout << "Unable to open file";
		exit(1); // terminate with error
	}

	while (inFile >> x) 
	{
		if(x == "(IS-A")
		{
			cout<<"(FACT (IS-A ";
			inFile >> x;
		        cout<< x;
			inFile >> x;
			cout<< x;
			cout<<endl;
			
		}
}



Please put any code you post here between :code: tags.

This post has been edited by Martyn.Rae: 22 April 2010 - 10:27 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Array of Strings Implementation and Push elements

#2 muballitmitte  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: Array of Strings Implementation and Push elements

Posted 22 April 2010 - 10:40 PM

you could declare a vector of string

#include<vector>
vector<string> v;


and then push elements as you have them

v.push_back(x)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1