Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 程序跳过std::getline_C++_Sorting_Doubly Linked List - Fatal编程技术网

C++ 程序跳过std::getline

C++ 程序跳过std::getline,c++,sorting,doubly-linked-list,C++,Sorting,Doubly Linked List,我必须设计一个程序,允许用户输入双链表中5个节点的信息,然后按字母顺序对双链表进行排序。它跳过了允许用户输入地址的操作,不打印除名称之外的任何信息,也不按字母顺序对列表进行排序。我做错了什么 #include <iostream> #include <string> struct node { std::string firstName; std::string lastName; std::string address; long ph

我必须设计一个程序,允许用户输入双链表中5个节点的信息,然后按字母顺序对双链表进行排序。它跳过了允许用户输入地址的操作,不打印除名称之外的任何信息,也不按字母顺序对列表进行排序。我做错了什么

#include <iostream>
#include <string>

struct node
{
    std::string firstName;
    std::string lastName;
    std::string address;
    long phoneNum;
    node *next;
    node *prev;
};

bool isEmpty(node *head);
void insertAsFirstElement(node *&head, node *&last, std::string firstName, std::string lastName, std::string address, long phoneNum);
void insert(node *&head, node *&last, std::string firstName, std::string lastName, std::string address, long phoneNum);
void searchFor(node *&last, std::string lastName);
void showList(node *current);


bool isEmpty(node *head)
{
    if(head == NULL)
        return true;
    else
        return false;
}


void insertAsFirstElement(node *&head, node *&last, std::string firstName, std::string lastName, std::string address, long phoneNum)
{
    node *temp = new node;
    temp->firstName = firstName;
    temp->lastName = lastName;
    temp->address = address;
    temp->phoneNum;
    temp->next = NULL;
    temp->prev = NULL;
    head = temp;
    last = temp;
}

void insert(node *&head, node *&last, std::string firstName, std::string lastName, std::string address, long phoneNum)
{

    if(isEmpty(head))

        insertAsFirstElement(head, last, firstName, lastName, address, phoneNum);

    else
    {
        node *temp = new node;
        temp->firstName = firstName;
        temp->lastName = lastName;
        temp->address = address;
        temp->phoneNum;

        int result = lastName.compare(last->lastName);

        if (result < 0)
        {
            temp->next = head;
            head->prev = temp;
            temp->prev = NULL;
            head = temp;
        }

        else if ( result > 0)
        {
            temp->next = NULL;
            temp->prev = last;
            last->next = temp;
            last = temp;
        }

    }

}

void searchFor(node *&last, std::string findName)
{
    node *temp = last;
    while (temp != NULL)
    {
        if (temp->lastName == findName)
        {
            std::cout << temp->firstName << " " << temp->lastName << " " << temp->address << " " << temp->phoneNum << std::endl;;
        }

        temp = temp->prev;
    }
}


void showList(node *current)
{
    if(isEmpty(current))
        std::cout << "The list is empty\n";
    else
    {
        std::cout << "The list contains: \n";
        while(current != NULL)
        {
            std::cout << current->firstName << " " << current->lastName << " " << current->address << " " << current->phoneNum << std::endl;
            current = current->next;
        }
     } 
}

int main()
{
    node *head = NULL;
    node *last = NULL;
    std::string firstName;
    std::string lastName;
    std::string address;
    long phoneNum;

    int count;
    count = 5;
    while (count > 0)
    {
        std::cout << "Enter the first name of person #" << count << ":\n";
        std::cin >> firstName;
        std::cout << "Enter the last name of person #" << count << ":\n";
        std::cin >> lastName;
        std::cout << "Enter the address of person #" << count << ":\n";
        std::getline(std::cin,address);
        std::cout << "Enter the phone number of person #" << count << ":\n";
        std::cin >> phoneNum;
        insert(head, last, firstName, lastName, address, phoneNum);
        count = count - 1;
     } 

    showList(head);

    std::string findName;
    std::cout << "What is the last name of the person you would like to find?\n";
    std::cin >> findName;

    searchFor(last, findName);

    return 0; 

}
#包括
#包括
结构节点
{
std::stringfirstname;
std::字符串lastName;
std::字符串地址;
长音素;
节点*下一步;
节点*prev;
};
布尔为空(节点*头部);
void insertAsFirstElement(node*&head、node*&last、std::string firstName、std::string lastName、std::string address、long phoneNum);
void insert(节点*&head,节点*&last,std::string firstName,std::string lastName,std::string address,long phoneNum);
void searchFor(node*&last,std::string lastName);
无效显示列表(节点*当前);
bool isEmpty(节点*头)
{
if(head==NULL)
返回true;
其他的
返回false;
}
void insertAsFirstElement(节点*&头,节点*&尾,std::string firstName,std::string lastName,std::string address,long phoneNum)
{
node*temp=新节点;
temp->firstName=firstName;
temp->lastName=lastName;
临时->地址=地址;
temp->phoneNum;
temp->next=NULL;
temp->prev=NULL;
压头=温度;
最后=温度;
}
无效插入(节点*&头,节点*&尾,std::string firstName,std::string lastName,std::string address,long phoneNum)
{
如果(i空(头))
insertAsFirstElement(头、姓、名、姓、地址、电话号码);
其他的
{
node*temp=新节点;
temp->firstName=firstName;
temp->lastName=lastName;
临时->地址=地址;
temp->phoneNum;
int result=lastName.compare(last->lastName);
如果(结果<0)
{
温度->下一步=头部;
压头->上一个=温度;
temp->prev=NULL;
压头=温度;
}
否则,如果(结果>0)
{
temp->next=NULL;
临时->上一次=最后一次;
最后->下一步=温度;
最后=温度;
}
}
}
void searchFor(node*&last,std::string findName)
{
节点*temp=last;
while(temp!=NULL)
{
如果(temp->lastName==findName)
{

STD::CuffFiSTNEXP>你的问题是你正在混合<代码> CIN > <代码>和<代码> GETLION>代码>,这是C++中的问题,因为后面有新行,而不是。 好的做法是始终使用
getline
,然后使用a将行拆分为令牌。例如,我将您的解决方案修改为仅使用getline和字符串流(注意:您需要在文件顶部包含

您可以阅读更多有关混合
cin>
getline
的问题,以及解决这些问题的其他方法

intmain()
{
node*head=NULL;
node*last=NULL;
std::stringfirstname;
std::字符串lastName;
std::字符串地址;
std::字符串phoneNumStr;
长音素;
整数计数;
计数=5;
而(计数>0)
{
标准::cout
int main()
{
    node *head = NULL;
    node *last = NULL;
    std::string firstName;
    std::string lastName;
    std::string address;
    std::string phoneNumStr;
    long phoneNum;

    int count;
    count = 5;
    while (count > 0)
    {
        std::cout << "Enter the first name of person #" << count << ":\n";
        std::getline(std::cin,firstName);   // no use of cin >>
        std::cout << "Enter the last name of person #" << count << ":\n";
        std::getline(std::cin,lastName);    // no use of cin >>
        std::cout << "Enter the address of person #" << count << ":\n";
        std::getline(std::cin,address);
        std::cout << "Enter the phone number of person #" << count << ":\n";
        std::getline(std::cin,phoneNumStr);
        std::stringstream s(phoneNumStr);    // no use of cin. Using stringstream to break up line and extract it into phoneNum
        s >> phoneNum;
        insert(head, last, firstName, lastName, address, phoneNum);
        count = count - 1;
     }

    showList(head);

    std::string findName;
    std::cout << "What is the last name of the person you would like to find?\n";
    std::cin >> findName;

    searchFor(last, findName);

    return 0;
}