关于C++中string输入cin,getline()问题,《C++ primer》学习总结

Chapter 3. Library Types

根据书上的例子,自己写了一个测试程序。

在头文件中定义了各个方法,以下是getstring.cpp()

#include "StringExample.h"
#include <string>
#include <iostream>
using namespace std;
void readString()
{
	cout<<"This is the string reading example."<<endl;
	string s;
	cin>>s;
	cout<<s<<endl;
}
void readUnkNum()
{
	cout<<"This is the reading unknown number of string example."<<endl;
	string word;
	while(cin>>word)
		cout<<word<<endl;
}
void getlineExamp()
{
	cout<<"This is the getline example."<<endl;
	string line;
	getline(cin,line);
	cout<<line<<endl;
}

之后是一个测试文件test.cpp

#include "StringExample.h"
#include <iostream>
#include <string>
int main()
{
	readString();
	readUnkNum();   
        getlineExamp();
	return 0;
}

由于C++中cin的执行机制是一次读完缓冲区中的所有字符串,然后再将字符串逐个赋给变量并且去掉任意的空格。所以当执行第一个函数的时候会有如下结果:

其中第二行是输入结果,第三行是输出结果。

接着程序运行第二个函数,此函数的功能为读取输入的字符串,然后逐行输出。但此时会遇到问题。结果如下:

可以看到,在输入接下来的字符串之前(第6行),有一行多出来的字符串(world),这个字符串为缓冲区中,上一次输入时,未被读取的。要解决这个问题,可以将上一次输入后的istream流清空。接下来,为了实现清空输入流,我使用了cin.clear()函数,但是发现不能达到想要达到的效果。

其实改函数的功能不是清空输入流,而是字符串输入检错功能。而真正的清空输入流则是cin.sync(),在函数最后加上该方法即可清空输入流中的滞留数据。

接下去是一个执行getline()的函数。执行此函数是又遇到了问题。

可以看到,我们想要此函数执行输入一行字符串之后输出的功能,但是在输入了两行字符串之后才输出第一行字符串。这是VC++6.0中的一个bug,换其他的编译器应该可以运行出正确的结果。微软在它的support里有提到(http://support.microsoft.com/default.aspx?scid=kb;EN-US;q240015

但是我试图找到“string”头文件,却找不到resolution中提到的语句。所以目前这个问题还没有解决,如果有哪个高手看到了这篇文章,知道解决办法,请务必告诉我,不胜感激。

下面附我解决问题过程中参考过的一篇文章:http://hi.baidu.com/jrckkyy/blog/item/0a4a3026b10a8f128b82a184.html

                                                                                                                                                                                                         2012/09/26


                
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include"iostream" #include"string" #include<iomanip> #include<fstream> using namespace std; typedef int Status; typedef int ElemType; #define OK 1 #define ERROR 0 typedef struct LNode//定义 { ElemType data;//结点的数据域 struct LNode *next;//结点的指针域 }LNode,*LinkList; //LinkList为结构体LNode的指针型 Status InitList(LinkList &L)//初始 { L=new LNode; L->next=NULL; return OK; } void GreateList_L(LinkList &L,int n)//后插法 输入元素 { /*L=new LNode;//生成新的结点作为头结点,用头指针L指向头结点 L->next=NULL;//头结点的指针域为空*/ LNode *r; LNode *p; r=L; //!!!!错误,定义指针 for(int i=0;i<n;++i) { p=new LNode; cin>>p->data; p->next=NULL; r->next=p; r=p; } } /*void CreateList_L(LinkList &L,int n)//前插法 { cout<<"输入录取数据个数 "; cin>>n; cout<<"输入数据"; L=new LNode;//生成新的结点作为头结点,用头指针L指向头结点 L->next=NULL;//头结点的指针域为空 for(int i=0;i<n;++i) { p=new LNode; cin>>p->data; p->next=L->next; L->next=p; } }*/ Status GetElem(LinkList L,int i,int &e)//取值 { LNode *p; p=L->next;int j=1; while(p&&j<i) { p=p->next; ++j; } if(!p||j>i) return ERROR; e=p->data; return OK; } LNode *LocateElem(LinkList L,ElemType e)//查找 { int j=1; LNode *p; p=L->next; while(p&&p->data!=e) { p=p->next; } return p; } Status LisInsert(LinkList &L,int i,ElemType e)//插入 { LNode *p; p=L->next;int j=0; while(p&&(j<i-1)) { p=p->next; j++; } if(!p&&(j>i-1)) return ERROR; LNode *s; s=new LNode; s->data=e; s->next=p->next; p->next=s; return OK; } Status ListDelete(LinkList &L,int i)//删除 { LNode *p; p=L->next;int j=0; while(p&&(j<i-1)) { p=p->next; j++; } if(!(p->next)&&(j>i-1)) return ERROR; LNode *q; q=p->next; p->next=q->next; delete q; return OK; } int main() { LNode *L; int i,temp,n,choose=-1; int e; cout << "1. 创建信息表\n"; cout << "2. 查找元素\n"; cout << "3. 插入元素\n"; cout << "4. 删除元素\n"; cout << "0. 退出程序\n\n"; while(choose!=0) { cout << "请选择:"; cin >> choose; switch (choose) { case 1: cout<<"请输入元素个数:"; cin>>n; cout<<"\n"<<"请输入元素"; GreateList_L(L,n); cout<<"\n"; break; case 2:cout<<"请输入查找的元素:"; cin>>e; LNode *s; s=LocateElem(L,e); cout<<"这个元素的地址是"<<s<<"\n\n"; break; case 3:cout<<"请输入需要插入的数:"; cin>>e; cout<<"\n"<<"请输入需要插入的位置"; cin>>i; if(LisInsert(L,i,e)) cout<<"插入成功\n\n"; else cout<<"插入失败\n\n"; break; case 4: cout<<"请输入删除的位置:"; cin>>i; if(ListDelete(L,i)) cout<<"删除成功\n\n"; else cout<<"删除失败\n\n"; break; } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值