Learn to Create a C++ Word Ladder Program: Step-by-Step Guide

  • Comp Sci
  • Thread starter Math Is Hard
  • Start date
  • Tags
    C++ Program
In summary, you are working on a word ladder program and you are having trouble getting your nodes into a vector. You are also having trouble understanding why you would want to do this.
  • #1
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
4,652
37
Hi, I'm working on a word ladder program outlined here:
http://www.pic.ucla.edu/~nathan/cgi-bin/moin.cgi/la4

To get started, I am just trying to read words from a file and put them into a vector of "node"s (a struct we were given to use).

So far I've only been able to read in each word and place it in a vector of strings(below). Getting them into a vector of nodes is going to be more challenging. I'm a little stuck because I'm having trouble figuring out how to work with a node's previous pointer. I'm also confused about whether the nodes of the vector hold will words or only pointers to words.(If only pointers, then where are the words stored?) The diagram of the vector is giving me trouble.

Here's what I did so far. Any help is appreciated. Thanks.

Code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

struct Wnode {string word; Wnode * prev;};

int main () 
{
	vector<string> v;	
	string word;	
	ifstream in("example.txt");

	if (in.is_open())
	{
		while(!in.eof())
		{
			in >> word; 
			v.push_back(word);
			cout << word << " "; //just a test				
		}
		in.close();
	}
	else cout << "Unable to open file"; 
	cout << v[0] <<v[1] << v[2]; //test printing vector

//messing around with nodes
	Wnode w;	
	//w.prev -> NULL; //doesn't work
	w.prev = NULL;
	w.word = "one";	

	Wnode x;
	//x.prev -> w; //doesn't work
	x.word = "two";
	return 0;
}
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
Hrm, that file i/o code doesn't give you an extra, empty word at the end of your list?


I'm also confused about whether the nodes of the vector hold will words or only pointers to words.(If only pointers, then where are the words stored?)
The definition of Wnode settles that question, doesn't it?


Getting them into a vector of nodes is going to be more challenging.
Is it? You just put the words in your nodes, and the nodes in your vector.

Anyways, why are you making a vector of nodes? There are reasons one might do that, but your homework assignment isn't suggesting you do such a thing.


//w.prev -> NULL; //doesn't work
Well, let's deconstruct this expression.

w is an (uninitialized) object of type Wnode.

A Wnode has a member named prev of type Wnode*.

Therefore, w.prev is an uninitialized object of type Wnode*.

Wnode does not have a member named NULL.

Therefore, w.prev->NULL attempts to reference a nonexistent field of the object Wnode object to which w.prev points.


And, as additional logic errors, w.prev is an uninitialized pointer, so attempting to dereference it leads to undefined behavior. And if w.prev was initialized, and Wnode did have a member named NULL, the expression w.prev->NULL would do nothing.



(P.S. have you ever made a linked list before?)
 
Last edited:
  • #3
Hurkyl said:
(P.S. have you ever made a linked list before?)
I have, actually, and I think that's part of my confusion. I was thinking about it when I was working on this and I think I've forgotten how it all works!:redface: (not that I understood it that well to begin with)

Thanks for breaking it down.
 
  • #4
so I guess this is legal:

Wnode w;
w.prev = NULL;
w.word = "one";

Wnode x;
x.prev = &w;
x.word = "two";
 
  • #5
eeyay! got it! (the part I was trying to do anyway)

thanks, Hurkyl!
 
  • #6
Yay! :smile:
 

1. What is a "C++ word ladder program"?

A "C++ word ladder program" is a computer program written in the C++ programming language that generates a sequence of words, starting from a given start word and ending at a given end word, with each word in the sequence differing by only one letter from the previous word. This type of program is often used for word games or language puzzles.

2. How does a "C++ word ladder program" work?

A "C++ word ladder program" works by using a combination of data structures and algorithms to generate a path between the start word and the end word. It typically uses a dictionary of words to check for valid steps in the ladder and a graph data structure to keep track of the connections between words.

3. Can I create my own "C++ word ladder program"?

Yes, you can create your own "C++ word ladder program" by learning the C++ programming language and understanding the necessary data structures and algorithms. There are also many online resources and tutorials available to help guide you in creating your own program.

4. What are some potential applications of a "C++ word ladder program"?

A "C++ word ladder program" can be used for various applications, such as word games, language learning exercises, and even natural language processing tasks. It can also be modified to generate word ladders for different languages or to incorporate additional features, such as scoring systems.

5. Are there any limitations to a "C++ word ladder program"?

One limitation of a "C++ word ladder program" is that it relies on having a comprehensive dictionary of words to generate the ladder. If the dictionary is incomplete or contains misspelled words, the program may not be able to generate a valid ladder. Additionally, the program may take longer to run for longer word ladders, as it needs to check for more possible steps between the start and end words.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
21
Views
4K
  • Programming and Computer Science
2
Replies
49
Views
10K
Back
Top