Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

菜鸟问题,请教ING

Posted by Selfind at 2006-11-25 09:35:07
一个完备的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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator