Cin跳过输入整数

时间:2018-04-09 01:14:56

标签: c++ inputstream mergesort cin sstream

这是一个mergesort类型的程序。

如果你看看main中代码的最底部,当我尝试使用cin让用户输入一个整数变量"选择"时,我认为该程序可能从输入中得到了一些东西缓冲。我已尝试过一些东西从输入缓冲区删除任何东西,但没有任何运气来解决这个问题。该程序似乎跳过cin>>选择;好像它不在那里。

我还不完全熟悉我之前在程序中使用stringstream的方式(查找Populate函数)。我想这可能与它有关。任何见解都会非常感激。

#include "stdafx.h"
#include <string>
#include <iostream>

#include <fstream>
#include <sstream>
#include <iomanip>

using namespace std;
int printcount = 1;

class Node
{
public:
    string retailPrice;
    string wholesalePrice;
    string productName;

    Node * next;
    Node(string ins = "", string ins1 = "", string ins2 = "")  // Constructor - assigns new Links(Nodes) an empty title, and assigns the nullptr to "next"
        //indicating that there is no data stored in the following node i.e. "tail"
    {
        productName = ins;
        retailPrice = ins1;
        wholesalePrice = ins2;

        next = nullptr;
    }
};

class List
{
public:
    Node * productList; // "productList" is the address of the first node in the list. 
    List() { productList = nullptr; } // constructor - makes the address of the first node "productList" nullptr i.e. "head"

    bool IsEmpty() //If the address of the first node "theList" is null, returns true
    {
        return (productList == nullptr) ? true : false;
    }

    void Add(string ins, string ins1, string ins2)  //this function adds a new node to the beginning of the linked list. 
    {
        /* Node * Node1 = productList;    //Ask bob why this doesn't work
        while (Node1->next != nullptr) //searches for tail
        {
            Node1 = Node1->next; //Node1 points to the following node
        }

        Node * temp = new Node(ins); //Creates a new Node;
        Node1->next = temp; //assigns the old tail to point to the new node
        */ 

        Node * oldFirst = productList;  //initializies pointer "oldFirst" to point to the current address of the first node, "productList"
        productList = new Node(ins, ins1, ins2);    // creates a new node on the heap with string passed to Add function
                                    // theList is now the address of this new node.
        productList->next = oldFirst;   // the new header "productList"
    }

    void Remove(Node * thisOne) 
    {
        Node * guyBefore = FindTheGuyBefore(thisOne); 
        guyBefore = thisOne->next;  //assigns the "next" address of node preceding node to be deleted, to the address of the node following
                                    // the node to be deleted.
        thisOne->next = nullptr;
    }

    int Populate()
    {
        //cin.ignore();
        string input;
        int count = 0;
        while (getline(cin, input))
        {
            stringstream ss(input);
            getline(ss, input, ',');
            string s = input; //name of product
            ss >> ws;
            getline(ss, input, ',');
            string s1 = input;  //retail price
            ss >> ws;
            getline(ss, input);
            string s2 = input;  //wholesale price
            count++;

            Add(s, s1, s2);
        }

        /*
        char buffer[120];
        string buf(buffer);
        int count = 0;
        while (getline(cin, buf))
        {
            Add(buf);
            count++;
        }
        */
        return count;
    }

    void Visit(void(*fp)(string s, string s1, string s2)) //visits all nodes and applies a void function to a string
    {   
        int count1 = 0;
        Node * currentNode = productList; //assigns 'curLink' the address of 'theList'
        while (currentNode != nullptr) //loop goes until it reaches the tail of the linked list
        {
            (*fp)(currentNode->retailPrice, currentNode->wholesalePrice, currentNode->productName); //applies the function to the title of the node


            currentNode = currentNode->next; //assigns address of 'curLink' the address of the following node
            ++count1;
        }
    }

    void MergeSort(int select)
    {
        if (productList == nullptr) return;         // if the list is empty, terminate the function (sort unnecessary)
        if (productList->next == nullptr) return;   // if there is only one item in the list, terminate the function (sort unnecessary)
        List list1;                                 // Instantiate two lists
        List list2;
        Split(list1, list2);                        // This should leave productList == nullptr. Splits theList into two lists. 
        list1.MergeSort(select);                            //  Performs the MergeSort operation 
        list2.MergeSort(select);                        
        Merge(list1, list2, select);
    }


private:


    Node * FindTheGuyBefore(Node * thisOne)
    {
        Node * aNode = productList;                 //aLink points to productList, the address of the first node
        while (aNode->next != thisOne)              //looks through the list until the address stored in "next" is the address of the value
                                                    //preceding 'thisOne', assigns aLink the value of that address
            aNode = aNode->next;        
        return aNode;

    }

    Node * GetHeadNode()                            //returns the address of the head of the linked list
    {
        Node * answer = productList;                //answer holds the address of theList
        if (answer != nullptr)                      //if the address of theList is not a null pointer
        {
            productList = answer->next;             //theList is now the address of the following node
            answer->next = nullptr;                 //answer points to the nullptr
        }
        return answer;
    }

    void Push(Node * new1)                          //inserts a node at the beginning of the linked list
    {
        if (new1 == nullptr) return;                // nothing to add 
        new1->next = productList;                   // the address pointed to by new1 is the address of the head
        productList = new1;                         // new1 is now the head
    }

    void Split(List & list1, List & list2)          //inserts null pointers between items in a list
    {
        Node * cur0 = nullptr;
        bool left = true;
        while (productList != nullptr)
        {
            cur0 = GetHeadNode();                   //cur0 is the head of the linked list

            if (left)                               // Add cur0 link to the start of list1
            {
                list1.Push(cur0);                   //inserts a node where the value of next is nullptr; essentially splitting the list
                left = false;
            }
            else                                    // Add cur0 link to the start of list2
            {
                list2.Push(cur0);
                left = true;
            }
        }
    }

    Node * GetSmaller(List & otherList, int select)
    {  // Will extra smaller head node from  this List or otherList 
        Node * head1 = productList;
        Node * head2 = otherList.productList;
        if (head1 == nullptr) return otherList.GetHeadNode();
        if (head2 == nullptr) return GetHeadNode();

        if (select == 1)
        {
            if ((head1->productName) < (head2->productName)) return GetHeadNode();
        }

        else if (select == 2)
        {
            if ((head1->retailPrice) < (head2->retailPrice)) return GetHeadNode();
        }

        else if (select == 3)
        {
            if ((head1->wholesalePrice) < (head2->wholesalePrice)) return GetHeadNode();
        }
        return otherList.GetHeadNode();
    }

    void Merge(List & list1, List & list2, int select)
    {// Assumes productList == nullptr
        Node * new1 = list1.GetSmaller(list2, select);      
        productList = new1;  // First guy in list or nullptr
        Node * cur0 = productList; // this must point to last elt added to theList (empty due to split)
        while ((new1 = list1.GetSmaller(list2, select)) != nullptr)
        {
            cur0->next = new1;
            new1->next = nullptr;
            cur0 = new1;
        }
    }
};

void print(string s, string s1, string s2)
{
    cout << "Product # " << printcount << endl;
    cout << left << setw(20) << "Product Name: " << left << setw(20) << s2 << endl;
    cout << left << setw(20) << "Retail Price: " << left << setw(20) << s << endl;
    cout << left << setw(20) << "Wholesale Price: " << left << setw(20) << s1 << endl << endl;
    ++printcount;
};

int main()
    {
        List myProductList;
        cout << "****************************************************************************************************" << endl
            << "           PROGRAM FOR SORTING INVENTORY BY PRODUCT NAME, RETAIL PRICE, OR WHOLESALE PRICE          " << endl
            << "****************************************************************************************************" << endl << endl;

        //POPULATE THE LIST
        cout << "Enter the name, retail price, and wholesale price of a product in that order, seperated by commas." << endl;
        cout << "You may enter multiple products. Seperate each product with a return. When you have finished entering items, enter CTRL-Z. " << endl << endl;
        myProductList.Populate();

        cout << "Would you like to:" << endl << endl;
        cout << "1.  Display your list." << endl;
        cout << "2.  Sort your list." << endl;
        cout << "3.  Add an item to your list." << endl;
        cout << "4.  Delete an item from your list." << endl;
        cout << "5.  Modify an item on your list." << endl;
        cout << "6.  Determine whether your list is empty." << endl << endl << "Enter an integer." << endl << endl;

        int select; 
        cin >> select;



        system("Pause");
        return 0;
    }

1 个答案:

答案 0 :(得分:0)

输入Ctrl + Z结束Populate中的循环后,输入流将处于文件结束状态,任何进一步的输入尝试都将失败。

因此,cin >> select;中的cin不会尝试阅读任何内容,因为class tableData { private $row1 = array("Kalle1", "address1", "postal code1", "1@email.se", "070111001", "08111001"); private $row2 = array("kalle2", "address2", "postcode2", "2@email.se", "070111002", "08111002"); private $row3 = array("kalle3", "address3", "postcode3", "3@email.se", "070111003", "08111003"); private $row4 = array("kalle4", "address4", "postcode4", "4@email.se", "070111004", "08111004"); private $rader; public function __construct() { $this->rader = array( array($this->row1), array($this->row2), array($this->row3), array($this->row4) ); } 报告已经到达输入结束。