   cin/getline problem [c++]
  My problem is in the getString() function at the bottom of the code.. I've added the cout line there just for debugging.. and the first time the getString() function is called (inside the traverseTree function) it prints out nothing and the second time its called it print out what was inputed last time it was called.. I guess it does not read the the new line char or something? But I got no clue how to fix this.. can anyone give an example with some explanations please?#include "DecisionTree.h"#include <iostream>#include <string>DecisionTree::DecisionTree(){Treenode* horse = new Treenode(NULL,NULL, "Horse");Treenode* bird = new Treenode(NULL,NULL, "bird");m_rootNode = new Treenode(bird, horse, "Does it have two legs?");}void DecisionTree::begin(){cout << "You must think of a single object. Pleas answere the following questions about this object." << endl;traverseTree(m_rootNode);}bool DecisionTree::validInput(char &input){if(input == 'y' || input == 'Y' || input == 'n' || input == 'N')return true;else{cout << "INCORRECT RESPONSE - Pleas type Y/N" << endl; return false;}}bool DecisionTree::getYesNoResponse(){char input;while(cin >> input && !validInput(input)){}return(input == 'y' || input == 'Y');}void DecisionTree::traverseTree(Treenode *nodePtr){if(nodePtr->getYesNode() == NULL && nodePtr->getNoNode() == NULL){cout << "I guess that your object is a(n) " << nodePtr->getQa() << " (Y/N): ";if(getYesNoResponse())cout << "Notice the superior intellect of the computer!" << endl;else{cout << "What object were you thinking of?: ";Treenode *newObject = new Treenode(NULL, NULL, getString());Treenode *curentCopy;curentCopy = nodePtr;cout << "Pleas specify a question that has a yes answere for your object and a no answere for my guess" << endl;nodePtr->setYesNode(newObject);nodePtr->setNoNode(curentCopy);nodePtr->setQa(getString());}}else{cout << "Question: " << nodePtr->getQa() << endl;if(getYesNoResponse())traverseTree(nodePtr->getYesNode());elsetraverseTree(nodePtr->getNoNode());}return;}string DecisionTree::getString(){string input;getline(cin, input);cout << "print: "<< input << endl;return input;}
  Your using "input" as variable to print.However, this variable is a local variable, and therefore will always be recreated (empty in this case) when you call your getString() function.To solve this, make input a global variable(then it does not get destroyed and remade), and pass it to the getString() method as a parameter.;Quote:Original post by DieseltjuhYour using "input" as variable to print.However, this variable is a local variable, and therefore will always be recreated (empty in this case) when you call your getString() function.To solve this, make input a global variable(then it does not get destroyed and remade), and pass it to the getString() method as a parameter.Um, no.input is initialized by the getline call, displayed via the cout call, and finally a copy of the string is returned from the getString function. This is perfectly legitimate and works fine.The problem (I expect) is that getline reads until it hits a newline. However, in certain situations the last thing that the stream has done is pull off a single char (for example if getYesNoResponse is called). This leaves a dangling newline on the input stream, and that is the first thing you read when you call getline.The fix should be easy: do { getline(cin, input); } while(input.empty());   ;  Sry bout that, i didn't took enough time to read it well, i thought it was an easy mistake so i stopped reading.   ;Quote:Original post by DieseltjuhSry bout that, i didn't took enough time to read it well, i thought it was an easy mistake so i stopped reading.I appreciate your willingness to help; however, please remember that here in the For Beginners forum we hold posters to a higher standard of accuracy and correctness, since we would like to avoid teaching new programmers bad lessons [smile]   ;  Thanks.. that worked.. kind of .. :) Now I have to press enter twice some times tho.. how can I fix that?Also.. for my assigment operator.. on the linesTreenode *curentCopy;curentCopy = nodePtr;I actualy want to make a new node in curentCopy, but as it is now it only points to it.. I can see why how the code is written now.. but I've tried a few other things but can't get it right.. this is what I've tried;Treenode curentCopy(nodePtr);---Treenode curentCopy;curentCopy = nodePtr;--Treenode curentCopy = new Treenode(nodePtr);---Right now Im just making a new object. This works, but I want to get the copy constructor/assignment operator to work aswell..Treenode *curentCopy = new Treenode(NULL,NULL, nodePtr->getQa());;  oops sorry.. the copy constructor is in treenode.cpp:#include "Treenode.h"Treenode::Treenode(const Treenode &rhs){copy(rhs);}void Treenode::copy(const Treenode &rhs){    m_yes = rhs.getYesNode();    m_no = rhs.getNoNode();m_questionOrAnswere = rhs.getQa();}const Treenode& Treenode::operator=(const Treenode &rhs){    copy(rhs);    return *this;}Treenode::Treenode(){m_yes = NULL;m_no = NULL;}Treenode::Treenode(Treenode *yesPtr, Treenode *noPtr,string q_aStr){m_questionOrAnswere = q_aStr;m_yes = yesPtr;m_no = noPtr;};  bump :)   ;  You really need here to not look for an easy fix, and instead first understand how the console input works.By default, everything the character types goes through the console window before your program sees it. This is a good thing. It allows the user to type backspace and actually correct the input, instead of sending wrong input to your program followed by a backspace key code. There are ways to turn this off, but it's not something you want to get into (and not really portable, either).In order to make this work, things that the user types are only made available to the program a line at a time.When the program reads from the console, it reads until it either has enough data to satisfy the read request (e.g. if you read into a std::string with operator>>, it will skip past any leading whitespace, read any non-whitespace characters, and then stop as soon as it sees any kind of whitespace), or has seen enough data to know that the read is impossible (e.g. if you read into an int with operator>>, and the next available character is 'q', it stops immediately, because 'q' can't possibly be part of text that represents an integer). If the read is impossible, for primitive types, anything read so far will be "put back", and the stream is put into a "failed" state, in which it will refuse to read any more until that state is cleared. This is so you have a way to find out what happened.If all of the currently available data (there might or might not be some) is insufficient, then the program waits until more data arrives. It will arrive a line at a time, but the program doesn't know this. It just processes characters in the order that the console feeds them.The program does not unconditionally wait for the user to type something, because the program does not know there is a user typing anything. It only knows about what the console is feeding it. It does not discard the rest of a line that is fed in from the console. Everything that the program receives from the console will be read, in order. That includes all forms of whitespace, including newlines.When the program reads things with operator>>, in general, it will skip past any leading whitespace, but does not skip past any trailing whitespace. Including newlines. It is left on the stream for the next read, and will be immediately available.When you use std::getline(), it reads from the input, not skipping leading whitespace, until the first newline. If the very first available character is a newline, that doesn't matter - an empty string is a perfectly valid line. It reads the newline and is done immediately.As for your copy constructor, you are currently copying the pointers from the node being copied, not the pointed-at nodes. Of course, the pointed-at nodes are themselves TreeNode instances, so you can use the copy constructor to copy them - until you get to a null pointer, at which point you stop. You are familiar with recursion, I hope?   ;Quote:Original post by NanookMy problem is in the getString() function at the bottom of the code.. I've added the cout line there just for debugging.. and the first time the getString() function is called (inside the traverseTree function) it prints out nothing and the second time its called it print out what was inputed last time it was called.. I guess it does not read the the new line char or something? But I got no clue how to fix this.. can anyone give an example with some explanations please?*** Source Snippet Removed ***could you post all your code here. Then we can sort it out better.   