Writing a program to read a paper and count

So I have been trying to write this program that opens a .txt file and then reads it and compares each word and special character to "and, but, ; " and counts the number of words typed. It has to recongnize that the end of the paper is when the user places a "#"
I have tried everything I know how to do and I still can't figure it out, so I have been trying to just print out what it is reading from the input buffer and then putting it in a loop and when it reaches a # it quits out of the loop but it turns into an infinite loop. Please help.

here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void input_paper()
{
	char word[20];
	char paper[8000];

	cout<<"Please type out your paper and at the end of it put a # to show that your done"<<endl;
	
	
		cin.get(paper, 8000,'#');

	do
	{
		cin.get(word, 20, ' ');

		cout<<word<<endl;
	}while(word != "#");
	
}
Because you're reading into a buffer of 20 characters with "cin.get()" and not clearing it at any point in your loop, the char array 'word' will never be exactly equal to "#". I would suggest using the find() member function from the Strings library. http://www.cplusplus.com/reference/string/string/find/
I think this way will be better and will give you oportunity to read from .txt doc
Here is the code:

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  int counter=0;
  int breaking=0;
  ifstream myfile ("yourtextfile.txt");
  if (myfile.is_open())
  {
	  do{
		  getline (myfile,line);
		  size_t found=0;
		  size_t found1;
		  found1=line.rfind('#');	  
		  if(found1<=line.size())
	      {
			  breaking=1;
			  if(--found1!=' ')
			  {
				counter++;
			  }
		  }
		  do{
			  found=line.find(' ',found+1);
			  if((found!=string::npos)&&(found!=found1))
			  {
				 counter++;
			  }
		  }while(found<=line.size());
		  if(breaking==1)
		  {
			  break;
		  }
    }while (!myfile.eof());
    myfile.close();
  }
  else
  {
	  cout << "Unable to open file \n"; 
  }
  cout<<"Founded total "<<counter<<" words \n";

  return 0;
}


Hope so you like it!
Topic archived. No new replies allowed.