Thread: Help with reading files into program

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    20

    Help with reading files into program

    I have a lot of code for this. I also have 13 errors. I have tried to correct them but some are absurd to me, for example:
    "Local function definitions are illegal" Could someone point me into the right direction to correct my problems?
    Thanks so much
    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    
    using namespace std;
    
    void NumLetters(char []);
    void NumWords(char []);
    void Replace(char []);
    
    int main (void)
    {
    	char NameOfFile[100];
    	char Selection;
    	string WordToFind, OneLine;
    	int StartPos = 0;
    	int FoundPos = 0;
    	
    	cout << "Please type the name of the file to be opened." << endl;
    	cin.getline(NameOfFile, 100, '\n');
    
    	do
    	{
    		char Selection;
    		do
    		{
    	cout << "Make a choice: \n1) Count frequency of letters \n2) Count frequency of words \n3") << endl;
    
    	cout << "           Please select one of the following functions ";
    	cout << "\n			A)Tabulate the frequency of occurrence of letters (A...Z) in a file " << endl;
    	cout << "\n			B)Count the occurrence of a specified word in a file. ";
    	cout << "\n			C)Replace a specified word by another word in a file. ";
    	cin >> Selection;
    	
    	switch(toupper(Selection))
    	{
    	case 'A':
    		NumLetters(NameOfFile);
    	break;		
    	
    	case 'B':
    		cout << "Please enter the word to find." << endl;
    		cin >> WordToFind;
    	
    	while(StartPos < OneLine.size() && FoundPos != -1)
    	{
    		FoundPos = OneLine.find(WordToFind, StartPos);
    		cout << FoundPos << endl;
    		StartPos = FoundPos + 1;
    	}
    		getline(NameOfFile, OneLine, '\n');
    		NumWords(NameOfFile);
    		
    	break;
    
    	case 'C':
    		Replace(NameOfFile);
    	break;
    
    	default:
    		cout <<  "Please make a valid selection" << endl;
    	}
    while(Selection != A && Selection != B && Selection != C)
    	{
    	cout << "Want to choose another function?" << endl;
    	cin>> Selection;
    	}
    }
    	return 0;
    }
    void NumLetters(char fileName[100])
    {
    	
    	int SumLetters [26];
    	
    	for(int index = 0; index < 26; index++)
    		SumLetters[index] = 0;
    
    	int TotalLetters = 0;
    
    	ifstream NameOfFile (fileName);
    
    	while (!NameOfFile.eof())
    	{
    		char TheChar;
    		TheChar = toupper(NameOfFile.get());
    		if(TheChar >= 'A' && TheChar <= 'Z')
    		{
    			SumLetters[int(TheChar) - 65]++;
    			TotalLetters++;
    		}
    	}
    	cin.get();
    	for(int k = 0; k < 26; k++)
    	{
    		cout << "For " << char(k + 65) << " or " << char(k + 97) << " : " << endl;
    		cout << setw(20) << "TotalCount = " << SumLetters[k] << setw(20) << "Percent = " << setiosflags(ios::fixed) << endl;
    	}
    }
    void NumWords(char fileName[100])
    {
    	ifstream NameOfFile(fileName);
    
    	string CountWord;
    
    	cout << "Enter the word you would like to count." << endl;
    	cin >> CountWord;
    
    	for (int k = 0; k < CountWord.size(); k++)
    		CountWord[k] = toupper(CountWord[k]);
    	cin.get();
    
    	int Count = 0;
    
    	while(!NameOfFile.eof())
    	{
    		string OneLine;
    
    		getline(NameOfFile, OneLine, '\n');
    
    		int Len = OneLine.size();
    
    		for(k = 0; k < Len; k++)
    			OneLine[k] = toupper(OneLine[k]);
    		if (Len > 0)
    		{
    			for(int StartPos = 0; StartPos < Len;)
    			{
    				int Pos;
    				Pos = OneLine.find(CountWord, StartPos);
    
    				if (Pos >= 0)
    				{
    					Count++;
    					StartPos = Pos + CountWord.size();
    				}
    				else
    					break;
    			}
    		}
    	}
    	cout << "The Word " << " ' " << CountWord << " ' " << " is found " << Count << " times." << endl;
    }
    void Replace(char fileName[100])
    {
    	ifstream NameOfFile (fileName);
    
    	string WordToReplace;
    	string ReplaceWord;
    	char FileOut[100];
    
    	cout << "Enter the word you would like to replace." << endl;
    	cin >> WordToReplace;
    
    	cout << "What word would you like to replace: " << WordToReplace << "with?" << endl;
    	cin >> ReplaceWord;
    
    	cin.get();
    
    	cout << "Please type where you would like the new file to go." << endl;
    	cin.getline(FileOut, 100, '\n');
    
    	ofstream Outfile(FileOut);
    
    	while(!NameOfFile.eof())
    	{
    		string OneLine;
    		getline(NameOfFile, OneLine, '\n');
    
    		int Len = OneLine.size();
    
    		if(!OneLine.empty())
    		{
    			for(int StartPos = 0; StartPos < OneLine.size();)
    			{
    				int Pos;
    				Pos = OneLine.find(WordToReplace, StartPos);
    
    				if(Pos >= 0)
    				{
    					OneLine = OneLine.replace(Pos, WordToReplace.size(), ReplaceWord);
    					StartPos = Pos + ReplaceWord.size();
    					cout << OneLine << endl;
    					cin.get();
    				}
    				else
    					break;
    			}
    		}
    		Outfile << Line << '\n';
    	}
    	cout << "The file " << FileOut << " is nice!" << endl;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    This has an extra closing bracket
    Code:
    cout << "Make a choice: \n1) Count frequency of letters \n2) Count frequency of words \n3") << endl;
    >>while(Selection != A && Selection != B && Selection != C)
    Did you mean:
    >>while(Selection != 'A' && Selection != 'B' && Selection !='C')

    Your do's don't match your while's
    You have inbalanced braces.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    in order to use objects of the STL string class you must have this line prior to the first time you declare a string object:

    #include <string>

    When you try to declare a string object without that line you are trying to use a constructor/function that isn't available. Same when you try to call any of the methods of a string object such as size(). It's been a while since I used this header so I forget whether find() is a generic algorhithm or whether it is specific to the string class or whether it is both, but it may be a source of error, too.

    Whether these corrections remove the error is difficult to say. Many compilers flag the offending line and so you can track the down the error more readily. It really helps those who try to help if you tell us where the error is occurring.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    20
    Ok thanks,yes I did forget the #include <string>.
    The functions NumWords & Replace are still saying"Local function definitions are illegal" I don't know what this means, I've never seen this error before. I'm really confused on the "syntax error: return" error. Type void unexpected for my NumLetters function. I'm sorry about the length of my troubles. But I really appreciate any comments on these.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by bearcat19
    The functions NumWords & Replace are still saying"Local function definitions are illegal" I don't know what this means, I've never seen this error before. I'm really confused on the "syntax error: return" error. Type void unexpected for my NumLetters function. I'm sorry about the length of my troubles. But I really appreciate any comments on these.
    If you have unmatched braces, your compiler is going to get very confused. Double check that first.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    DEbugging is an art. One technique I use is the comment out technique. To do this comment out the last function and compile. Compare error messages you get with the last function commented out compared to the errors when not commented. Then comment out the last two functions, etc. When you find a level of code that runs without error, then uncomment the functions one at a time fixing all errors as they come up. Once you have localized a given error to a given function, start commenting out sections of the offending function until you have a very small section of code, often a single line, that you need to look at.

    I also heartily agree with the advise to redo your indenting to make it easier to match braces. The commenting out technique can help you find this type of error, but it's a lot easier to do it right the first time.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    20
    Thanks. I will definetly try commenting out the "bad" sections.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Why isn't my program reading my file properly?
    By DCMann2 in forum C Programming
    Replies: 10
    Last Post: 04-23-2008, 01:16 AM
  4. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  5. Having some trouble with reading from files
    By bluebob in forum C Programming
    Replies: 19
    Last Post: 04-18-2002, 05:29 AM