关于getline函数以及输入终止的有关问题
关于getline函数以及输入终止的问题
就是以下的程序,包含两部分,第一部分是while(cin>>word)那段,用户可以输入任意多的字符串,直到不想输入为止(此时按Ctrl+Z);第二部分是while(getline(cin, line))那部分,用户可以输入任意多行字符,每次换行程序就会把上一行的字符输出一遍,直到不想再输入为止(此时按Ctrl+Z)。两部分单独编译的时候都没问题,但是合在一起的时候,第二部分就被无视掉了(但是前面的提示语还留着)。请问这个是怎么回事呢?我百思不得其解啊!求各位大神帮帮忙!
------解决方案--------------------
在第一个while (cin >> word)的循环时,你键入CTRL+Z来结束了该循环,这时cin(输入流)的状态被标志为遇到文件结尾,如果不调用in.clear()或其他可以清除流的状态的函数来将清除cin的状态,则cin被标志为遇到文件结尾的状态一起保持。到第二个 while (getline(cin, line))循环时再次引用cin则此时的cin的状态为遇到文件结束标志,while循环直接退出
------解决方案--------------------
string word;
// read until end-of-file, writing each word to a new line
cout<<"\nNow you can input as many words as you can. If you want to terminate, you may press Ctrl+z:\n";
while (cin >> word)
cout << word << endl;
cin.clear();//调用clear()来清除cin的状态
string line;
// read line at time until end-of-file
cout<<"\nNow you can input as many lines of words as you wish. To terminate, please press Ctrl+z:\n";
while (getline(cin, line))
cout << line << endl;
cin.clear();
keep_window_open();
------解决方案--------------------
“I want to chang the world.^Z”这里的“CTRL+Z”只是2个普通的字符而已,当前任务不能将其解析成中断指令,只当2个普通字符来处理。而直接按下键盘的上的CTRL+Z是向当前任务发送了一条终止的指令。
------解决方案--------------------
Windows对于Ctrl+Z是,只有按下回车之后才有可能检测在此之前是否有Ctrl+Z按下。还有一个特点就是:如果输入缓冲区中有可读的数据则不会检测Ctrl+Z(有要读的数据,还不能认为到了流的末尾)。Ctrl+Z产生的不是一个普通的ASCII码值,也就是说它产生的不是一个字符,所以不会跟其它从键盘上输入的字符一样能够存放在输入缓冲区。(两种看法,第二种更具说服力)
------解决方案--------------------
http://bbs.****.net/topics/380156456 可以去这个帖子看看
就是以下的程序,包含两部分,第一部分是while(cin>>word)那段,用户可以输入任意多的字符串,直到不想输入为止(此时按Ctrl+Z);第二部分是while(getline(cin, line))那部分,用户可以输入任意多行字符,每次换行程序就会把上一行的字符输出一遍,直到不想再输入为止(此时按Ctrl+Z)。两部分单独编译的时候都没问题,但是合在一起的时候,第二部分就被无视掉了(但是前面的提示语还留着)。请问这个是怎么回事呢?我百思不得其解啊!求各位大神帮帮忙!
//
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
void keep_window_open();
int main()
{
string word;
// read until end-of-file, writing each word to a new line
cout<<"\nNow you can input as many words as you can. If you want to terminate, you may press Ctrl+z:\n";
while (cin >> word)
cout << word << endl;
string line;
// read line at time until end-of-file
cout<<"\nNow you can input as many lines of words as you wish. To terminate, please press Ctrl+z:\n";
while (getline(cin, line))
cout << line << endl;
keep_window_open();
}
void keep_window_open()
{
cout << "\nPress any key to exit:";
getchar();
}
getline
终止
字符串
------解决方案--------------------
在第一个while (cin >> word)的循环时,你键入CTRL+Z来结束了该循环,这时cin(输入流)的状态被标志为遇到文件结尾,如果不调用in.clear()或其他可以清除流的状态的函数来将清除cin的状态,则cin被标志为遇到文件结尾的状态一起保持。到第二个 while (getline(cin, line))循环时再次引用cin则此时的cin的状态为遇到文件结束标志,while循环直接退出
------解决方案--------------------
string word;
// read until end-of-file, writing each word to a new line
cout<<"\nNow you can input as many words as you can. If you want to terminate, you may press Ctrl+z:\n";
while (cin >> word)
cout << word << endl;
cin.clear();//调用clear()来清除cin的状态
string line;
// read line at time until end-of-file
cout<<"\nNow you can input as many lines of words as you wish. To terminate, please press Ctrl+z:\n";
while (getline(cin, line))
cout << line << endl;
cin.clear();
keep_window_open();
------解决方案--------------------
“I want to chang the world.^Z”这里的“CTRL+Z”只是2个普通的字符而已,当前任务不能将其解析成中断指令,只当2个普通字符来处理。而直接按下键盘的上的CTRL+Z是向当前任务发送了一条终止的指令。
------解决方案--------------------
Windows对于Ctrl+Z是,只有按下回车之后才有可能检测在此之前是否有Ctrl+Z按下。还有一个特点就是:如果输入缓冲区中有可读的数据则不会检测Ctrl+Z(有要读的数据,还不能认为到了流的末尾)。Ctrl+Z产生的不是一个普通的ASCII码值,也就是说它产生的不是一个字符,所以不会跟其它从键盘上输入的字符一样能够存放在输入缓冲区。(两种看法,第二种更具说服力)
------解决方案--------------------
http://bbs.****.net/topics/380156456 可以去这个帖子看看