程序跳过std::getline
Program skips over std::getline
我必须设计一个程序,允许用户在一个双重链表中输入5个节点的信息,然后按字母顺序对双重链表进行排序。它跳过了允许用户输入地址,不打印除姓名以外的任何信息,也不按字母顺序排序列表。我做错了什么?
#include <iostream>
#include <string>
struct node
{
std::string firstName;
std::string lastName;
std::string address;
long phoneNum;
node *next;
node *prev;
};
bool isEmpty(node *head);
void insertAsFirstElement(node *&head, node *&last, std::string firstName, std::string lastName, std::string address, long phoneNum);
void insert(node *&head, node *&last, std::string firstName, std::string lastName, std::string address, long phoneNum);
void searchFor(node *&last, std::string lastName);
void showList(node *current);
bool isEmpty(node *head)
{
if(head == NULL)
return true;
else
return false;
}
void insertAsFirstElement(node *&head, node *&last, std::string firstName, std::string lastName, std::string address, long phoneNum)
{
node *temp = new node;
temp->firstName = firstName;
temp->lastName = lastName;
temp->address = address;
temp->phoneNum;
temp->next = NULL;
temp->prev = NULL;
head = temp;
last = temp;
}
void insert(node *&head, node *&last, std::string firstName, std::string lastName, std::string address, long phoneNum)
{
if(isEmpty(head))
insertAsFirstElement(head, last, firstName, lastName, address, phoneNum);
else
{
node *temp = new node;
temp->firstName = firstName;
temp->lastName = lastName;
temp->address = address;
temp->phoneNum;
int result = lastName.compare(last->lastName);
if (result < 0)
{
temp->next = head;
head->prev = temp;
temp->prev = NULL;
head = temp;
}
else if ( result > 0)
{
temp->next = NULL;
temp->prev = last;
last->next = temp;
last = temp;
}
}
}
void searchFor(node *&last, std::string findName)
{
node *temp = last;
while (temp != NULL)
{
if (temp->lastName == findName)
{
std::cout << temp->firstName << " " << temp->lastName << " " << temp->address << " " << temp->phoneNum << std::endl;;
}
temp = temp->prev;
}
}
void showList(node *current)
{
if(isEmpty(current))
std::cout << "The list is emptyn";
else
{
std::cout << "The list contains: n";
while(current != NULL)
{
std::cout << current->firstName << " " << current->lastName << " " << current->address << " " << current->phoneNum << std::endl;
current = current->next;
}
}
}
int main()
{
node *head = NULL;
node *last = NULL;
std::string firstName;
std::string lastName;
std::string address;
long phoneNum;
int count;
count = 5;
while (count > 0)
{
std::cout << "Enter the first name of person #" << count << ":n";
std::cin >> firstName;
std::cout << "Enter the last name of person #" << count << ":n";
std::cin >> lastName;
std::cout << "Enter the address of person #" << count << ":n";
std::getline(std::cin,address);
std::cout << "Enter the phone number of person #" << count << ":n";
std::cin >> phoneNum;
insert(head, last, firstName, lastName, address, phoneNum);
count = count - 1;
}
showList(head);
std::string findName;
std::cout << "What is the last name of the person you would like to find?n";
std::cin >> findName;
searchFor(last, findName);
return 0;
}
你的问题是你混合了cin >>
和getline
,这在c++中是有问题的,因为尾随换行符等等。
好的做法是始终使用getline
,然后使用stringstream将行拆分为令牌。例如,我修改了您的解决方案,仅使用getline和字符串流(注意:您需要在文件的顶部添加#include <sstream>
。
你可以在这里阅读更多关于混合cin >>
和getline
的问题以及其他解决方法。
int main()
{
node *head = NULL;
node *last = NULL;
std::string firstName;
std::string lastName;
std::string address;
std::string phoneNumStr;
long phoneNum;
int count;
count = 5;
while (count > 0)
{
std::cout << "Enter the first name of person #" << count << ":n";
std::getline(std::cin,firstName); // no use of cin >>
std::cout << "Enter the last name of person #" << count << ":n";
std::getline(std::cin,lastName); // no use of cin >>
std::cout << "Enter the address of person #" << count << ":n";
std::getline(std::cin,address);
std::cout << "Enter the phone number of person #" << count << ":n";
std::getline(std::cin,phoneNumStr);
std::stringstream s(phoneNumStr); // no use of cin. Using stringstream to break up line and extract it into phoneNum
s >> phoneNum;
insert(head, last, firstName, lastName, address, phoneNum);
count = count - 1;
}
showList(head);
std::string findName;
std::cout << "What is the last name of the person you would like to find?n";
std::cin >> findName;
searchFor(last, findName);
return 0;
}
相关文章:
- 在 std::getline 和 std::cin 期间卡在循环中
- 确切地说,如何解释 std::getline(stream, string) 函数在C++中填充的字符串
- std::getline没有在while循环中重新请求用户输入
- 如何将 std::getline 转换为模板类型?
- 如何在 std::getline 中自定义分隔符
- 使用std::getline()读取一行
- std::getline with std::fstream
- 在 std::getline() 上使用任何字符作为分隔符
- C++ - 重复 std::getline() 作为用户整数输入?
- 如何使用 std::in 和 getline() 检测后台进程中从 bash 注入的输入
- std::getline 读取最后一个字符串两次
- 我很好奇 std::getline() 的实现
- std::getline 未产生正确的输出 (C++)
- C STD :: getline / std :: Sort无法正常工作
- std::getline(std::cin, string) 可能因键盘输入失败的方式
- if(!getline(std::cin,str1))break;这个条件检查什么
- 为什么 getline() 在 C++ 中不起作用?(调用'getline(std::ofstream&, std::string&)'没有匹配函数
- 接收错误"no matching function for call to 'getline(std::ifstream&, int&, char)'"
- 需要有关此编译错误的帮助 getline 函数错误:没有匹配函数调用"getline(std::istream&,char&)"|
- What is Scala for: getline(), std::cin.eof(), std::cin.bad()