WIerd Vector Error

This is a discussion on WIerd Vector Error within the C++ Programming forums, part of the General Programming Boards category; This is my text analyzer program for school it is getting this error not a debug error but in the ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    11

    WIerd Vector Error

    This is my text analyzer program for school it is getting this error not a debug error but in the console:

    Illegal vector index: 2 max index = 1

    here is my code

    Code:
    #include<fstream.h>
    #include<iomanip.h>
    #include<iostream.h>
    #include"apvector.h"
    #include"apstring.h"
    ofstream fout;
    
    apstring getfile(apstring &);
    void foutopen(apstring &);
    void letterfrequency(apvector <int> &, apstring); 
    void getwords(apvector <apstring> &, apstring); 
    void wordfrequency(apvector<int> &, apvector<apstring>); 
    void output(apvector<int>, apvector<int>, apvector <apstring>, apstring); 
    void sort(apvector<apstring> &, int, int);
    int part(apvector<apstring> &, int, int); 
    
    // Daniel Meade, Per 4, 2/2/06, Advanced Programming
    // This is the madlib
    
    int main()
    {
     	apstring text, filename;
     	apvector <int> letfreq(26,0);
     	apvector <apstring> words;
     	char ch;
     	do
     	{
     		text=getfile(filename);
     		foutopen(filename);
     		letterfrequency(letfreq, text);
     		getwords(words, text);
     		sort(words, 0,words.length()-1);
     		apvector<int>wordfreq(words.length()-1);
     		wordfrequency(wordfreq, words);
     		output(letfreq, wordfreq, words, filename);
     		cin.get(ch);
     	}while(ch!='n'&&ch!='N');
     	fout.close();
     }
     apstring getfile(apstring & filename)
     {
     	ifstream fin;
     	char let;
     	apstring text;
     	
     	cout<< "Input Filename Please:";
     	getline(cin, filename);
     	
     	fin.open(filename.c_str());
     	while (fin.fail())
     	{
     		cout << " Invalid File: "<< endl << " Input Filename Please:";
     		getline(cin, filename);
     	}
     	do
     	{
     		fin.get(let);
     		if(let>='a'&&let<='z')
     		let-=32;
     		text+=let;
     	}
     	while(!fin.eof());
     	
     	fin.close();
     	
     	return text;
     }
     void letterfrequency(apvector<int> & letfreq, apstring text) 
    {
     	for(int c=0; c<text.length(); c++)
     	
     		if(text[c]>='A'&& text[c]<='Z')
     		
     			letfreq[text[c]-'A']++;
     		
     }
     
     
     void getwords(apvector<apstring> &words, apstring text)
     {
     	apstring word ="";
     	for(int c=0;c<text.length();c++)
     	{
     		if(text[c]>='A' &&text[c]<='Z'||text[c]=='\' '&& word !="")
     			word+=text[c];
     		else if (word!="")
     		{
     			words.resize(words.length()+1);
     			words[words.length()-1]=word;
     			word="";
     		}
     	}
     }
     void wordfrequency(apvector<int> & wordfreq, apvector<apstring>words)
     {
     	int cnt=0;
     	for (int c=0; c<words.length()-1;c++)
     	{
     		if(words[c]==words[c+1])
     			cnt++;
     		else
     		{
     			wordfreq[c]=cnt+1;
     			cnt=0;
     		}
     	}
     }
     void output(apvector<int>letfreq, apvector<int> wordfreq, apvector<apstring>words, apstring filename)
     {
     	fout << " The frequency if the letters..."<< endl;
     	for(int c=0; c<26-3;c+=3)
     	{
     		fout << char(c+'A') << '_' << letfreq[c] << "\t\t\t"
     			<< char(c+'B') << '_' << letfreq[c+1] << "\t\t\t"
     			<< char(c+'C') << '_' << letfreq[c+2] << endl;
     	}
     	fout << endl << "The disctinct words...." << endl;
     	for(int c=0;c<words.length();c++)
     		if(wordfreq[c]!=-1)
     			fout << words[c] << '_' << wordfreq[c] << endl;
     	fout << endl << "There were " << words.length() << "words";
     	cout << endl << "\nCheck the text file:" << filename;
     	cout << "\n\nDo you want to continue?(y/n)";
     }
     void foutopen(apstring &filename)
     { 
     	int c;
     	for(c=filename.length()-1; c>=0;c--)
     		if(filename[c]=='.')
     			break;
     	filename=filename.substr(0,c)+"h:\\out.txt";
     	fout.open(filename.c_str());
     }
     void sort(apvector<apstring> &words, int first, int last)
     {
     	int pivot = 0;
     	if(first < last)
     	{
     		pivot=part(words,first,last);
     		sort(words,first,(pivot-1));
     		sort(words,(pivot+1),last);
     	}
     }
     int part(apvector<apstring> & words, int first, int last)
     {
     	int firstpos, lastpos;
     	apstring firstword = words[first],temp;
     	firstpos = first;
     	lastpos=last;
     	do
     	{
     		while (words[firstpos] <= firstword && firstpos < last)
     		firstpos ++ ;
     		while (words [lastpos] > firstword && lastpos > first)
     		lastpos--;
     		if(firstpos < lastpos)
     		{
     			temp = words[firstpos];
     			words[firstpos] = words[lastpos];
     			words[lastpos] = temp;
     		}
     	}
     	while (lastpos > firstpos);
     	words[first] = words[lastpos];
     	words[lastpos] = firstword;
     	return lastpos;
     }
    Thanks guys i appreciate it alot

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    1) You don't read from files with a loop condition like this:

    while(!fin.eof())

    What happens if there is a stream error before you hit eof which prevents you from reading from the file anymore?

    2) Use at() instead of [], and get the compiler error and the line number.
    Last edited by 7stud; 03-31-2006 at 09:08 AM.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Ok, so what code did you add before you got that error? It would help if you narrowed down the problem to a certain code segment (especially since we can't compile what you've posted without apstring/apvector headers)

    What I will say is that you're using outdated code.
    Code:
    #include<fstream.h>
    #include<iomanip.h>
    #include<iostream.h>
    Those are all deprecated headers. You should use:
    Code:
    #include <fstream>
    #include <iomanip>
    #include <iostream>
    using namespace std;
    And if that won't compile, you need a new compiler.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Those are all deprecated headers.
    Not even deprecated.. they're completely non-standard.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    11
    i know stupid school is dumb and wont change curriculum or compilers.........

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Your school won't change... but you can. If you have your own box, install Intel/gcc/some standard compiler and learn good programming practices (eg. here)

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Code:
    cout<< "Input Filename Please:";
    getline(cin, filename);
    
    fin.open(filename.c_str());
    while (fin.fail())
    {
        cout << " Invalid File: "<< endl << " Input Filename Please:";
        getline(cin, filename);
    }
    When will that ever work? You get a new filename but you don't use it to open the file? Once you enter the while loop, the fail state is never going to change.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd struct error
    By sujeet1 in forum C Programming
    Replies: 5
    Last Post: 09-27-2007, 06:38 AM
  2. OpenGL Wierd **** - Order of Drawing Stuff?
    By Tonto in forum Game Programming
    Replies: 9
    Last Post: 11-09-2006, 08:56 PM
  3. Wierd chars...
    By mikahell in forum C++ Programming
    Replies: 8
    Last Post: 08-06-2006, 07:18 PM
  4. make user press enter / wierd code thingy
    By CorvusVita in forum C++ Programming
    Replies: 10
    Last Post: 04-05-2006, 09:55 AM
  5. This is wierd.
    By Ranedhel in forum C++ Programming
    Replies: 6
    Last Post: 12-12-2003, 02:26 PM

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