if statement not working

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.

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
/*    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
using namespace 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;
			}
			else if (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		 



























Last edited on
Line 100: Your loop condition is i<26.
Line 110: How is i ever going to be 26?

Line 32: quit is an empty string.
Line 108: You're comparing name to an empty string. Did you mean "quit"?

Last edited on
Topic archived. No new replies allowed.