Files trouble

I have some code that opens a file using a fuction to request the file name i then request and id that the user wants to look for in the file. if found should display it along with the name associated with it if not it adds it. then loops to ask the user if they want to look for another file. I keep getting a problem with outputting the name and anything you guys can do to help would be great.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196

#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 >> id_num >> name;
		if ( ID == id_num)
		{
			outFile << 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;
		}
	}
}
What exactly is happening? Is the outputted name wrong? Is the file not being opening?

Btw, you don't need stdio.h, instead of getchar() just use cin.get().
the probelm i have is tryin ti display the lines from the file after i search for them and also a looping issue. as you can see i have it ask to enter a new id but when you say yes it says not found when it should go back to the getid function.


Please enter the fully qualified name of the
input text file (i.e. including the path): roster.txt

Enter the ID number: 666
Id not found would you like to add it? Y/N y
What name would you like to input?jim
Look for another ID? Y/N y
Id not found would you like to add it? Y/N y
What name would you like to input?n
Look for another ID? Y/N n
Press Enter to continue
So you open the file for reading (infile) and also open the same file for writing (outfile).
When you want to add a new ID, you write it to the outfile and expect it to automatically appear in the infile.

Mmmmm... I can't see that happening.
I don't think the stream will automatically update. You'd have to close and reopen the input file repeatedly which is really not very efficient.
yes i open the file to read it then the user inputs a number to look for in the file. if they find the number its supposed to display the name associated with the number if they cant find it it then asks if they want to write it to the file so thats where the outfile comes in. then it asks if they want to look for a different number which should then start all over regardless of the new number added.. not sure if im making good enough sense of it still alittle new to c++
Topic archived. No new replies allowed.