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

  • Context: Comp Sci 
  • Thread starter Thread starter Math Is Hard
  • Start date Start date
  • Tags Tags
    C++ Program
Click For Summary

Discussion Overview

The discussion revolves around creating a C++ program for a word ladder, specifically focusing on reading words from a file and storing them in a vector of a custom struct called "Wnode". Participants explore issues related to file input/output, struct initialization, and the use of pointers within the struct.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant expresses difficulty in reading words from a file into a vector of "Wnode" structs and questions how to handle the "prev" pointer within the struct.
  • Another participant questions whether the nodes in the vector hold words or pointers to words, suggesting that the definition of "Wnode" clarifies this.
  • Concerns are raised about the potential for an extra empty word being added to the list during file reading.
  • There is a discussion about the initialization of the "prev" pointer and the implications of dereferencing uninitialized pointers, with an emphasis on avoiding undefined behavior.
  • A participant confirms their prior experience with linked lists, indicating that this knowledge may be contributing to their confusion.
  • One participant demonstrates correct initialization of the "Wnode" struct and the use of pointers to link nodes together.
  • Another participant expresses satisfaction upon resolving their issue with the program.

Areas of Agreement / Disagreement

Participants generally agree on the technical aspects of struct initialization and pointer usage, but there are varying levels of understanding and confusion regarding the implementation details. The discussion remains somewhat unresolved regarding the best approach to structuring the vector of nodes.

Contextual Notes

There are limitations in the discussion regarding the handling of file input/output and the potential for uninitialized pointers leading to undefined behavior. The exact requirements of the homework assignment are not fully detailed, leaving some ambiguity in the approach to take.

Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
Messages
4,663
Reaction score
36
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
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:
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.
 
so I guess this is legal:

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

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

thanks, Hurkyl!
 
Yay! :smile:
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 49 ·
2
Replies
49
Views
12K