关于cin,getline一起出现无法正常输入的问题

int N;
    char c;
    int num=0;
    string s;
     cin>>N>>c;

     getline(cin,s,‘\n‘);
     for(int i=0; s[i]!=‘\0‘;i++)
     {
         num++;
     }
     cout<<num<<endl;

这时cin(输入流)的状态被标志为遇到文件结尾,如果不调用in.clear()或其他可以清除流的状态的函数来将清除cin的状态,则cin被标志为遇到文件结尾的状态一起保持。到第二个 getline(cin, line)再次引用cin,则此时的cin的状态为遇到文件结束标志,无法输入,因此num始终为0;

一.调用clear()来清除cin的状态

二.cin.flush();

三.cin.ignore();

总有一款适合你……尝试了好多次,终于可以了!

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();

原文地址:https://www.cnblogs.com/lyqf/p/8614359.html

时间: 2024-08-30 14:10:54

关于cin,getline一起出现无法正常输入的问题的相关文章

C++中cin、cin.get()、cin.getline()、getline()、gets()等函数的用法(转)

学C++的时候,这几个输入函数弄的有点迷糊:这里做个小结,为了自己复习,也希望对后来者能有所帮助,如果有差错的地方还请各位多多指教(本文所有程序均通过VC 6.0运行) 1.cin 2.cin.get() 3.cin.getline() 4.getline() 5.gets() 6.getchar() 附:cin.ignore();cin.get()//跳过一个字符,例如不想要的回车,空格等字符 1.cin>>          用法1:最基本,也是最常用的用法,输入一个数字: #includ

C++中cin、cin.get()、cin.getline()、getline()、gets()等

学C++的时候,这几个输入函数弄的有点迷糊:这里做个小结,为了自己复习,也希望对后来者能有所帮助,如果有差错的地方还请各位多多指教(本文所有程序均通过VC 6.0运行) 1.cin  2.cin.get()  3.cin.getline()  4.getline()  5.gets()  6.getchar() 附:cin.ignore();cin.get()//跳过一个字符,例如不想要的回车,空格等字符 1.cin>>          用法1:最基本,也是最常用的用法,输入一个数字: #i

C++中cin、cin.get()、cin.getline()、getline()、gets()等函数的用法

C++中cin.cin.get().cin.getline().getline().gets()等函数的用法 学C++的时候,这几个输入函数弄的有点迷糊:这里做个小结,为了自己复习,也希望对后来者能有所帮助,如果有差错的地方还请各位多多指教(本文所有程序均通过VC 6.0运行) 1.cin 2.cin.get() 3.cin.getline() 4.getline() 5.gets() 6.getchar() 附:cin.ignore();cin.get()//跳过一个字符,例如不想要的回车,空

cin,cin.get(),cin.getline(),getline()

学C++的时候,这几个输入函数弄的有点迷糊:这里做个小结,为了自己复习,也希望对后来者能有所帮助,如果有差错的地方还请各位多多指教(本文所有程序均通过VC 6.0运行) 1.cin 2.cin.get() 3.cin.getline() 4.getline() 5.gets() 6.getchar() 附:cin.ignore();cin.get()//跳过一个字符,例如不想要的回车,空格等字符 1.cin>>          用法1:最基本,也是最常用的用法,输入一个数字: #includ

c++中 cin、cin.get()、cin.getline()、cin.getchar()的区别

①cin>>:无法接收空格.Tap键且以空格.Tap键.回车符为分隔符: ②cin.get( ):可以接收空格.Tap键且以回车符为结束符: 一:可输入单个字符 格式: char ch; ch=cin.get( );/cin.get(ch); 二:可输入字符串 格式: cin.get(字符数组名,元素个数) ③getline( ):可接收空格.Tap键且以回车符为结束符: 格式: string str;//字符串变量 getline(cin,str); ④cin.getline( ):可接收空

cin/cin.get()/cin.getline()/getline()/gets()/getchar()

1.cin>>    用法1:最基本,也是最常用的用法,输入一个数字: #include <iostream> using namespace std; main () { int a,b; cin>>a>>b; cout<<a+b<<endl; } 输入:2[回车]3[回车] 输出:5 注意:>> 是会过滤掉不可见字符(如 空格 回车,TAB 等) cin>>noskipws>>input[j];

cin、cin.get()、cin.getline()、getline()、gets()等函数的用法

学C++的时候,这几个输入函数弄的有点迷糊:这里做个小结,为了自己复习,也希望对后来者能有所帮助,如果有差错的地方还请各位多多指教(本文所有程序均通过VC 6.0运行)转载请保留作者信息:1.cin1.cin.get()2.cin.getline()3.getline()4.gets()5.getchar() 1.cin>>           用法1:最基本,也是最常用的用法,输入一个数字: #include <iostream> using namespace std; mai

C++:cin、cin.getline()、getline()的用法

主要内容: 1.cin用法 2.cin.getline()用法 3.getline()用法 3.注意的问题 一.cin>> 用法1:输入一个数字或字符 #include <iostream>using namespace std;main (){int a,b;cin>>a>>b;cout<<a+b<<endl;} 用法2:接收一个字符串,遇“空格”.“TAB”.“回车”就结束 #include <iostream>usi

[转载]cin、cin.get()、cin.getline()、getline()、gets()函数的用法

1.cin>>           用法1:最基本,也是最常用的用法,输入一个数字: #include <iostream>using namespace std;main (){int a,b;cin>>a>>b;cout<<a+b<<endl;} 输入:2[回车]3[回车]输出:5 用法2:接受一个字符串,遇“空格”.“TAB”.“回车”都结束 #include <iostream>using namespace st