程序跳过getline [英] program skips over getline

查看:197
本文介绍了程序跳过getline的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行这个程序,并选择选项1,它打印 cout 语句 void CTree :: Add()立刻跳过 cin.getline(newPerson-> name,20);

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;
   } 
}


推荐答案

您在 displayMenu 子例程中读取选择,则留下用户输入行的其余部分。具体来说,您留下行结束指示符:'\\\
'
。稍后,当您读取 newperson-> name 时,实际上是检索菜单行的其余部分,而不是 / em>。

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.

您可以使用 istream :: ignore 来使用菜单选项行的其余部分尝试读取名称。

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

displayMenu 的最后两行替换为:

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

这篇关于程序跳过getline的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