Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I break the reading this text file (using ifstream)? C++
    primarykey
    data
    text
    <p>I wanna stop the reading of my text input file when the word "synonyms" appears. I'm using ifstream and I don't know how to break the loop. I tried using a stringstream "synonyms" but it ended up junking my bst. I included the complete project files below in case you wanna avoid typing. </p> <p>Important part:</p> <pre><code> for(;;) /*here, I wanna break the cycle when it reads "synonyms"*/ { inStream &gt;&gt; word; if (inStream.eof()) break; wordTree.insert(word); } wordTree.graph(cout); </code></pre> <p>dictionary.txt</p> <pre><code> 1 cute 2 hello 3 ugly 4 easy 5 difficult 6 tired 7 beautiful synonyms 1 7 7 1 antonyms 1 3 3 1 7 4 5 5 4 7 3 </code></pre> <p>Project.cpp</p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;string&gt; #include &lt;sstream&gt; #include "MiBST.h" using namespace std; class WordInfo{ public: //--id accesor int id ()const {return myId; } /* myId is the number that identifies each word*/ //--input function void read (istream &amp;in) { in&gt;&gt;myId&gt;&gt;word; } //--output function void print(ostream &amp;out) { out&lt;&lt;myId&lt;&lt;" "&lt;&lt;word; } //--- equals operator bool operator==(const WordInfo &amp; otherword) const { return myId == otherword.myId; } //--- less-than operator bool operator&lt;(const WordInfo &amp; otherword) const { return myId &lt; otherword.myId; } private: int myId; string word; }; //--- Definition of input operator istream &amp; operator&gt;&gt;(istream &amp; in, WordInfo &amp; word) { word.read(in); } //---Definition of output operator ostream &amp; operator &lt;&lt;(ostream &amp;out, WordInfo &amp;word) { word.print(out); } int main(){ // Open stream to file of ids and words string wordFile; cout &lt;&lt; "Enter name of dictionary file: "; getline(cin, wordFile); ifstream inStream(wordFile.data()); if (!inStream.is_open()) { cerr &lt;&lt; "Cannot open " &lt;&lt; wordFile &lt;&lt; "\n"; exit(1); } // Build the BST of word records BST&lt;WordInfo&gt; wordTree; // BST of word records WordInfo word; // a word record for(;;) /*here, I wanna break the cycle when it reads "synonyms"*/ { inStream &gt;&gt; word; if (inStream.eof()) break; wordTree.insert(word); } wordTree.graph(cout); //wordTree.inorder(cout); system ("PAUSE"); return 0; } </code></pre> <p>MiBST.h (in case you wanna run it)</p> <pre><code>#include &lt;iostream&gt; #include &lt;iomanip&gt; #ifndef BINARY_SEARCH_TREE #define BINARY_SEARCH_TREE template &lt;typename DataType&gt; class BST { public: /***** Function Members *****/ BST(); bool empty() const; bool search(const DataType &amp; item) const; void insert(const DataType &amp; item); void remove(const DataType &amp; item); void inorder(std::ostream &amp; out) const; void graph(std::ostream &amp; out) const; private: /***** Node class *****/ class BinNode { public: DataType data; BinNode * left; BinNode * right; // BinNode constructors // Default -- data part is default DataType value; both links are null. BinNode() : left(0), right(0) {} // Explicit Value -- data part contains item; both links are null. BinNode(DataType item) : data(item), left(0), right(0) {} }; //end inner class typedef BinNode * BinNodePointer; /***** Private Function Members *****/ void search2(const DataType &amp; item, bool &amp; found, BinNodePointer &amp; locptr, BinNodePointer &amp; parent) const; /*------------------------------------------------------------------------ Locate a node containing item and its parent. Precondition: None. Postcondition: locptr points to node containing item or is null if not found, and parent points to its parent.#include &lt;iostream&gt; ------------------------------------------------------------------------*/ void inorderAux(std::ostream &amp; out, BST&lt;DataType&gt;::BinNodePointer subtreePtr) const; /*------------------------------------------------------------------------ Inorder traversal auxiliary function. Precondition: ostream out is open; subtreePtr points to a subtree of this BST. Postcondition: Subtree with root pointed to by subtreePtr has been output to out. ------------------------------------------------------------------------*/ void graphAux(std::ostream &amp; out, int indent, BST&lt;DataType&gt;::BinNodePointer subtreeRoot) const; /*------------------------------------------------------------------------ Graph auxiliary function. Precondition: ostream out is open; subtreePtr points to a subtree of this BST. Postcondition: Graphical representation of subtree with root pointed to by subtreePtr has been output to out, indented indent spaces. ------------------------------------------------------------------------*/ /***** Data Members *****/ BinNodePointer myRoot; }; // end of class template declaration //--- Definition of constructor template &lt;typename DataType&gt; inline BST&lt;DataType&gt;::BST() : myRoot(0) {} //--- Definition of empty() template &lt;typename DataType&gt; inline bool BST&lt;DataType&gt;::empty() const { return myRoot == 0; } //--- Definition of search() template &lt;typename DataType&gt; bool BST&lt;DataType&gt;::search(const DataType &amp; item) const { typename BST&lt;DataType&gt;::BinNodePointer locptr = myRoot; typename BST&lt;DataType&gt;::BinNodePointer parent =0; /* BST&lt;DataType&gt;::BinNodePointer locptr = myRoot; parent = 0; */ //falta el typename en la declaracion original bool found = false; while (!found &amp;&amp; locptr != 0) { if (item &lt; locptr-&gt;data) // descend left locptr = locptr-&gt;left; else if (locptr-&gt;data &lt; item) // descend right locptr = locptr-&gt;right; else // item found found = true; } return found; } //--- Definition of insert() template &lt;typename DataType&gt; inline void BST&lt;DataType&gt;::insert(const DataType &amp; item) { typename BST&lt;DataType&gt;::BinNodePointer locptr = myRoot, // search pointer parent = 0; // pointer to parent of current node bool found = false; // indicates if item already in BST while (!found &amp;&amp; locptr != 0) { parent = locptr; if (item &lt; locptr-&gt;data) // descend left locptr = locptr-&gt;left; else if (locptr-&gt;data &lt; item) // descend right locptr = locptr-&gt;right; else // item found found = true; } if (!found) { // construct node containing item locptr = new typename BST&lt;DataType&gt;::BinNode(item); if (parent == 0) // empty tree myRoot = locptr; else if (item &lt; parent-&gt;data ) // insert to left of parent parent-&gt;left = locptr; else // insert to right of parent parent-&gt;right = locptr; } else std::cout &lt;&lt; "Item already in the tree\n"; } //--- Definition of remove() template &lt;typename DataType&gt; void BST&lt;DataType&gt;::remove(const DataType &amp; item) { bool found; // signals if item is found typename BST&lt;DataType&gt;::BinNodePointer x, // points to node to be deleted parent; // " " parent of x and xSucc search2(item, found, x, parent); if (!found) { std::cout &lt;&lt; "Item not in the BST\n"; return; } //else if (x-&gt;left != 0 &amp;&amp; x-&gt;right != 0) { // node has 2 children // Find x's inorder successor and its parent typename BST&lt;DataType&gt;::BinNodePointer xSucc = x-&gt;right; parent = x; while (xSucc-&gt;left != 0) // descend left { parent = xSucc; xSucc = xSucc-&gt;left; } // Move contents of xSucc to x and change x // to point to successor, which will be removed. x-&gt;data = xSucc-&gt;data; x = xSucc; } // end if node has 2 children // Now proceed with case where node has 0 or 2 child typename BST&lt;DataType&gt;::BinNodePointer subtree = x-&gt;left; // pointer to a subtree of x if (subtree == 0) subtree = x-&gt;right; if (parent == 0) // root being removed myRoot = subtree; else if (parent-&gt;left == x) // left child of parent parent-&gt;left = subtree; else // right child of parent parent-&gt;right = subtree; delete x; } //--- Definition of inorder() template &lt;typename DataType&gt; inline void BST&lt;DataType&gt;::inorder(std::ostream &amp; out) const { inorderAux(out, myRoot); } //--- Definition of graph() template &lt;typename DataType&gt; inline void BST&lt;DataType&gt;::graph(std::ostream &amp; out) const { graphAux(out, 0, myRoot); } //--- Definition of search2() template &lt;typename DataType&gt; void BST&lt;DataType&gt;::search2(const DataType &amp; item, bool &amp; found, BST&lt;DataType&gt;::BinNodePointer &amp; locptr, BST&lt;DataType&gt;::BinNodePointer &amp; parent) const { locptr = myRoot; parent = 0; found = false; while (!found &amp;&amp; locptr != 0) { if (item &lt; locptr-&gt;data) // descend left { parent = locptr; locptr = locptr-&gt;left; } else if (locptr-&gt;data &lt; item) // descend right { parent = locptr; locptr = locptr-&gt;right; } else // item found found = true; } } //--- Definition of inorderAux() template &lt;typename DataType&gt; void BST&lt;DataType&gt;::inorderAux(std::ostream &amp; out, BST&lt;DataType&gt;::BinNodePointer subtreeRoot) const { if (subtreeRoot != 0) { inorderAux(out, subtreeRoot-&gt;left); // L operation out &lt;&lt; subtreeRoot-&gt;data &lt;&lt; " "; // V operation inorderAux(out, subtreeRoot-&gt;right); // R operation } } //--- Definition of graphAux() template &lt;typename DataType&gt; void BST&lt;DataType&gt;::graphAux(std::ostream &amp; out, int indent, BST&lt;DataType&gt;::BinNodePointer subtreeRoot) const { if (subtreeRoot != 0) { graphAux(out, indent + 8, subtreeRoot-&gt;right); out &lt;&lt; std::setw(indent) &lt;&lt; " " &lt;&lt; subtreeRoot-&gt;data &lt;&lt; std::endl; graphAux(out, indent + 8, subtreeRoot-&gt;left); } } #endif </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload