C++音乐专辑打印轨道标题

标签 c++

我有点卡在这个问题上..

头文件**

#include "Duration.h"

class Track
{
private:

    Duration trackTime;
    std::string trackTitle;

public:

    inline Duration getTrackTime() const;

    inline std::string getTrackTitle() const;

    Track(Duration d  = Duration(0,0,0),std::string trackTitle = "");

};

inline std::string Track::getTrackTitle() const
{
    return trackTitle;
}

****cpp 文件../em>****

using namespace std;

Track::Track(Duration trackTime , string trackTitle)
{
    this->trackTitle = trackTitle;
    this->trackTime  = trackTime;

}

istream& operator>>(istream& is, Track & t)
{
    Duration trackTime;
    string trackTitle;

    char c1;

    if (is >> trackTime >> c1 >>trackTitle)
    {
        if(c1 == '-')
        {
            t = Track(trackTime,trackTitle);
        }
        else
        {
            is.clear(ios_base::failbit);
        }
    }

    return is;
}

*****main ****

int main(int argc, const char * argv[])
{
    Track track;
    cin >> track;
    cout << track <<endl;


}

我只是在测试 ostream,这是我所期望的。

但是当我输入这样的字符串时。 “0:03:30 - 嘿,乔(比利·罗伯茨)”

它只打印出“0:03:30 - 嘿”

谁能解释一下为什么打印出来的结果是这样的? 我怎样才能打印出整个轨道标题。?

最佳答案

>>> 运算符输入标记,由空格(空格/制表符/换行符)分隔。您只输入标题的第一个标记,所以这就是您输出的内容。

查看 getline: http://www.cplusplus.com/reference/string/getline/

关于C++音乐专辑打印轨道标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13813118/

相关文章:

c++ - 使用 UINT64_C 的目的?

c++ - 在 C++ (g++) 中使用 errno 作为异常参数的意外控制流(编译器错误?)

c++ - 对比 2013 年 : breakpoint into cppunittest test throws exception

c++ - C++中的递归

c++ - C++ 中的数据抽象和二进制方法

java - 找到最窄间隔的算法,其中 m 将覆盖一组数字

c++ - 与 std::future 关联的存储是如何分配的?

c++ - 获取字符串中的第 i 个字符时遇到问题

c++ - 带指针的可调整大小数组

用于连接 std::vector 和 std::array 类型的 C++ 模板函数