hi guys,

I have another noob question, I was wondering if anyone can help me with it.

the home work assignment I have is

"In this assignment we are going to write a phone directory program. The user is asked to input a name and the program looks up the address and number. The actual phone directory is a file that I’ve posted on the Web site, phoneDir. Download it.

Examine the file to see the format. Here is a part of the file:

gomez, thomas
954-182-3819
3829 Broward Blvd.

hanrity, john
954-182-3812
982 Hillcrest Rd.

michaels, marilyn
954-329-3451
16 Pine View Rd


Your program will request a name, last name first.

The program terminates when the user inputs a blank line.

Please don’t use absolute paths for your phoneDir file, as it is more difficult for your instructor to test.

so far my code is

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

using namespace std;


int main(){
	int count = 0;
	
	string n,name,phnum,address;
	
ifstream out;
out.open("phoneDir.txt");
cout<<"Name please: <last name, first name> ";
	getline(cin, n);
	cout<<endl;
getline(out, name);
do {
	
	
	if(n== name){
	
	
      cout << name << endl;
      getline(out,phnum);
      getline(out,address);
	  cout << phnum <<endl<<address<<endl;
	  out.close();
	}
	
	
}while(out>>n);









system("pause");


}

when entering the first name in the list, it works great, but it does not work on any other name.

anyone have any suggestions on how I can make this work please?

Thank you

Rusty

Hint: what if your name input is not matched by the first line of the file? If they don't match keep going, reading in lines until it does. You need another loop.

Using "out" as the name of your input file might be confusing to someone (or you!)trying to read your program later.

Also, what happens if the file is not found or went bad?

I tried to put in

if (n!=name)
		getline(out,name);
	else
		cout<<"name not found"<<endl;

and it skips first name, prints out the phone number and address of first name than prints out everyone else last name, than first name on next line and their phone number.
I even put in just these lines

if (n!=name)
		
		cout<<"name not found"<<endl;

and it reads off a bunch of names, it does not read each line, it displays each sting on each line.

I even made the program like this

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

using namespace std;


int main(){
	int count = 0;
	const int SIZE = 30;
	char n[SIZE], name[SIZE];
	string ln, lname;
	
ifstream out;
out.open("phoneDir.txt");
cout<<"Name please: <last name, first name> ";
	cin.getline(n, SIZE);
	cout<<endl;
out.getline(name, SIZE);

do {
	
	if(!out){
		cout<<"error opening file. "<<endl;
	
	if(strcmp(n,name)==0){
      cout << name << endl;
      
	  
	if(!strcmp(n,name)){
		
		cout<<"name not found"<<endl;
	
	count++;
	}
	}
	}
	
}while(out>>name);

 out.close();

then it does not find any name and does not say that it did not find it.

using just this finds the names
but still having problems with getting what comes after those names.

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

using namespace std;


int main(){
	int count = 0;
	string ln, lname;
	string n,name,phnum,address;
	bool nameline(string m,string f);
ifstream out;
out.open("phoneDir.txt");
if(!out)
		cout<<"error opening file. "<<endl;
cout<<"Name please: <last name, first name> ";
	getline(cin,n);
	cout<<endl;


while(getline(out, name)){
	
	
	
	
if (name.find(n) != string::npos)
      cout << name << endl;
   
	
	
	count++;

	
}

 out.close();






system("pause");


}

can anyone help me please? when I add any more if statements after the first one the output gets all messed up.

Can you post your code with those if statements in there?

After you find the names, since you know the next two lines are the phone number and address, you could keep reading the file until your count is incremented by 2, then get out of the loop on that condition. Reset the count and file position and ask the user again.

ya, I figured it out on my way into class last night, and a 2 other students had done it that way as well. out of 15 ppl in the class only 2 had figured out how to get it to work and I only figured it out on the way to class which was to late to do anything about it.

have to open and close the file in the loop to get it to work. it drove me nuts for 2 days.

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.