2016-04-17 9 views
-1

何らかの理由で、リストをテキストファイルに出力しているときに、余分な空白ノードがリストの先頭に追加されている以外は、すべてのノードを取得しています。テキストファイルへの余分な出力

(blank), (blank) $(blank) 
Ball, 20 $9.99 

どうすればこの問題を解決できますか?私はwhileループの後に頭を削除してnodePtr/headをインクリメントしてみましたが、役に立たないです。あなたのItemListコンストラクタは常にリストの先頭に新しいノードを挿入するため

#include <iostream> 
#define nullptr 0 
#include <cstdlib> 
#include <algorithm> 
#include <string> 
#include <conio.h> 
#include <fstream> 

using namespace std; 


class ItemList { 
    private: 
     struct ListNode{ 
      string IName; 
      string QQuantity; 
      string PPrice; 
      struct ListNode * next; 
       }; 
ListNode *head; 
    public: 
     ItemList() 
      { 
       head = new ListNode; 
       head->next=nullptr; 
      } 
      ~ItemList(); 

     void insertNode(string Item, string Quantity, string Price) 
      { 
       ListNode *newNode; 
       ListNode *nodePtr; 
       ListNode *previousNode=nullptr; 

       newNode=new ListNode; 
       newNode->IName=Item; 
       newNode->QQuantity=Quantity; 
       newNode->PPrice=Price; 

       if(!head) 
       { 
        head=newNode; 
        newNode->next=nullptr; 
       } 
       else 
       { 
        nodePtr=head; 
        previousNode=nullptr; 

        while(nodePtr != nullptr && nodePtr->IName < Item) 
        { 
         previousNode=nodePtr; 
         nodePtr=nodePtr->next; 
        } 
        if(previousNode==nullptr) 
        { 
         head=newNode; 
         newNode->next=nodePtr; 
        } 
        else 
        { 
         previousNode->next=newNode; 
         newNode->next=nodePtr; 
        } 
       } 
      } 
     void displayNode() 
      { 
       ListNode *nodePtr; 
       nodePtr=head->next; 

       int i=0; 
       if(head->next==nullptr) 
       { 
        cout << "The store is empty." << endl; 
       } 
       while(nodePtr) 
       { 
        i++; 
        cout << i << ". " << nodePtr->IName << ", "; 
        cout << nodePtr->QQuantity << " "; 
        cout << "$" << nodePtr->PPrice << "\n" << endl; 
        nodePtr=nodePtr->next; 
       } 

      } 

     void modifyNode(string Item) 
      { 
      ListNode *nodePtr; 
      ListNode *nodePrev; 
      string newName, newQuantity, newPrice; 
      int modify; 
      if (head==nullptr) 
      { 

       return; 
      } 
      else 
      { 
       nodePtr = head; 
       if (head->IName==Item) 
        nodePtr = head->next; 
       else 
       { 
        while (nodePtr != nullptr && nodePtr->IName != Item) 
        { 
         nodePrev = nodePtr; 
         nodePtr = nodePtr->next; 
        } 
       } 
       if (nodePtr) 
       { 
        system("cls"); 
        cout << nodePtr->IName << "\t" << nodePtr->QQuantity << "\t" << nodePtr->PPrice << endl; 
        cout << "What would you like to change?\n"; 
        cout << "1. Item" << endl; 
        cout << "2. Quantity" << endl; 
        cout << "3. Price" << endl; 
        cout << "4. Whole Entry" << endl; 
        cin >> modify; 
        switch (modify) 
        { 
         case 1: 
          cout << "Change to what?\n"; 
          cin.sync(); 
          getline(cin,newName); 
          transform(newName.begin(), newName.end(), newName.begin(), ::toupper); 
          nodePtr->IName = newName; 
          break; 
         case 2: 
          cout << "Change to what?\n"; 
          cin >> newQuantity; 
          nodePtr->QQuantity = newQuantity; 
          break; 
         case 3: 
          cout << "Change to what?\n"; 
          cin >> newPrice; 
          nodePtr->PPrice = newPrice; 
          break; 
         case 4: 
          cout << "What is the product called?\n"; 
          cin.sync(); 
          getline(cin,newName); 
          transform(newName.begin(), newName.end(), newName.begin(), ::toupper); 
          nodePtr->IName = newName; 
          cout << "How many are there really?\n"; 
          cin >> newQuantity; 
          nodePtr->QQuantity = newQuantity; 
          cout << "What is the actual price?\n"; 
          cin >> newPrice; 
          nodePtr->PPrice = newPrice; 
        } 
        newName=nodePtr->IName; 
        newQuantity=nodePtr->QQuantity; 
        newPrice=nodePtr->PPrice; 
        deleteNode(newName); 
        insertNode(newName, newQuantity, newPrice); 
        cout << "Entry successfully changed." << endl; 
       } 
       else 
        cout << "Product not found\n"; 
      } 
     } 

