int number=0, subtranslines=50;
	string term[]={"Jordan"};
	//string term="Jordan";
	string voltage= "69";
	string	line;
	size_t found;

	
	while (! data_file.eof() && getline(data_file,line).good() )
	{
		//cout<<line<<'\n';
		for (int i=0; i <=subtranslines; i++){
		if( (found=line.find ("term[i]",0)) !=string::npos && (found=line.find (voltage,0)) !=string::npos)
		{
		number++;
		cout<<line<<'\n';
		out_file<<line<<endl;
		}}}
	//To find the number of outages and subtract the header
		cout<<"number = "<<number<<'\n';

The code works when I do not try to pass term and just use term. What I am trying to do is read in a file, search for a word, if the row contains that word write it to a file and keep searching until it does not find it and then search for the next word, find, write it and so on.

For some reason it will not work when I try and use an array.


Another questions...
On the line

if( (found=line.find ("term[i]",0)) !=string::npos && (found=line.find (voltage,0)) !=string::npos)

What does the ,0 do after the two terms??


Thank YOU!

Recommended Answers

All 14 Replies

Why are you using

string term[]={"Jordan"};

? Why not just

string term = "Jordan";

Also, why do you have quotes around

"term[i]"

?

well I am going to add more to that string

and I removed those quotes. It wasnt working so I was just trying some things

ah I am at work and that site is blocked! Can you give me another example please?

#include <iostream>
#include <string>

int main()
{
  std::string string1 = "Hello";
  string string2 = "World";

  // Concatenate strings together using the operator +
  string full = string1 + " " + string2;

  std::cout << full << std::endl;
  return 0;
}

That wont do what I want though? I want to search the csv file for the term "Jordan", write them to another file, count how many times it exists and then search for the next term "Smith" and keep going. I would need a loop for that. Concatenating wont do what I want I dont think

You should also look at making a std::vector<std::string> .

im pretty sure my looping is incorrect

I am very close

while (! data_file.eof() && getline(data_file,line).good() )

Where does this line store the value so I can clear it????

Scan:	
		ifstream data_file("DailyReport(2).csv");
		if(data_file.bad()){
		printf("Could not open File!\n");
		exit (8);}
		else{
		printf("File opened successfully\n");}

		while (! data_file.eof() && getline(data_file,line).good() )
		{
		if( (found=line.find (term[adv],0)) !=string::npos && (found=line.find (voltage,0)) !=string::npos)
		{
		number++;
		cout<<line<<'\n';
		out_file<<line<<endl;
		}}
		adv=adv+1;
		if (adv<subtranslines)
		{
		goto Scan;
		}

I figure it all out, but is there a better way to do this??

Yikes! Never, ever, ever use goto! Also, this is going back and opening the file again - is this what you want?

well If i do not re open the file, it will not re enter the while loop again. Hence my previous question on how to clear that value.

As far as goto, I dont know what else to use

Scan:	
			while (! data_file.eof() && getline(data_file,line).good() )
		{
		if( (found=line.find (term[adv],0)) !=string::npos && (found=line.find (voltage,0)) !=string::npos)
		{
		number++;
		cout<<line<<'\n';
		out_file<<line<<endl;
		}}

		adv=adv+1;
		data_file.close();
		data_file.seekg(0,std::ios::beg); //reset read pos to beginning
		data_file.clear(); //clear eofbit
		if (adv<subtranslines)
		{
		
		out_file<<term[adv]<<"outages"<<endl;
		goto Scan;
		}

AHHHH! Whats wrong here! No errors but it will not go back into that first while loop again! I tried resetting the values

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.