| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
菜鸟问题,请教ING一个完备的directory问题,电话本中根据名字找号码,可以多次查找,
希望有人帮忙改下错,感激不禁,
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void findNumber(string inputName);
int main()
{
int count=0;
string inputName;
char again;
while(count==0||again=='Y')
{
count++;
cout<<"Please enter a first and last name,for example,he tao."<<endl;
getline(cin,inputName);//1.不能输入第2次,不知道为什么?
cout<<"The name you entered is "<<inputName<<endl;
findNumber(inputName);
cout<<endl<<"Do you want to look up another word ?"
<<endl<<"Y for Yes,and any other words for exiting"<<endl;
cin>>again;
}
return 0;
}
void findNumber(string inputName)
{
ifstream telNum;
string aLine;
string::size_type pos;
string num;
telNum.open("directory.txt");
getline(telNum,aLine);
pos=aLine.find(inputName);
while(pos>=255&&aLine.length()!=0)
{
telNum.ignore(255,'/n');//2.不能跳到下一行
getline(telNum,aLine);
}
num="The phone number is "+aLine.substr(pos+inputName.length()+1,aLine.length()-(pos+inputName.length()+1));//输出输入名字后的一行内所有字符
//3.不能保证输入的名字包含于directory中的名字时,比如:输入R而directory中是R H
cout<<num;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator