Ok what im tryin to do is open a file by the name a user request after that i ask the user and id number to look for in the file if its found i want to display it along with the name after it but i cant figure out how to get it to display the name and id num if its not found then gives the user the option to add it to the file and then look again for another id num..... please help

/*************************************************************************************
** File name	: lab6_driver.cpp
**
** This program will open and read a file then ask user for an ID to look up and then display if its found or not.
**
**
**
** Programmer	: Christopher Erb
**
** Date Created	: 02/26/10
**
** Date last revised:
************************************************************************************/

#include <iostream>				    // Need for cout,cin
#include<iomanip>                   // Need setf,fixed,showpoint,setprecision
#include <stdio.h>				    // Need for getchar
#include <fstream>					// Needed for files
#include <cstdlib>					// Needed for exit function
#include <string>					// Need for string class

using namespace std;


string getInputFileName(); // a function to prompt for the complete file name
int getId(); // Function that gets the id number 
void search(ofstream& outFile, ifstream& inFile, int);

int main ()
{


	ofstream outFile;
	ifstream inFile;
	string fileName; // complete file name including the path

	fileName = getInputFileName(); // prompt and obtain the full file name

	// try to open the file
	outFile.open(fileName.c_str(),ios::in | ios::out | ios::app);
	inFile.open(fileName.c_str(),ios::in);

	if (!inFile.is_open())
	{
		cerr << "File open error " ;
		cout << " Press enter to continue" << endl;
		cin.ignore();
		char ch = getchar();
		exit (1);
	}

	if(!outFile.is_open())
	{
		cerr << "File open Error Creating file" ;
		cout << " Press enter to continue" << endl;
		cin.ignore();
		char ch = getchar();
		exit(1);
	}



	/*
	char c;
	inFile.get (c);
	while (!inFile.eof ())
	{
	cout << c;
	inFile.get (c);
	}
	string str;
	int id_num;
	while (inFile >> str >> id_num)
	{ cout << str <<  "ID" << id_num << endl;
	}
	*/
	int ID;
	ID = getId();
	search(outFile, inFile, ID);
	outFile.close();
	inFile.close();
	cout.setf (ios::showpoint );
	cout.setf( ios::fixed);
	cout << setprecision(2);

	cout << " Press Enter to continue" << endl;
	cin.ignore();
	char ch = getchar();

	return 0;

}

//************************************************************
//
// Function name: getInputFileName
//
// Purpose: to prompt for the fully qualified name of a file
//              i.e. including the path of the file
//
// Input parameters: none
//
// Output parameters: none
//
// Return Value: a string containing the fully qualified name
//               of a file
//
//************************************************************

string getInputFileName()
{
	string f_Name; // fully qualified name of the file

	cout << "Please enter the fully qualified name of the " << endl
		<< "input text file (i.e. including the path): ";
	cin >> f_Name ;
	cout << endl; // skip a line

	return f_Name;
}
//************************************************************
//
// Function name: getId
//
// Purpose: to prompt the user for the ID 
//             
//
// Input parameters: char id
//
// Output parameters: none
//
// Return Value: inputId
//               
//
//************************************************************

int getId ()
{
	int inputId;
	cout << " Enter the ID number: " ;
	cin >> inputId ;



	return inputId;
}
//************************************************************
//
// Function name: Search
//
// Purpose: Locate the ID the user entered. If found displays it with the name if not asks to enter into to the file
//             
//
// Input parameters: string id, filehandle passed by reference (ifstream &inFile, ofstream &outFile)
//
// Output parameters: none
//
// Return Value: 
//               
//
//************************************************************

void search(ofstream &outFile, ifstream &inFile, int ID)
{
	int id_num;
	string name;
	char userreply;
	char lookagain = 'Y';
	string inputname;
	

	while (!inFile.eof() && (lookagain == 'Y' || lookagain == 'y'))
	{
		inFile.seekg (0, ios::beg);
		inFile.getline(file);
		if ( ID == id_num)
		{
			std::cout << id_num << name;
		}
		else
		{
			cout << "Id not found would you like to add it? Y/N " ;
			cin >> userreply;
			if( userreply == 'Y' || userreply == 'y')
			{
				cout << " What name would you like to input?" ;
				cin >> inputname;
				outFile << "\n" << ID << " " << inputname;
				outFile.flush();

			}
		}

		cout << "Look for another ID? Y/N " ;
		cin >> lookagain;

		if (lookagain == 'Y' || lookagain == 'y')
		{
			
			inFile.clear();
			int getId ();

		}
		else
		{
			break;
		}
	}
}

Would you like to help pinpoint where the problem is a little closer than somewhere in 200 lines of code?

And have you ever heard of periods? They are those little dots that end sentences. They are used to help others understand the written word so they don't have to decipher run-on sentences.

> And have you ever heard of periods? They are those little dots that end sentences.
Hehe, perhaps the OP sorted the text in some way.

> another id num..... please help
There's a whole bunch at the end ;)

sorry pretty much its the void search function. I open the file but then when i go to find the id it cant find it or it dosnt display it after i find it

void search(ofstream &outFile, ifstream &inFile, int ID)
{
	int id_num;
	string name;
	char userreply;
	char lookagain = 'Y';
	string inputname;
	

	while (!inFile.eof() && (lookagain == 'Y' || lookagain == 'y'))
	{
		inFile.seekg (0, ios::beg);
		inFile.getline(file);
		if ( ID == id_num)

What is the value of id_num?

And see this about .eof()

updated code... id_num should be the id i read from the text file. The txt file is set up in the following format so i wanted to find the id the user inputs. ie 666 and if found display the id with the name that follows.


.txt
666 jim 456 bob 999 rick

void search(ofstream &outFile, ifstream &inFile, int ID)
{
	int id_num;
	string name;
	char userreply;
	char lookagain = 'Y';
	string inputname;


	while (!inFile.eof() && (lookagain == 'Y' || lookagain == 'y'))
	{
		inFile.seekg (0, ios::beg);
		inFile >> id_num >> name;
		 
		if ( ID == id_num)
		{
			 cout << id_num << name ;
		}
		else
		{
			cout << "Id not found would you like to add it? Y/N " ;
			cin >> userreply;
			cin.ignore();
			if( userreply == 'Y' || userreply == 'y')
			{
				cout << " What name would you like to input?" ;
				cin >> inputname;
				outFile << "\n" << ID << " " << inputname;
				outFile.flush();

			}
		}

		cout << "Look for another ID? Y/N " ;
		cin >> lookagain;

		if (lookagain == 'Y' || lookagain == 'y')
		{
			
			inFile.clear();
		ID = getId ();

		}
		else
		{
			break;
		}
	}
}

And what did the user enter. Exactly.

Look at each of the characters in the user input and the file input. You may find they are not the same. Display each character before the IF statement.

i think i see what you mean its only reading the first id num and name and filling it in there. So i think i have to put it in a string then compare it to the input right? if it matches print the string. which would be i would need multiple strings for each idnum and name together?

That's not at all what I said. Reread. I said "display each character [of User and File input] before the IF statement?" That suggestion is simple, to the point, and does imply new variable(s).

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.