Code error

This is a discussion on Code error within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <fstream> #include <conio2.h> #include <vector> #include <string> #include <cstring> using namespace std; int main() { vector ...

  1. #1
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227

    Code error



    Code:
    #include <iostream>
    #include <fstream>
    #include <conio2.h>
    #include <vector>
    #include <string>
    #include <cstring>
    
    using namespace std;
    
    
    int main()
    {
     
        vector <string> Words;
        char letterlist[256];
        char wordlist[256];
        int wordcount = 0;
        int x = 0, y = 0;
        int inputlength;
        int wordlength;
        bool Positive = false, quit = false;
        char exit;
        
    while (quit == false)
    {    
        cout << "Letter list: \n(All lowercase)";
        gotoxy(14,1);
        cin >> letterlist;
        
        inputlength = strlen (letterlist);
            
        ifstream OpenFile("dict.txt");
    
        while(! OpenFile.eof())
        {   
            x = 0;
            y = 0;
            wordcount++; 
                    
            OpenFile.getline(wordlist, 256);
            
            wordlength = strlen (wordlist);
            
            while (x <= wordlength)
            {
                if (wordlist[x] != wordlist[y])
                {
                    y += 1;
                    
                    if (y == inputlength + 1)
                    {        
                       Positive =  false;      
                       break;
                    }
                }
                
                else
                {
                    ++x;
                    Positive = true;
                }//close else statement   
            }//close inner while loop
            
            if (Positive == true)
            {
                Words.push_back(wordlist);
            }    
            
        }//close outer while loop    
        
        clrscr();
        cout << "Finished!\nWords Returned Positive:\n";
        for (int x = 0; x < Words.size(); ++x)
        {
            cout << '\t' << Words[x] << '\n';
        }
        
        
        system("pause");   
        clrscr();
        cout << "Quit Y/N?:";
        cin >> exit;
        
        if (exit == 'y' || exit == 'Y')
        quit = true;
        
        if (exit == 'n' || exit == 'N')
        quit = false;
     
     
            
    }//close biggest wile loop
    
        
    }//close main()
    it is supposed to find all the words that you can make with the letters that you input, and it uses dict.txt as a reference. but all it ends up doing is loading every word in dict.txt into vector "Words" and then printing to the screen. why wont it work?
    Keyboard Not Found! Press any key to continue. . .

  2. #2
    Registered User
    Join Date
    May 2004
    Posts
    12
    Well firstly
    Code:
     if (wordlist[x] != wordlist[y])
    should be
    Code:
     if (wordlist[x] != letterlist[y])
    Also be aware that you can overrun your letterlist buffer, and should also check for errors when opening your input stream.

  3. #3
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    > and should also check for errors when opening your input stream.

    yea, i guess ill add that later, but its just for personal use, nothing big

    hahahah, cant believe that was the mistake
    Keyboard Not Found! Press any key to continue. . .

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > while(! OpenFile.eof())
    The reason you shouldn't do this is in the FAQ

  5. #5
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    i actually picked openfile.eof() up from another tutorial i was reading, but thanks
    Keyboard Not Found! Press any key to continue. . .

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Also not to sound evil and mean but using a more descriptive title is nice as well. Like
    Letter Comparision Error or something like that
    Woop?

  7. #7
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    oh ok, didnt really know it mattered, but i got another minor problem:
    it will keep on pushing blank lines (like sometimes 10 at a time) into the vector and there are none in dict.txt
    Keyboard Not Found! Press any key to continue. . .

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Can you post the updated code?
    Woop?

  9. #9
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    Code:
    #include <iostream>
    #include <fstream>
    #include <conio2.h>
    #include <vector>
    #include <string>
    #include <cstring>
    
    using namespace std;
    
    
    int main()
    {
     
        vector <string> Words;
        char letterlist[256];
        char wordlist[256];
        int wordcount = 0;
        int x = 0, y = 0;
        int inputlength;
        int wordlength;
        bool Positive = false, quit = false;
        char exit;
        
    while (quit == false)
    {    
        cout << "Letter list: \n(All lowercase)";
        gotoxy(14,1);
        cin >> letterlist;
        
        inputlength = strlen (letterlist);
            
        ifstream OpenFile("dict.txt");
    
        while(! OpenFile.eof())
        {   
            x = 0;
            y = 0;
            wordcount++; 
                    
            OpenFile.getline(wordlist, 256);
            
            wordlength = strlen (wordlist);
            
            while (x <= wordlength)
            {
                if (wordlist[x] != letterlist[y])
                {
                    y += 1;
                    
                    if (y == inputlength + 1)
                    {        
                       Positive =  false;      
                       break;
                    }
                }
                
                else
                {
                    ++x;
                    Positive = true;
                }//close else statement   
            }//close inner while loop
            
            if (Positive == true)
            {
                Words.push_back(wordlist);
            }    
            
        }//close outer while loop    
        
        clrscr();
        cout << "Finished!\nWords Returned Positive:\n";
        for (int x = 0; x < Words.size(); ++x)
        {
            cout << '\t' << Words[x] << '\n';
        }
        
        
        system("pause");   
        clrscr();
        cout << "Quit Y/N?:";
        cin >> exit;
        
        if (exit == 'y' || exit == 'Y')
        quit = true;
        
        if (exit == 'n' || exit == 'N')
        quit = false;
     
     
            
    }//close biggest wile loop
    
        
    }//close main()
    i have only made DonFiasco's change so far, so there isnt a lot of change
    Keyboard Not Found! Press any key to continue. . .

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > i actually picked openfile.eof() up from another tutorial i was reading, but thanks
    I'll leave you to figure out why the last line of the file appears twice in your word list then.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  3. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  4. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM

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