     void deleteNode(string Item) 
      { 
       ListNode *nodePtr; 
       ListNode *previousNode; 

       if(!head) 
        return; 
       if(head->IName==Item) 
       { 
        nodePtr=head->next; 
        delete head; 
        head=nodePtr; 
       } 
       else 
       { 
        nodePtr=head; 
        while(nodePtr!=nullptr && nodePtr->IName!=Item) 
        { 
         previousNode=nodePtr; 
         nodePtr=nodePtr->next; 
        } 
        if(nodePtr) 
        { 
         previousNode->next=nodePtr->next; 
         delete nodePtr; 
        } 
        else 
        { 
         cout << "Nothing to delete." << endl; 
        } 
       } 
      } 

     void deleteRangeNode(int start, int stop) 
      { 
       ListNode *nodePtr; 
       ListNode *newNode; 
       nodePtr = head; 
       int i=-1; 
       cin.sync(); 

       while(nodePtr!=nullptr) 
       { 
        i++; 

        if((i>=start)&&(i<=stop)) 
        { 
         newNode->next = nodePtr -> next; 
         cout << "Deleted Product: " << nodePtr->IName << endl; 
         delete nodePtr; 
         nodePtr=newNode; 
        } 
        newNode=nodePtr; 
        nodePtr=nodePtr->next; 


       } 

      } 




     void displayRange() 
      { 
       ListNode * nodePtr; 
       nodePtr=head; 
       int i=-1; 
       int start, stop; 

       if(head->next==nullptr) 
       { 
        cout << "The store is empty." << endl; 
       } 
       else 
       { 
        cout << "The display should START at: "; 
       cin >> start; 
       cout << "The display should END at: "; 
       cin >> stop; 
       system("cls"); 
       while(nodePtr!=nullptr) 
       { 
        i++; 
        if((i>=start && i<=stop)) 
        { 
         cout << i << ". " << nodePtr->IName << ", "; 
         cout << nodePtr->QQuantity << " "; 
         cout << "$" << nodePtr->PPrice << "\n" << endl; 
        } 
        nodePtr=nodePtr->next; 
       } 
       } 


      } 

     void deleteAllNodes() 
      { 
       ListNode *temp = head; 
       while(head->next!=nullptr) 
       { 
        temp=head->next; 
        free(head); 
        head=temp; 
       } 
      } 

     void keepNodes() 
     { 
      ofstream FinalInventory; 
      FinalInventory.open("store.txt"); 
      ListNode *nodePtr; 
      nodePtr=head; 

      while(nodePtr!=nullptr) 
      { 
       FinalInventory << nodePtr->IName << ", " << nodePtr->QQuantity << " $" << nodePtr->PPrice << endl; 
       nodePtr=nodePtr->next; 
      } 
      delete head++; 
      FinalInventory.close(); 
     } 
}; 

ItemList::~ItemList() 
{ 
    ListNode *nodePtr; 
    ListNode *nextNode; 

    nodePtr=head; 
    while(nodePtr!=nullptr) 
    { 
     nextNode=nodePtr->next; 
     delete nodePtr; 
     nodePtr=nextNode; 
    } 
} 

