473,698 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

long post - need help with beginner level EBKAC logic error

I am writing an anagram program for my fiance. Figured it would be an
excellent task to learn from. The way it is supposed to work is it
reads in a word list from a file into a temporary vector<string>. From
that it selects a random word 6 letters or more long. That is the word
of the game - the one to make all the anagrams from. After it selects
a word, I initialize two more vector<string>' s - unplayed_anagra ms and
played_anagrams . I want it to read the file a second time, testing
each line in to see if it an anagram of the game word, and if it is,
push it into a vector<stringth at will contain the entire list of
possible anagrams. As the player inputs a word, check the word against
unplayed_anagra ms and if it is a match remove the word from
unplayed_anagra ms and move it to played_anagrams .

All of the parts of this work so far except one, the part that
populates unplayed_anagra ms. Now I am sure there are about
305,657,828 better ways of doing this, but like I said - it is a
learning exercise. I am not familiar with the more advanced features
(read: classes and up) of C++ yet. I just want to get this working and
then refine it down as I learn more.

Oh - I want it to find the anagrams by loading the game word and the
line from the file into char arrays - word_array and test_word_array
(respectively.) After that I just flip through the elements in the
test_word_array and compare them to the elements in word_array one by
one. The elements that match I want to test against a vector<intof
elements that have already been used. It should do that by comparing
the element from word_array against the vector<intand if no match is
found, push it in to the vector<intand decrease a running counters
of letters_left. If there letters_left == 0 then push the word onto
unplayed_anagra ms. Rinse, repeat - one for every line. That is the
point of all the nests (really really nasty nests, but like I said -
still learning). That was how it was supposed to work anyways. Guess
what - nada. It tests each letter ok, but doesn't work in the
comparison to the vector<int>. It fails to check the numbers
correctly, as well as keep any word from filtering through.

Here is the lone broken function:

//code

void populate( vector<string>& unplayed_anagra ms, string& word,
ifstream& word_file )
{
const int max_size = word.size() + 1;
int letters_left;
char word_array[max_size];
char test_word_array[max_size];
string measure_word;
vector<intused_ letters;
vector<int>::it erator iter;

used_letters.cl ear();
memset( word_array, '\0', max_size);
strcpy( word_array, word.c_str() );

cout << "word_array in populate() is: "; //debug code
for(int z = 0; z < max_size; ++z) //debug code
{ //debug code
cout << word_array[z]; //debug code
} //debug code
cout << endl; //debug code

cout << "\nPlease wait... Populating the arrays." << endl;
while( getline(word_fi le, measure_word) )
{
letters_left = (max_size - 1 ) ;
used_letters.cl ear();
if( measure_word.si ze() word.size() )
{
cout << "Stepping into the if." << endl; //
debug code
continue;
}
else if( measure_word.si ze() < 3 )
{
cout << "Stepping in to the first else if." <<
endl; //debug code
continue;
}
else if( measure_word == word )
{
cout << "Stepping in to the second else if." <<
endl;//debug code
continue;
}
//int size_m = measure_word.si ze();
//memset( test_word_array , '\0', size_m );
strcpy(test_wor d_array, measure_word.c_ str());

cout << "word is: " << word << endl;//debug code
cout << "test_word_arra y is: "; //debug code
for( int x = 0; x < measure_word.si ze(); ++x ) //
debug code
{ //debug code
cout << test_word_array[x];//debug code
} //debug code
cout << endl; //debug code

for( int x1 = 0; x1 < max_size; ++x1) //word_array
{
cout << "This is in the first for (x1)." <<
endl; //debug code
for( int x2 = 0; x2 < max_size; ++x2 ) //
test_word_array
{
cout << "This is in the second for (x2)."
<< endl; //debug code
if( test_word_array[x2] ==
word_array[x1] ) //test if letter in test_word_array is the same as
the letter in word_array
{
for( iter = used_letters.be gin();
iter != used_letters.en d(); ++iter ) //this chuck checks to make sure
the letters

{ //
that match aren't matching to an
cout << "Checking vector of
used letters." << endl; //debug code
cout << "used_lette rs *iter
is: " << *iter << endl; //debug code
if( x2 !=
*iter ) //
element that has been previously matched.
{
continue;
}
else
{
break;
}
}
if( iter ==
used_letters.en d() ) //if no matches are found, place the element
that matched into used_letters
{
int break_out = 0;
while( iter !=
used_letters.be gin() )
{
iter -= 1;
if( x1== *iter )
{
break_out = 1;
break;
}
else
{
continue;
}
}
if( break_out == 1 )
{
break;
}
used_letters.pu sh_back(x1);
--letters_left;
}
else
{
continue;
}

}
else
{
continue;
}
}
}
cout << "letters_le ft is: " << letters_left << endl; //
debug code
if( letters_left == 0 )
{
string blah = test_word_array ;
cout << "blah is: " << blah << endl;//debug code
unplayed_anagra ms.push_back(bl ah);
}
else
{

cout << "used_lette rs is: "; //debug code
for( iter = used_letters.be gin(); iter !=
used_letters.en d(); ++iter ) //debug code
{ //debug code
cout << *iter; //debug code
} //debug code
cout << endl; //debug code

continue;
}
}
cout << "\nDone." << endl;
}

//code

If you need I can post the calling test code. Thanks for any help or
information.

Raymond

