944,221 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2149
  • C++ RSS
Dec 1st, 2004
0

need help with arrays

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lilpil02 is offline Offline
6 posts
since Dec 2004
Dec 1st, 2004
0

Re: need help with arrays

Post the code you've got between [CODE][/CODE] tags. Quid pro quo.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 1st, 2004
0

Re: need help with arrays

C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<string>
  3. #include<fstream>
  4. using namespace std;
  5.  
  6.  
  7. void main()
  8. {
  9. int i;
  10. int a;
  11. int b;
  12. string until;
  13. int arrayj[50];
  14. cout<<"Jay Pilrose"<<endl;
  15. cout<<"00519995"<<endl;
  16. ifstream file1;
  17. file1.open("db.txt", ios::in);
  18. for (int k=0; k<50; k++)
  19. {
  20. file1 >> arrayj[k];
  21. }
  22.  
  23. int count=0;
  24. int arrayi[50];
  25. for (i=0; i<50; i++)
  26. {
  27. while (i<50)
  28. {
  29. if(i=a)
  30. {
  31. i++;
  32. if(i=b)
  33. {
  34. i++;
  35. if(i=a)
  36. {
  37. cout<<"The sequence starts at "<<endl;
  38.  
  39. }
  40. }
  41. else
  42. i++; until; i=a;
  43. }
  44. }
  45. }
  46.  
  47.  
  48.  
  49.  
  50. }
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lilpil02 is offline Offline
6 posts
since Dec 2004
Dec 1st, 2004
0

Re: need help with arrays

>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().
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 1st, 2004
0

Re: need help with arrays

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lilpil02 is offline Offline
6 posts
since Dec 2004
Dec 1st, 2004
0

Re: need help with arrays

I have many questions so please be patient. I am also looking in help as we speak.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lilpil02 is offline Offline
6 posts
since Dec 2004
Dec 1st, 2004
0

Re: need help with arrays

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?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 1st, 2004
0

Re: need help with arrays

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.?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lilpil02 is offline Offline
6 posts
since Dec 2004
Dec 1st, 2004
0

Re: need help with arrays

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.
C++ Syntax (Toggle Plain Text)
  1. len = 616
  2. 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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 1st, 2004
0

Re: need help with arrays

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lilpil02 is offline Offline
6 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: please help me out with my c++ assignment!
Next Thread in C++ Forum Timeline: need help on corporate sales program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC