Was doing this project for school, but i cant get it to run, it's a telephone directory project here's the code

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;


char getChoice()
{
	char choice;
	cout<<endl<<"Do you want to look for another? ";
	cin>>choice;
	return choice;
}


void getNames(string &strFName, string &strLName)
{
	
	cout<<endl<<"Enter first name: ";
	cin>>strFName;
	cout<<"Enter last name: ";
	cin>>strLName;
}

int findSubString(string line, string s)
{
	return line.find(" ");
}
int main()
{
	string line;
	string fName,lName,tNumber, strFName, strLName;
	char choice;
	int flag=0;
	int nextLoc=0;
	
	cout<<"A Telephone Directory "<<endl;
	
	do{
			flag=0;
			
			ifstream myfile ("input.txt");
			
			if (myfile.is_open())
			{
				getNames(strFName,strLName);
				
				while (! myfile.eof() )
				{
					
					getline (myfile,line);
					
					nextLoc=findSubString(line," ");
					
					fName=line.substr(0,nextLoc);
					
					line = line.substr(nextLoc+1,line.length());
					
					nextLoc=findSubString(line," ");
					
					lName=line.substr(0,nextLoc);
					
					line = line.substr(nextLoc+1,line.length());
					tNumber = line;
					if(fName==strFName && lName==strLName)
					{
						cout<<"Found!!"<<endl;
						cout<<endl<<"First Name: "<<fName<<"\n"<<"Last Name: "<<lName<<"\n"<<"Telephone Number"<<tNumber<<endl ;
						flag=1;
						break;
					}
				}
			}
			else
			{
				cout << "Unable to open file"<<endl; 
				exit(0);
			}
			if(flag==0)
			{
				cout<<"Not Found...Sorry."<<endl; 
			}
			
			choice = getChoice();
	}while(choice=='y');
	cout<<"Good Bye..."<<endl;
	cin.get(),cin.get();
	return 0;
}

here is the input file


Mark John 8876543455
John Mark 8888888885
Bob Jackob 8899999999
Billy Bob 3456789087
Jackob Billy 8876543333

Thanx

Recommended Answers

All 5 Replies

Once I added <cstdlib> to the #includes so you could use exit, seemed fine.


A Telephone Directory

Enter first name: John
Enter last name: Mark
Found!!

First Name: John
Last Name: Mark
Telephone Number8888888885

Do you want to look for another? n
Good Bye...

What if i wanted to enter the number and find the name... using the same code what would i need to modify?

Well if you want to make your program bidirectional, contrary to your present unidirectional program, you need to assign another user defined funtion, with the whole logic in reverse order, then call that function with a conditional operator......

Ok how do i start going about that... im a not so coding savvy? thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.