int main() 
{ 
    int menu(); 
    ItemList pro; 
    int method; 
    while(method!=0) 
    { 
    int method=menu(); 
    system("cls"); 
    string It, Q, P; 
    int begin, end; 
    switch(method) 
    { 
    case 1: 
     int count; 
     cout << "How many products would you like to put in?" << endl; 
     cin >> count; 
     system("cls"); 
     for(int i=0; i<count; i++) 
     { 
      cout << "Product #" << i + 1 << endl; 
      cout << "Enter the item name: "; 
      cin.sync(); 
      getline(cin,It); 
      transform(It.begin(), It.end(), It.begin(), ::toupper); 
      cout << "Enter the Quantity: "; 
      cin >> Q; 
      transform(Q.begin(), Q.end(), Q.begin(), ::toupper); 
      cout << "Enter the Price: "; 
      cin >> P; 
      pro.insertNode(It, Q, P); 
      cout << "\n"; 
     } 
     break; 

    case 2: 
     int dis; 
     cout << "How many products would you like to display?" << endl; 
     cout << "1. Entire Store" << endl; 
     cout << "2. Range of Products" << endl; 
     cin >> dis; 
     system("cls"); 
     switch(dis) 
     { 
      case 1: 
       pro.displayNode(); 
       break; 
      case 2: 
       system("cls"); 
       pro.displayRange(); 
       break; 
     } 
     break; 

    case 3: 
     pro.displayNode(); 
     cout << "What product do you wish to modify? (by item name)" << endl; 
     cin.sync(); 
     getline(cin, It); 
     transform(It.begin(), It.end(), It.begin(), ::toupper); 
     pro.modifyNode(It); 
     break; 

    case 4: 
     int del; 
     cout << "Do you wish to delete one product or more?" << endl; 
     cout << "1. One" << endl; 
     cout << "2. Range of Products" << endl; 
     cout << "3. Entire Store" << endl; 
      cin >> del; 
      system("cls"); 
      switch(del) 
      { 
       case 1: 
        cout << "What product do you wish to delete? (by item name)" << endl; 
        pro.displayNode(); 
        cin.sync(); 
        getline(cin,It); 
        transform(It.begin(), It.end(), It.begin(), ::toupper); 
        pro.deleteNode(It); 
        cout << "\nItem successfully deleted." << endl; 
        cout << "\n"; 
        break; 
       case 2: 
        pro.displayNode(); 
        cout << "What range of items do you wish to delete? (by number)" << endl; 
        cout << "START: "; 
        cin >> begin; 
        cout << "END: "; 
        cin >> end; 
        pro.deleteRangeNode(begin, end); 
        break; 
       case 3: 
        pro.deleteAllNodes(); 
        cout << "All items deleted." << endl; 
        break; 
      } 
     break; 

    case 5: 
     pro.keepNodes(); 
     cout << "The inventory has been saved." << endl; 
     break; 

    case 0: 
     cout << "Exiting the program." << endl; 
     return 0; 
    } 
    system("pause"); 
    system("cls"); 
    } 
    return 0; 
} 

int menu() 
{ 
    string space1= "     "; 
    string space2= "         "; 
    int method; 
    cout << space1 << "What would you like to do to the store's inventory?" << endl; 
    cout << space2 << "1. Add Product" << endl; 
    cout << space2 << "2. Display Products" << endl; 
    cout << space2 << "3. Modify Product" << endl; 
    cout << space2 << "4. Delete Product" << endl; 
    cout << space2 << "5. Save Inventory" << endl; 
    cout << space2 << "0. Exit\n" << endl; 
    cout << space2; 
    cin >> method; 
    return(method); 
} 

答えて

0

これは、次のとおりです。

 ItemList() 
     { 
      head = new ListNode; 
      head->next=nullptr; 
     } 

ノードのフィールドは、デフォルトで構築すべてブランクにしているstd::string秒です。続行してリストを作成すると、追加のノードが追加されます。次に、リスト全体を印刷します。そして、それはあなたの出力で、リストの冒頭に表示されます。

あなたが私を疑うなら、you should ask your rubber duck

関連する問題