首页 > 解决方案 > 字符串和在 C++ 中获取输入的方法

问题描述

目前我正在做一些接受用户输入文件并制作对象指针链接列表(4个单独文件)的东西。在处理一个名为 lookup 的函数时,该函数遍历链表以查看传入的标题是否在链表内,我注意到我存储的所有标题都比它们的长度长 1 个字符。例如,在我查找“Spaces VS Tabs”(18) 的情况下,我在 vlist.cpp 的查找函数中调用 tempPtr->getTitle(),然后获取它的长度。它返回的长度为 19,我对此感到非常困惑。需要明确的是,存储在异常长度的对象中的标题;你可以在 main.cpp 中查看我是如何得到它们的。

到目前为止,我一直在使用奇怪数量的 cin.ignore()s 来获取我的输入,所以我觉得可能是这样。除此之外,我不知道如何在我的标题中添加额外的字符。

主文件

#include <iostream>
using namespace std;
#include "vlist.h"
#include "video.h"

int main()
{
    string firstLine, secondLine, thirdLine, userPreference;
    float fourthLine;
    int fifthLine;
    VList list;

    string lookUp = "Spaces Versus Tabs";

    cin >> userPreference; //Why do I need two cin.ignore()'s after this? WHO KNOWS?!
    cin.ignore();
    cin.ignore(); 

    while(cin.peek() != EOF) {
        getline(cin, firstLine);
        getline(cin, secondLine);
        getline(cin, thirdLine);
        cin >> fourthLine;
        cin >> fifthLine;
        cin.ignore();
        cin.ignore();
        Video * tempVid = new Video (firstLine, secondLine, thirdLine, fourthLine, fifthLine); // Assign object
        list.insert(tempVid);
    }
    list.lookup(lookUp);
    return 0;
}

vlist.cpp

#include <iostream>
using namespace std;
#include "vlist.h"

VList::VList() {
    m_head = NULL;
}

VList::~VList() {
    Node *ptr = m_head;
    while (ptr != NULL) {
        Node *temp;

        temp = ptr;
        ptr = ptr->m_next;
        delete temp;
    }
}

void VList::insert(Video *myVid) {
    m_head = new Node(myVid, m_head);

    int occurance = 0;
    Node *ptr = m_head;
    Node *staticPtr = m_head;
    Video *tryingToAdd = staticPtr->m_vid;

    while (ptr != NULL) {
        Video *tempPtr = ptr->m_vid;
        //Look for title, if you find it, occurance ++
        cout << tryingToAdd->getTitle() << endl;//This only works if I have << endl; at the tail of it. Why?
        if(occurance > 1) {
            cout << "Couldn't insert video <" << tempPtr->getTitle() << ">, already in list.";
            return;
        }
        ptr = ptr->m_next;
    }

}

void VList::length() {
    //ADD IN A CHECK FOR EMPTY LIST HERE
    int lengthCounter = 0;
    Node *ptr = m_head; 
    while (ptr != NULL) {
        lengthCounter++;
        ptr = ptr->m_next;
    }
    cout << lengthCounter << endl;
}

void VList::lookup(string title) {
    bool isInList = false;//Saftey check
    Node *ptr = m_head; 
    while (ptr != NULL) {
        Video *tempPtr = ptr->m_vid;
        string compareMe = tempPtr->getTitle();
        cout << compareMe.length() << " - " << compareMe << endl;
        if(title.compare(compareMe) == 0) {
            tempPtr->print();
        }
        ptr = ptr->m_next;
    }
    if(!isInList) {
        cout << "Title <" << title << "> not in list." << endl;
        cout << title.length() << endl;
    }
}

void VList::remove(string title) {
    bool isInList = false;//Saftey check
    Node *ptr = m_head; 
    while (ptr != NULL) {
        Video *tempPtr = ptr->m_vid;
        string compareMe = tempPtr->getTitle();
        if(title.compare(compareMe) == 0) {
            //REMOVE CURRENT VIDEO
        }
        ptr = ptr->m_next;
    }
    if(!isInList) {
        cout << "Title <" << title << "> not in list, could not delete." << endl;
    }
}

void VList::print() {
    Node *ptr = m_head; 
    while (ptr != NULL) {
        Video *tempPtr = ptr->m_vid;
        tempPtr->print();//Prints ALL information for a single video
        ptr = ptr->m_next;
    }
}

vlist.h

#ifndef VLIST_H
#define VLIST_H
#include "video.h"

class VList
{
    public:
        VList();
        ~VList();
        void insert(Video *myVid);
        void print();
        void length();
        void lookup(string title);
        void remove(string title);
    private:
        class Node
        {
            public:
                Node(Video *myVid, Node *next) {    
                    m_vid = myVid; 
                    m_next = next;
                }
                Video *m_vid;
                Node *m_next;
        };
        Node *m_head;   
};

#endif

视频.cpp

#include "video.h"
#include <iostream>

using namespace std;

Video::Video(string title, string URL, string comment, float length, int rating) {
    vidTitle = title;
    vidURL = URL;
    vidComment = comment;
    vidLength = length;
    vidRating = rating;
}

void Video::print() { //Print entire video object information
    cout << getTitle() << endl;
    cout << getURL() << endl;
    cout << getComment() << endl;
    cout << getLength() << endl;
    cout << getRating() << endl;
    return;
}

视频.h

#ifndef VIDEO_H
#define VIDEO_H

#include <string>
#include <iostream>

using namespace std;

class Video
{
    public:
        Video(string title, string URL, string comment, float length, int rating);
        int getRating() {
            return vidRating;
        }
        float getLength() {
            return vidLength;
        }
        string getTitle() {
            return vidTitle;
        }
        string getURL() {
            return vidURL;
        }
        string getComment() {
            return vidComment;
        }
        void print();
    private:
        string vidTitle, vidURL, vidComment, vidPreference;
        float vidLength;
        int vidRating;
};

#endif

标签: c++stringcingetline

解决方案


推荐阅读