Reading list from input file,place in array, print the index of the name

Need some direction on this one if anyone can help me out.

Write a C++ program that reads the input file information as a name list. Compare the entered name with the names in the list.

Step 1. Open an input and an output file.

Step 2. Read contents (26 names) from the input file. (input12.txt)

Step 3. Receive a name from a user and compare it to the names in the list. Show the index number if the name is found in the list. If not found, state that. (See the output example below.)

Step 4. Repeat step 3 until the user decides to quit by typing “quit”.

Step 5. Record the entire session to the output file.

Step 6. Close both input and output files.

using this as the starting point:

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
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;

int main()
{
	string ifilename, ofilename, line;
	ifstream inFile, checkOutFile;
	ofstream outFile;
	char response;

	// Input File
	cout << "Please enter the name of the file you wish to open :";
	cin >> ifilename;


	inFile.open(ifilename.c_str());

	if (inFile.fail())
	{
		cout << "The file" << ifilename << "was not successfully opened." << endl;
		cout << "Please check the path and name of the file." << endl;
		exit(1);
	}
	else
	{
		cout << "the file is successfully opened." << endl;
	}

	// Output file
	cout << "Please enter the name of the file you wish to write :";
	cin >> ofilename;

	checkOutFile.open(ofilename.c_str());

	if (!checkOutFile.fail())
	{
		cout << "A file" << ofilename << "exists.\n Do you want to continue to overwrite it? (y/n) :";
		cin >> response;
		if (tolower(response) == 'n')
		{
			cout << "The existing file will not be overwritten." << endl;
			exit(1);
		}
	}

	outFile.open(ofilename.c_str());

	if (outFile.fail())
	{
		cout << "The file" << ofilename << "was not successfully opened." << endl;
		cout << "Please check the path and name of the file." << endl;
		exit(1);
	}
	else
	{
		cout << "The file is successfully opened." << endl;
	}

	// Copy file contents from inFile to outFile

	while (getline(inFile, line))
	{
		cout << line << endl;
		outFile << line << endl;
	}


	// Close Files
	inFile.close();
	outFile.close();
}//main 


Last edited on
Before you can start with step 3 you need to store the names somewhere. Are you allowed to use a vector or an array?
Yes I can use an array.
OK then you are ready to go.

Just declare an array like this:

1
2
3
4

#define NUM_NAMES  26

string Names[NUM_NAMES];


and store the names in it when you read the file. To make sure that you read the names correctly display them on the screen.
Topic archived. No new replies allowed.