gpt4 book ai didi

c++ - 如果在某些输入之后使用 getline() 将不起作用

转载 作者:IT老高 更新时间:2023-10-28 22:00:12 25 4
gpt4 key购买 nike

Possible Duplicate:
Need help with getline()

getline() 不起作用,如果我在一些输入后使用它,即

#include<iostream>
using namespace std;

main()
{
string date,time;
char journal[23];


cout<<"Date:\t";
cin>>date;
cout<<"Time:\t";
cin>>time;

cout<<"Journal Entry:\t";
cin.getline(journal,23);


cout<<endl;
system("pause");
}

就好像我在输入之上使用 getline() 一样,它确实有效,即

cout<<"Journal Entry:\t";
cin.getline(journal,23);
cout<<"Date:\t";
cin>>date;
cout<<"Time:\t";
cin>>time;

可能是什么原因?

最佳答案

Characters are extracted until either (n - 1) characters have beenextracted or the delimiting character is found (which is delimiter if thisparameter is specified, or '\n' otherwise). The extraction also stopsif the end of the file is reached in the input sequence or if an erroroccurs during the input operation.

cin.getline() 从输入中读取时,输入流中会留下一个换行符,因此它不会读取您的 c 字符串。在调用 getline() 之前使用 cin.ignore()

cout<<"Journal Entry:\t";
cin.ignore();
cin.getline(journal,23);

关于c++ - 如果在某些输入之后使用 getline() 将不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12691316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com