2016-10-05 4 views
1

二重リンクリストの5ノードの情報を入力し、二重リンクリストをアルファベット順に並べ替えるプログラムを設計する必要があります。ユーザーはアドレスを入力することをスキップし、名前以外の情報は印刷せず、アルファベット順にソートしません。私は間違って何をしていますか?プログラムはstd :: getlineをスキップします

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

} 

答えて

1

あなたの問題はあなたが原因改行やものではありませんが、末尾にC++で問題があるcin >>getlineを混合していることです。

常にgetlineを使用し、stringstreamを使用して、行をトークンに分割することをお勧めします。例えば、私はNOTE(getlineの文字列ストリームを使用するソリューション修正:あなたは、ファイルの先頭に#include <sstream>する必要性を

あなたがcin >>getlineを混合しても解決する方法は他の問題についての詳細を読むことができますそれらhere

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; 
} 
関連する問題