Hello everyone! I sure wish I would have known about this place befroe now, it would have saved some long nights. I am having probems with arrays. The situation is this read all the lines from a file and store it into an array. Once you do this, you have a character array. All your further operations should be done on the array. create a function called "Matcher". The Matcher function should go through the entire array and find how many times the string or character pattern 'aba' occurs in the entire array.
On finding a match, the program should display the index number of the array at which the string pattern started.
Once the search is complete, the program should display how many times it found the string or character pattern 'aba' in the entire array or more appropriately, in the file.
If the number of matches is zero, the program should output "Search String NOT found".
For programming purpose use this file( "file.txt"). I tried to write it with out any help and am finding I need some. I am a little older and you know what they say about teaching an old dog new tricks.

Recommended Answers

All 9 Replies

Post the code you've got between CODE tags. Quid pro quo.

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


void main()
{
	int i;
	int a;
	int b;
	string until;
	int arrayj[50];
	cout<<"Jay Pilrose"<<endl;
	cout<<"00519995"<<endl;
	ifstream file1;
	file1.open("db.txt", ios::in);
	for (int k=0; k<50; k++)
	{
		file1 >> arrayj[k];
	}
	
	int count=0;
	int arrayi[50];
	for (i=0; i<50; i++)
	{
		while (i<50)
		{
			if(i=a)
			{
				i++;
				if(i=b)
				{
					i++;
					if(i=a)
					{
						cout<<"The sequence starts at  "<<endl;

					}
				}
			else
				i++; until; i=a;
			}
		}
	}


	

}

I am pretty sure this won't do what it is supposed to but it may do something.
Another try looked much the same but used && operators and compared them on the same line. I have a hard time knowing how and what to pass in a function. I would use a void function for the matcher part but I am not sure that is the best way to go about it.

>if(i=a)

This is assigning the value of a to i and then evaluating whether i is nonzero. Use if(i==a).

Why do you take input into an array of int if it is supposed to be an array of char?

>void main()

Use int main().

So by changing the int arrayj to char arrayj I will get characters instead of numbers? That was one problem I had with it. I am trying to use a book that uses all numbers in the examples but then asks you to use characters in the problems. Will the code that I have now work in a function?

I have many questions so please be patient. I am also looking in help as we speak.

Try adding some debugging statments to help you as you go. Just use cout to display something you may be curious about. This will be one way to answer your own questions as you go.

Are you required to do user input char by char, or are you allowed to use other standard functions such as getline?

The book does't say to do it any special way it just says to load a file into an array and then use the array to do everything else. The file is supposed to be created in notepad. When you say debugging statements you mean like this //and the text following turns green.?

No. Debugging with cout would be like this:

#include <iostream>
#include <fstream>
#include <cstdio>
using namespace std;

char array [ 1000 ];

int main()
{
   ifstream file(__FILE__);
   const char seq[] = "123";
   file.getline(array, sizeof array, '\0');
   const size_t len  = strlen(array);
   cout << "len = " << len << endl; // DEBUG
   const size_t size = strlen(seq);
   cout << "size = " << size << endl; // DEBUG
   for ( size_t i = 0; i < len - 2; ++i )
   {
      if ( strncmp(&array[i], seq, size) == 0 )
      {
         cout << "The seq starts at " << i << endl;
         cout << &array[i] << endl;
         break;
      }
   }
   return 0;
}

They are extraneous lines that during development will at least tell you what is going on in a program at a certain point. And lines that are generally removed after you've debugged and gotten portions working.

In the above example, these two lines produce this output.

len = 616
size = 3

This can be helpful information while you're getting things working. For example, you might find that a buffer size of 50 is not big enough.

So they are lines that are not integral to the program but they output values? This book dosen't say anything about them. Those will be helpful, if I can get the program to work.

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.