-2

When I run this program, and select option 1, it prints both cout statements in void CTree::Add() at once, jumping over the cin.getline(newPerson->name, 20);

I had the same piece of code in linked list program and it behaved properly, I am really stuck at how to fix this.

    //header file

using namespace std;

struct PersonRec
{
    char name[20];
    int bribe;
    PersonRec* leftLink;
    PersonRec* rightLink;
};


class CTree
{

private:
    PersonRec *tree;
    bool IsEmpty();
    void AddItem( PersonRec*&, PersonRec*);
    void DisplayTree(PersonRec*);

public:
    CTree();
    //~CTree();
    void Add();
    void View();

};

//implementation file`

#include <iostream>
#include <string>

using namespace std;

#include "ctree.h"

CTree::CTree()
{
    tree = NULL;
}

//PersonList::~MyTree()
//{
//
//}


bool CTree::IsEmpty()
{
    if(tree == NULL) 
    {
        return true;
    }
    else
    {
        return false;
    }
}

void CTree::Add()
{
    PersonRec* newPerson = new PersonRec();

    cout << "Enter the person's name: ";
    cin.getline(newPerson->name, 20);
    cout << "Enter the person's contribution: ";
    cin >> newPerson->bribe;


    newPerson->leftLink = NULL;
    newPerson->rightLink = NULL;

    AddItem(tree, newPerson);
}

void CTree::View()
{
    if (IsEmpty())
    {
        cout<<"The list is empy";
    }
    else
    {
        DisplayTree(tree);

    }

};

void CTree::AddItem( PersonRec*& ptr, PersonRec* newPer )
{
        if (tree == NULL)
        {
            ptr = newPer;
        }
        else if ( newPer->bribe < ptr->bribe)
            AddItem(ptr->leftLink, newPer); 
        else
            AddItem(ptr->rightLink, newPer); 
}
void CTree::DisplayTree(PersonRec* ptr)
{
    if (ptr == NULL)
                    return;
    DisplayTree(ptr->rightLink);
    cout<<ptr->name<<" "<<"$"<<ptr->bribe <<endl;
    DisplayTree(ptr->leftLink); 
}

    //driver file
    #include <iostream>

using namespace std;
#include <cstdlib>
#include "ctree.h"

int displayMenu (void);
void processChoice(int, CTree&);

int main (void)
{
int num;
CTree ct;
do 
{
num = displayMenu();
if (num != 3)
processChoice(num, ct);
} while (num != 3);
return 0;
}

int displayMenu (void)
{
int choice;
cout << "\nMenu\n";
cout << "==============================\n\n";
cout << "1. Add student to waiting list\n";
cout << "2. View waiting list\n";
cout << "3. Exit program\n\n";
cout << "Please enter choice: ";
cin >> choice;
return choice;
}

void processChoice(int choice, CTree& myTree)
{
   switch (choice)
   {
      case 1: myTree.Add (); break;
      case 2: myTree.View (); break;
   } 
}
jonsca
  • 10,218
  • 26
  • 54
  • 62
Zzz
  • 2,927
  • 5
  • 36
  • 58
  • 1
    Duplicate of about a million other questions, including but not limited to: http://stackoverflow.com/questions/1744665/need-help-with-getline, http://stackoverflow.com/questions/9336209/mixing-ifstream-getline-and, http://stackoverflow.com/questions/8248239/what-am-i-not-understanding-about-getlinestrings, http://stackoverflow.com/questions/6378662/getline-problem – chris May 18 '12 at 18:20
  • Also: Always check the results of input. Always. – Mooing Duck May 18 '12 at 18:41

2 Answers2

3

After you read choice in the displayMenu subroutine, you leave the remainder of the user's input line. Specifically, you leave the end-of-line indicator: '\n'. Later, when you read newperson->name, you are actually retrieving the remainder of the menu line, and not the name line.

You can use istream::ignore to consume the rest of menu choice line, before trying to read the name.

Replace the last two lines of displayMenu with these:

cin >> choice;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return choice;
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • I am not sure what your are suggesting, could you explain? :) – Zzz May 18 '12 at 18:21
  • @Azzi - here you go. I hope that explains it sufficiently. – Robᵩ May 18 '12 at 18:24
  • The assignment does not allow us to alter the driver file (it was provided by the professor). I used the same driver file for a linked list and read newPerson->name the same way but didn't have this issue. – Zzz May 18 '12 at 18:33
  • @Azzi It looks like this assignment may have a file associated with it, and that is the intended means of input, not input from the keyboard. – Drise May 18 '12 at 18:37
  • @Azzi - If you cannot edit `displayMenu` then you might add the `std::cin.ignore` call to `CTree::Add`, just before you call `getline`. – Robᵩ May 18 '12 at 18:38
  • Thanks !!! Is it ok if I ask you one more question about this assignment? – Zzz May 18 '12 at 18:42
  • @Azzi, if it is unrelated your post or my answer, open a new question. If it related to your post and my answer, go ahead. – Robᵩ May 18 '12 at 18:45
  • Ill start a new post if I can't find the answer to my question...Thanks for your help! – Zzz May 18 '12 at 18:46
0

Adding a

cin.ignore(2000, '\n');

before the input call fixes the problem!

user1895783
  • 381
  • 1
  • 3
  • 8