编译器无法识别 Getline

Getline not recognized by compiler

本文关键字:Getline 识别 编译器      更新时间:2023-10-16

我在与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 上使用终端。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 数组(你不应该(,你需要使用成员函数版本:

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也为我这样做。