Thread: Incorrect result with my program

  1. #1
    *this
    Join Date
    Mar 2005
    Posts
    498

    Incorrect result with my program

    well i compiled and ran tests with my program and it seems to count the numbers incorrectly, but it is off by one and i cant seem to find the error, maybe its because im tired, but if you can see an error let me know. thanks!

    Code:
    /***************************
    Made by: Joshua R
    Date Created: 4-26-05
    Date Finished: 4-26-05
    ***************************/
    #include <iostream>
    #include <vector>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    
    struct wordData
    {
       string word;
       int count;
    };
    
    struct listType
    {
       vector< wordData > list;
       int size;
       int index;
    };
    
    void countWords (listType &, string);
    bool validate (const char);
    void listAdd (listType &, string);
    void unCap (string &);
    int bsearch (const listType &, string);
    void quickSort (vector< wordData > &, int, int);
    void printRank (const listType &);
    ostream& operator<< (ostream& out, const wordData &);
    
    main()
    {
       listType myWords;
       
       myWords.size = 0;
       myWords.index = 0;
       
       countWords (myWords, "Lesson26b.txt");
       quickSort (myWords.list, 0, myWords.size);
       printRank (myWords);
       
       cin.get();
       return 0;
    }
    
    void countWords (listType &words, string fileName)
    {
       /****************************************/   
       //     Check to see if file exists      //
       /****************************************/
       const char* fn = fileName.c_str();
       ifstream infile;
       
       infile.open(fn);
       if (infile.fail())
       {
          cerr << "ERROR: " << fileName << " could not be opened" << endl;
          abort();
       }
       /****************************************/
       char letterA, letterB;
       string word;
       
       if (infile.get(letterA))
          if (validate(letterA))
             word += letterA;
             
       while (infile.get(letterB))
       {
          if (letterB == '-')
          {   
             if (letterA != ' ')
                word += letterB;
          }
          else if (validate(letterB))
             word += letterB;
          else if (validate(letterA) && !validate(letterB))
          {
             listAdd (words, word);
             word = "";
          }
          letterA = letterB;
       }
       
       infile.close();
    }
    
    bool validate (const char letter)
    {
       if (('a' <= letter && letter <= 'z') ||
           ('A' <= letter && letter <= 'Z') ||
           (letter == '\''))
          return true;
       else
          return false;
    }
    
    void listAdd (listType &words, string word)
    {
       int index;
       unCap (word);
       
       index = bsearch (words, word);
       
       if (index == -1)
       {
         words.size++;
         words.list.resize(words.size);
         words.list[words.index].word = word;
         words.list[words.index].count = 1;
         words.index++;
       }
       else
         words.list[index].count++;
    }
    
    void unCap (string &word)
    {
       for (int count = 0; count < word.size(); count++)
          if ('A' <= word[count] && word[count] <= 'Z')
             word[count] += 32;
    }
    
    int bsearch (const listType &words, string wToSearch)
    { 
       for (int count = 0; count < words.size-1; count++)
          if (words.list[count].word == wToSearch)
             return count;
       
       return -1;
    }
    
    void quickSort (vector< wordData > &list, int first, int last)
    {
    	int g = first, h = last;
    	int midIndex;
    	int dividingValue;
    
    	midIndex = (first + last) / 2;
    	dividingValue = list[midIndex].count;
    	do
    	{
    		while (list[g].count > dividingValue) g++;
    		while (list[h].count < dividingValue) h--;
    		if (g <= h)
    			swap(list[g++], list[h--]);
    	}
    	while (h > g);
    	if (h > first) quickSort (list,first,h);
    	if (g < last) quickSort (list,g,last);
    }
       
    void printRank (const listType &temp)
    {
       int count = 0;
       
       cout << "Number of words used = " << temp.size << endl;
       cout << "Total # of words = ";
       
       for (int a = 0; a < temp.size-1; a++)
          count += temp.list[a].count;
          
       cout << count << endl << endl;
       
       for (int a = 0; a < 30; a++)
       {
          cout << setw(3) << a+1 << temp.list[a] << endl << endl;
       }
    }
       
    ostream& operator<< (ostream& out, const wordData &temp)
    {
       out << setw(6) << temp.count << "   " << temp.word;
       return out;
    }

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    At a first glance I see this:
    Code:
    for (int a = 0; a < 30; a++)
    {
          cout << setw(3) << a+1 << temp.list[a] << endl << endl;
    }
    What this means? Why 30?

    P.S. use int main()
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    *this
    Join Date
    Mar 2005
    Posts
    498
    that displays the top 30 words, each row is: # in top thirty, count of word, and word.
    for example: 1 105 the

    and btw it doesnt really matter for main to be int, because its assumed to be of type int, i guess it would be good practice? i dunno

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    FAQ > What's the difference between... > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[])
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    Basically, main() was last acceptable in the C89 standard and never in C++. Use int main().
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #5
    *this
    Join Date
    Mar 2005
    Posts
    498
    ok thats settled, but what about my error? can anyone find any error?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Problem with simple socket client/server program
    By spencer88 in forum C Programming
    Replies: 6
    Last Post: 05-05-2009, 11:05 PM
  3. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 09:48 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM