Can anyone tell me why the second if statement in the do while loop is not working? The first one works but when i check the name vs. the name in the list against a name that is not in the list, it does not work.
/* ELEN 1301-01 Programming Assignment #12
Name : Name
Student ID : ID
Due date : November 23, 2015
Objective of this assignment :
Read 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. Showthe 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.
*/
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#define NUM_NAMES 26
usingnamespace std;
int main()
{
string ifilename, ofilename, line, name,quit;
ifstream inFile, checkOutFile;
ofstream outFile;
char response;
int i;
string Names[NUM_NAMES];
// 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
cout << "Welcome to my name list! Please enter quit to exit the program." << endl;
outFile << "Welcome to my list! Please enter quit to exit the program." << endl;
for (i = 0; i < 26; i++)
inFile >> Names[i];
do
{
cout << "Please enter a name you would like to check with the list: ";
cin >> name;
for (i = 0; i < 26; i++)
{
if (Names[i] == name)
{
cout << "The name " << name << " is found in the list at index " << i << endl;
outFile << "The name " << name << " is found in the list at index " << i << endl;
}
elseif (name == quit)
break;
if((i == 26) && (name != "quit"))
{
cout << name << " is not found in the list." << endl;
outFile << name << "is not found in the list." << endl;
}
}
}while (name != "quit");
// Close Files
inFile.close();
outFile.close();
}//main