C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 1st, 2006, 11:18 PM
AniamL AniamL is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 3 AniamL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 53 m 39 sec
Reputation Power: 0
Question Pretty new programmer with a slightly complex problem...

Okay, I'm a pretty new programmer, but I've decided to tackle a rather sophisticated project by myself. I've done it all by myself, with extensive debugging (only had to look at a book to figure out the ifstream business!) but I'm hung up with one slight problem.
The basic function of the code is to let the user input a word, and then, with the help of a word list, display the first word that contains all the original word's letters minus one (as in removing one letter then rearranging the remaining letters.)
Example:
sprite
spite

Then the following word is passed through the same process, yielding:

pits
tip
it
i

(or other words that also fit the criteria)

The problem I have is that my program only runs through the word list once. So if the word list is:

i
it
tip
pits
spite

Then if you type in sprite, only spite will come up, and then the program will end (because it reaches the end of the word list).
The reason for loops and while loops don't work is because I'm running an ifstream (newfile) and so the next iteration of the loop, newfile won't start at the top again. The only way I know to get the ifstream to start anew is to use another stream (as in typing "ifstream newfile2"). I'm sure there is a very easy solution, but this is my first true venture into file streams. Hopefully one of you can help me.

PHP Code:
#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
using namespace std;
int InWord(char characterstring word)
{
    
int x 0match 0;
    for(
x;x<word.length();x++)
    {
        if(
character==word[x])
            
match 1;
    }
    
return 
match;
}
int main()
{
int x 0loop 1next 0counter=0;
string wordword2longerword;
cout << "Enter a word.\n";
cin >> word2;
ifstream newfile("c:\\wordlist2.txt");
while(
loop)
{
    if(
word2.length() < 2)
    {
    
cout << "Congratulations, you found a winning word!\n";
    
loop 0;      //achieved goal, loop ends.
    
}

    if(
next == || word == word2)
    
newfile >> word;

/*following if statement makes sure word is the right length and
 makes sure a new word can be accessed if the previous word 
had the wrong characters, i.e. next == 0 */

    
if(word.length() != word2.length() - || next == 1)         
    {                                                     
        do{                                               
        
newfile >> word;                                
        }while(
word.length() != word2.length() - 1);
    }

    
cout << word << endl;
    for(
int j=0;j<word.length();j++)
    {
//function checks to see if the word generated from newfile
//matches every one of input's letters
        
if(InWord(word[j],word2))         
        {                                 
            
//cout << "yes.\n";
            
next 0;
        }
        else
        {
            
//cout << "Sorry.  No match.\n";
            
counter++;
            
next 1;
            
loop 1;
            break;  
//makes sure next doesn't get turned back to 0
                                //and also exits the for loop
        
}
    }
    if(
next==1)
    {
        
cout << "Sorry, " << counter << " characters did not match.\n"//# of chars incorrect
        
counter 0;
    }
    if(
next == && word != "")
    {
        
cout << "Congratulations!  You found a match!  Your match is \"" << word << "\"!\n";
        
word2 word;  //continues down the chain of deleting letters, all the way to one letter
    
}

}    
infile.close();
return 
0;



This is my word list, just for reference:
arsenic
carneys
shroud
yearns
earns
test
gopher
near
word
ran
blah
an
i
a


In case the code isn't clear (sorry), I'll try to describe it better for you. Thanks for the help (and yes, this is for my own random nerdy purposes, it isn't for homework...)

Reply With Quote
  #2  
Old February 1st, 2006, 11:43 PM
AniamL AniamL is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 3 AniamL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 53 m 39 sec
Reputation Power: 0
In addition

I also realized that my "anagram finder" function potentially messes up when a letter is repeated in a word... but never mind that, I'll try to fix it once I squash the other bug.

Reply With Quote
  #3  
Old February 4th, 2006, 05:41 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 997 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 26 m 27 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
You can reset the ifstream pointer to the beginning of the file by using:
C++ Code:
Original - C++ Code
  1. newfile.seekg(0,ios_base::beg);


Do that at the end of each loop, and it should solve the problem.
__________________
This is my code. Is it not nifty?

"The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools."
---Douglas Adams


Join the Itsacon fanclub!    
Zero Tolerance: Spammers banned so far: 280

Reply With Quote
  #4  
Old February 4th, 2006, 05:42 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 997 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 26 m 27 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
Note: see here for a full overview of the options in the different stream libraries.

Reply With Quote
  #5  
Old February 8th, 2006, 12:16 AM
AniamL AniamL is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 3 AniamL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 53 m 39 sec
Reputation Power: 0
Quote:
Originally Posted by Itsacon
Note: see here for a full overview of the options in the different stream libraries.


Awesome, thank you. I fixed the problem by means of vectors (which use a lot more memory, I'm assuming) but your help allows me to write much more elegant and precise code. Thanks.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Pretty new programmer with a slightly complex problem...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT