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;
}
}
}