Mar 11 '07 #1
2 1929
On 11 Mar 2007 10:54:38 -0700 in comp.lang.c++,
th************* @gmail.com wrote,
>I am writing an anagram program for my fiance. Figured it would be an
excellent task to learn from.
I'm afraid I did not figure out exactly what is not working in your
code, but maybe I can make some suggestions.

You start with std::string holding your word. That's very good, I'd
stick with that and use nothing but std::string to hold your words and
anagrams. No char arrays, no int vectors. Don't forget string supports
operator[] and a lot more.

Next, your nested loops. Too complicated. Say you have two strings and
you wish to do something depending on the relationship between them.
That would look like

if (adverb(game_wo rd, test_word)) {

where adverb() is a small function you supply (with an appropriate name)
that encapsulates the relationship you are looking for and gives it a
name. Unless, of course, the relationship is pre-coded like
string::operato r==(). Not some inline loop looking char-by, leading to
hundred-line functions with nested loops six levels deep.

Working with anagrams you almost always encounter a point where it is
useful to sort the characters of a word; for example that allows you to
easily check if one word is an anagram of another. A function that does
that:
std::string sorted(std::str ing const & word)
{
std::string result(word);
std::sort(resul t.begin(), result.end());
return result;
}

Example usage:
bool anagram_match(s td::string const &first, std::string const &second)
{
return sorted(first) == sorted(second);
}

Example usage of that:
if(anagram_matc h(game_word, test_word)) ...

Giving particles like that reasonable makes the code easier to
understand the bigger picture, and short functions make it easier to
tell if the code does what the name says,.

Mar 12 '07 #2
Next, your nested loops. Too complicated. Say you have two strings and
you wish to do something depending on the relationship between them.
That would look like

if (adverb(game_wo rd, test_word)) {

where adverb() is a small function you supply (with an appropriate name)
that encapsulates the relationship you are looking for and gives it a
name. Unless, of course, the relationship is pre-coded like
string::operato r==(). Not some inline loop looking char-by, leading to
hundred-line functions with nested loops six levels deep.
Actually, I felt like someone had hit me with the stupid stick when I
thought of that last night. Sat on my lunch and completely re-wrote
that rat's nest into cleaner functions. Made it easier to understand
too.
Working with anagrams you almost always encounter a point where it is
useful to sort the characters of a word; for example that allows you to
easily check if one word is an anagram of another. A function that does
that:
std::string sorted(std::str ing const & word)
{
std::string result(word);
std::sort(resul t.begin(), result.end());
return result;

}
That's nice to know - that will make it soooo much easier. Thanks for
the heads up.

Raymond

Mar 12 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4257
by: TG | last post by:
Dear PHP Group, I have two forms that are used to collect user information. The first one takes user inputted values such as fullname, city, address etc. I want these values to display in the second form when it is called. Both forms are .htm files that call themselves when the submit button is press via the following command in each form: <form method="post" action="<?php $server?>">
4
6361
by: Tim Jarman | last post by:
Apologies in advance for the long post - I wanted to be sure I included all the relevant details. The answer is probably very, very simple. I am doing something stupid here, but I don't know what it is. I'm writing an application with a Tkinter GUI (Python 2.4, Tcl/Tk 8.4.) and I want to put a custom icon on the main window. I've followed (so far as I understand it) the recipe in the eff-bot's splendid Introduction to Tkinter - see:...
11
5298
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our 4-month old C#/MC++ .NET project project. I'd like to thank in advance anyone who takes the time to read and/or respond to this message. At a couple points, it may seem like a rant against C# / .NET, but we are pretty firmly stuck with this approach...
3
14511
by: RAJESH | last post by:
I am working with c# and asp.net in developing web applications, iam using ..netframework 1.1 ,i want to know what is the need of 3-tier or 4-tier architecture in our application development.what is its part when some body is using our application.plese give links where i can find the answer.
20
6614
by: Keith G. Murphy | last post by:
I'm trying to get a feel for what most people are doing or consider best practice. Given a mod_perl application talking to a PostgreSQL database on the same host, where different users are logging onto the web server using LDAP for authentication, do most people 1) have the web server connecting to the database using its own user account (possibly through ident), and controlling access to different database entities strictly through...
52
5973
by: lcw1964 | last post by:
Greetings, all, I am trying to port a little bit of math code to gcc, that in the original version used the long double version of several functions (in particular, atanl, fabsl, and expl). I get a complie-time "unidentified reference" error to the expl() calls, but gcc seems to digest atanl and fabsl just fine. Changing expl to exp cures the compile time problem, but I get at best double precision in the final results. I am assuming...
8
5086
by: Filipe Marcelino | last post by:
Hi, I'm trying to create a textbox inheriting from the standard textbox. I would like to: 1. repaint the textbox border; 2. define a color for that border; Till now I made this:
4
12705
by: Natalia | last post by:
Hello, I need to provide the ability to post file and some form elements via our website (asp.net) to the third party website (asp page). On http://aspalliance.com/236#Page4 - I found great advices but still having troubles... it might some obvious error that I am making but I just dont see it. ==================FIRST - Webclient=================================
56
7220
by: UKuser | last post by:
Hi, I'm not sure if this can be done as I've searched the web and this forum. I am using an online merchant provider and I must post certain variables to their webforms through a form on my website. The issue is that I need to gauge whether a user has any items in their basket to decide which page I redirect them too. I could
0
8678
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9166
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.