- #1
- 4,616
- 37
Hi, I'm working on a word ladder program outlined here:
http://www.pic.ucla.edu/~nathan/cgi-bin/moin.cgi/la4 [Broken]
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.
http://www.pic.ucla.edu/~nathan/cgi-bin/moin.cgi/la4 [Broken]
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: