首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

getline的一些有关问题,来帮帮忙撒

2012-03-30 
getline的一些问题,来帮帮忙撒我想自己创建一个可以建立文档并且向里面写入语句的程序,但是似乎在getline

getline的一些问题,来帮帮忙撒
我想自己创建一个可以建立文档并且向里面写入语句的程序,但是似乎在getline那里有点问题,比如说输入了行数是3行,在输完第3行以后问题就出来了,但是感觉应该没有问题啊,文档里面也是除了最后一行没有以外都有。


#include <iostream>
#include <string>
#include <fstream>

using   namespace   std;

#define   MaxStrSize   256

ofstream     fp;
ifstream     ifp;
bool   check_create   =   false;
int   cline;

void   CreatTextFile()
{
if(check_create   ==   true)
{
cout < < "指定文件已经打开,该操作失败! " < <endl;
return;
}
else
{
char   fname[10];
string   str;
cout < < "输入要建立的文件名以及保存路径: " < <endl;
cin> > fname;
cout < < "您要输入多少行内容? " < <endl;
cin> > cline;
fp.open(fname,   ios::ate);
cout < < "请输入内容:(共 " < <cline < < "行): " < <endl;
for(int   i   =   0;   i   <   cline;   i++)
{
getline(cin,   str);
fp   < <   str;
fp   < <   endl;
}
ifp.open(fname);
cout < < "该文件已经建立并被打开! " < <endl;
check_create   =   true;
}
}

void   main()
{
int   xz;

do{
cout < <endl;
cout < < "************************* " < <endl;
cout < < "*文本文件的检索   子串的统计及定位* " < <endl;
cout < < "************************* " < <endl;
cout < < "*   1   建立新文件并打开   * " < <endl;
cout < < "*   2   打开已有的文件       * " < <endl;
cout < < "*   3   单词子串的计数       * " < <endl;
cout < < "*   4   单词子串的定位       * " < <endl;
cout < < "*   5   退出整个系统           * " < <endl;
cin> > xz;
switch(xz)
{
case   1:CreatTextFile();break;
case   2:OpenTextFile();break;
case   3:SubStrCount();break;
case   4:SubStrInd();break;
case   5:return;
default:cout < < "选择错误!重新选择 " < <endl;
}
}while(1);
fp.close();
ifp.close();
}


[解决办法]
cin> > cline; //问题在这里, 输入一个整数后有一个回车字符滞留在输入缓冲中
fp.open(fname, ios::ate);
cout < < "请输入内容:(共 " < <cline < < "行): " < <endl;
for(int i = 0; i < cline; i++)
{
getline(cin, str); //第一次getline被 滞留的回车填充

结果就是只输入两个string就OVER了

热点排行