occurances of string in a file

This is a discussion on occurances of string in a file within the C++ Programming forums, part of the General Programming Boards category; I am to request a file description and a string from a user then state the number of occurrences of ...

  1. #1
    MB1
    MB1 is offline
    Registered User
    Join Date
    Feb 2005
    Posts
    38

    occurances of string in a file

    I am to request a file description and a string from a user then state the number of occurrences of the string in the file. Maybe I made mistakes in my notes. Also, getline(cin, InputLine, i\ni). The i\ni is not 'i's. What is the character and how do I type it?

    Code:
    /* Write a program that requests a file description (word)
    and a string from the user then states the number of occurances
    of the string in the file. (We will search for the letter 'a'.)
    If the file does not exist, the program should state this 
    to the user.*/
    
    #include <iostream>
    #include <fstream>
    #include <conio.h>
    #include <string>
    using namespace std;
    
    int main()
    {
    	//Step01: Declare Memory Locations
    	ifstream InFile;
    	string MyWord; //Location of text to be searched for
    	//Step02: Initialize Memory Locations
    	InFile.open("C:/Temp/TestData1.txt");/*Location of file 
    	to be read*/
    	int Index = 0; //Counter for occurances
    	//Step03: Do the Work
    	cout << "Please enter text: ";
    	cin >> MyWord;
    	if (!InFile.is_open())
    	{
    		cerr << "File does not exist" << endl;
    		getch();
    		exit(0);
    	}
    	while(InFile.open)
    	{
    		getline(cin, MyWord, i\ni);
    		string Letter = MyWord.find("a");
    		Letter = MyWord.substr(0,1);
    		Index++;
    	}
    	//Step04: Do the Work
    	InFile.close;
    	cout << endl;
    	getch();
    	return 0;
    }
    Last edited by MB1; 03-28-2005 at 11:43 AM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The i\ni is not 'i's
    What language is that?

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    38
    'i\ni' ... don't know what you mean. It will never compile, ofcourse. Perhaps the newline character, '\n'?

  4. #4
    MB1
    MB1 is offline
    Registered User
    Join Date
    Feb 2005
    Posts
    38
    Quote Originally Posted by 7stud
    What language is that?
    c++ The actual character is not an 'i'. It is supposed to terminate the extraction. I was hoping someone would know. It may be the substr that I am supposed to look for.

    The main idea is for me to read a string and pull out the occurances of a work. I am using 'the'.

    Code:
    /* Write a program that requests a file description (word)
    and a string from the user then states the number of occurances
    of the string in the file. (We will search for the word 'the'.)
    If the file does not exist, the program should state this 
    to the user.*/
    
    #include <iostream>
    #include <fstream>
    #include <conio.h>
    #include <string>
    using namespace std;
    
    int main()
    {
    	//Step01: Declare Memory Locations
    	ifstream InFile;
    	string MyWord; //Location of text to be searched for
    	//Step02: Initialize Memory Locations
    	InFile.open("C:/Temp/TestData1.txt");/*Location of file 
    	to be read*/
    	int Index = 0; //Counter for occurances
    	//Step03: Do the Work
    	cout << "Please enter text: ";
    	cin >> MyWord;
    	if (!InFile.is_open())
    	{
    		cerr << "File does not exist" << endl;
    		getch();
    		exit(0);
    	}
                    getline(cin, MyWord, the);
    	{
    		string Letter = MyWord.find("the");
    		Letter = MyWord.substr(0,1);
    		Index++;
    	}
    	//Step04: Do the Work
    	InFile.close;
    	cout << endl;
    	getch();
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Replies: 6
    Last Post: 01-03-2007, 02:02 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21