2012-05-18 32 views
-2

当我运行这个程序,并选择选项1,它打印在void CTree::Add()cout报表一次,跳跃在cin.getline(newPerson->name, 20);程序跳过函数getline

我在链表程序相同的代码和它的表现正确地说,我真的被困在如何解决这个问题。

//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; 
    } 
} 
+1

重复约一百万个其他问题,包括但不限于:http://stackoverflow.com/questions/1744665/need-help-with-getline,http://stackoverflow.com/questions/9336209/mixing -ifstream-getline-和,http://stackoverflow.com/questions/8248239/what-am-i-not-understanding-about-getlinestrings,http://stackoverflow.com/questions/6378662/getline-problem – chris

+0

也:总是检查输入的结果。总是。 –

回答

3

您在displayMenu子程序读choice后,你离开了用户的输入行的剩余部分。具体而言,您离开行尾指标:'\n'。之后,当您读取newperson->name时,您实际上正在检索菜单行的其余部分,而不是行的名称行。

在尝试读取名称之前,可以使用istream::ignore来消耗菜单选择行的其余部分。

将最后两行displayMenu这些:

cin >> choice; 
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
return choice; 
+0

我不确定你在暗示什么,你能解释一下吗? :) – Zzz

+0

@Azzi - 你走了。我希望能够充分解释它。 –

+0

该作业不允许我们更改驱动程序文件(它由教授提供)。我使用相同的驱动程序文件作为链接列表,并以相同的方式读取newPerson->名称,但没有此问题。 – Zzz

0

添加

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

输入呼叫解决问题之前!