我在使用 C++ 中的 getline() 函数时遇到问题
I am having trouble with getline() function in C++
您好,我在使用 C++ 中的 getline() 函数时遇到问题我是 C++ 的新手,对编程时期也比较陌生。我正在学习 C++ 课程的介绍,我自己在网上对这个主题所做的研究并没有让我找到解决方案我并不真正理解他们所说的所有事情,因为这是我 class。任何 quick/rough help/understanding 都将不胜感激,因为我们将在 class 中更全面地介绍这个主题。下面是我正在编写的代码。这是一个简单的程序,只要求用户提供姓名和地址,然后以正确的邮件格式显示该信息。
#include <iostream>
#include <string>
int main()
{
// Variables for Mailing Addresses
std::string firstName;
std::string lastName;
int houseNum;
std::string streetName;
std::string cityName;
std::string state;
int zipCode;
// Asking for input
std::cout << "What is your First name?: ";
std::cin >> firstName;"\n";
std::cout << "What is your Last name?: ";
std::cin >> lastName;"\n";
std::cout << "What is your House Number?: ";
std::cin >> houseNum;"\n";
std::cout << "What is your Street Name?: ";
std::getline(std::cin,houseNum);
return 0;
}
抛出的错误代码是"No matching function for call to 'getline'"。
好吧,我看到了几个主要问题。
你有这条线:
std::cin >> firstName;"\n";
第一部分正确,第二部分不正确。
你想要这个:
std::cin >> firstName;
std::cout << "\n" << std::endl;
此外,您对之前创建的字符串对象调用 getline
。 std::cin
是流类型的对象,不是字符串。我建议你看看 this page 中谈到 getline
。
getline()
的问题是您试图将字符串流分配给 int
变量 houseNum
,因此出现错误。
no matching function for call to 'getline(std::istream&, int&)'
另外,代码行:
std::cin >> firstName;"\n";
应该给你:
warning: statement has no effect [-Wunused-value]
对于"\n"
启用compiler warnings,它们可以为您节省很多时间。
您好,我在使用 C++ 中的 getline() 函数时遇到问题我是 C++ 的新手,对编程时期也比较陌生。我正在学习 C++ 课程的介绍,我自己在网上对这个主题所做的研究并没有让我找到解决方案我并不真正理解他们所说的所有事情,因为这是我 class。任何 quick/rough help/understanding 都将不胜感激,因为我们将在 class 中更全面地介绍这个主题。下面是我正在编写的代码。这是一个简单的程序,只要求用户提供姓名和地址,然后以正确的邮件格式显示该信息。
#include <iostream>
#include <string>
int main()
{
// Variables for Mailing Addresses
std::string firstName;
std::string lastName;
int houseNum;
std::string streetName;
std::string cityName;
std::string state;
int zipCode;
// Asking for input
std::cout << "What is your First name?: ";
std::cin >> firstName;"\n";
std::cout << "What is your Last name?: ";
std::cin >> lastName;"\n";
std::cout << "What is your House Number?: ";
std::cin >> houseNum;"\n";
std::cout << "What is your Street Name?: ";
std::getline(std::cin,houseNum);
return 0;
}
抛出的错误代码是"No matching function for call to 'getline'"。
好吧,我看到了几个主要问题。
你有这条线:
std::cin >> firstName;"\n";
第一部分正确,第二部分不正确。 你想要这个:
std::cin >> firstName;
std::cout << "\n" << std::endl;
此外,您对之前创建的字符串对象调用 getline
。 std::cin
是流类型的对象,不是字符串。我建议你看看 this page 中谈到 getline
。
getline()
的问题是您试图将字符串流分配给 int
变量 houseNum
,因此出现错误。
no matching function for call to 'getline(std::istream&, int&)'
另外,代码行:
std::cin >> firstName;"\n";
应该给你:
warning: statement has no effect [-Wunused-value]
对于"\n"
启用compiler warnings,它们可以为您节省很多时间。