繁体   English   中英

编译器无法识别Getline

[英]Getline not recognized by compiler

在使用C大约一年之后,我才刚开始使用C ++。 我正在编写一个程序供用户输入有关歌曲的信息。 我读到我应该使用getline()读取带空格的字符串。 这是我的代码:

#include <string>
#include <cstring>
#include <iostream>
using namespace std;

int main()
{
    typedef struct Song
    {
        char title[20];
        char album[20];
        char artist[20];
        int year;
    } song;

    //store song info
    Song Input;
    char inputStr[20];
    int inputYear;
    cout << "Enter the name of a song: ";
    getline(cin, inputStr);
    strcpy(Input.title, inputStr);
    cout << "Enter the album of your song: ";
    getline(cin, inputStr);
    strcpy(Input.album, inputStr);
    cout << "Enter the artist of your song: ";
    getline(cin, inputStr);
    strcpy(Input.artist, inputStr);
    cout << "Enter the year your song was released: ";
    cin >> inputYear;
    Input.year = inputYear;

    //print
    cout << "Song title: " << Input.title << endl;
    cout << "From album: " << Input.album << endl;
    cout << "Artist: " << Input.artist << endl;
    cout << "Released: " << Input.year << endl;
    return 0;
}

我的编译器1抛出3个错误,每个getline()调用一个,尽管我有#include <string> ,但无法识别getline() 我查找了getline()函数的示例用法。

谢谢你的帮助。

1 我想知道这个问题是否可能与编译器支持的C ++标准有关。 我做了一些研究,没有发现任何可以帮助我了解所使用标准的东西。 这是一些信息:

我在Mac上使用Terminal。 g++ version

Configured with: --prefix=/Applications/Xcode.app/Cont.../usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.42)

这些行似乎是这里唯一使用的行,但是我可以提供更多信息。 如果有人对C ++的标准有所了解,无论是C ++ 11,C ++ 14还是其他,这也将非常有帮助。 再次感谢。

更新 :从头开始,尝试在遵循我所知的前提下尽可能多地采纳您的建议。 没有错误,并且按我希望的那样工作。 感谢你的帮助。 新代码:

#include <string>
#include <cstring>
#include <iostream>
    using namespace std;

struct Song
{
    string title;
    string artist;
    string album;
    string year;
}song;

int main()
{
    Song Input;
    cout << "Song? ";
    getline(cin, Input.title);
    cout << "Artist? ";
    getline(cin, Input.artist);
    cout << "Album? ";
    getline(cin, Input.album);
    cout << "Year? ";
    getline(cin, Input.year);

    cout << "Song: " << Input.title << endl;
    cout << "Artist: " << Input.artist << endl;
    cout << "Album: " << Input.album << endl;
    cout << "Year: " << Input.year << endl;
    return 0;
}

您使用的getline版本采用std :: string作为参数,而不是char数组。 如果要使用char数组(并且不应使用),则需要使用成员函数版本:

    cin.getline( some_char_array, array_size );

我会从使用char数组切换到在各处使用string 例如,我将这样编写您的代码:

#include <string>
#include <iostream>

struct Song{
    std::string title;
    std::string album;
    std::string artist;
    int year;
};

int main()
{    
    //store song info
    Song Input;
    int inputYear;
    std::cout << "Enter the name of a song: ";
    getline(std::cin, Input.title);
    std::cout << "Enter the album of your song: ";
    getline(std::cin, Input.album);
    std::cout << "Enter the artist of your song: ";
    getline(std::cin, Input.artist);
    std::cout << "Enter the year your song was released: ";
    std::cin >> Input.year;

    //print
    std::cout << "Song title: " << Input.title << '\n';
    std::cout << "From album: " << Input.album << '\n';
    std::cout << "Artist: " << Input.artist << '\n';
    std::cout << "Released: " << Input.year << std::endl;
    return 0;
}

我的首选是不使用using namespace std; 但没有错。 请注意,直接使用字符串不需要复制任何内容。 我可以使用getline为我做所有的事情。 我也不必担心会超出char数组的大小,因为string也对我有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM