C++ Array Help #2

Good evening everyone!

I just want to take a moment to thank everyone for being so helpful. I've only asked one question (this being my second) but I got fast and accurate help with my first issue! I really like the community here, thanks for making me feel so welcome!

Now to my question... I recently ran into problems with arrays. I have since been able to get my program working and can now open the file and input a name and it will be added to the file and then the array (files contents) will be outputs on the console for the user.
My problem is:
Can I add something to check the the names in the array for a duplicate name after the user inputs the name she/he wants to add, then tell the user whether the name is in the file or not?

I've tried to do this myself and failed miserably. I think my newness and my sloppy code are causing problems. Here is my current program (the one I described above:

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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <conio.h>

using namespace std;

int i;

int main()

{

	string line;

	string array[1000]; 

	ifstream infile("words.txt"); 
	

		std::ofstream outfile;

		outfile.open("words.txt", ofstream::app);

		cout << "Please enter a first name in lowercase letters: ";

		cin >> n;

		outfile << n;

		outfile.close();

		
		int count = 0;

		while ((infile >> line) && (count < 1000))
		{
			array[count++] = line;
		}

		

		cout << "Names in array:\n";

		for (int i = 0; i < count; i++)
		{
			cout << array[i] << "(" << i << ")" << endl;
		}

		cout << "Press return to continue\n";

		char junk;

		cin >> junk;

		return(0);
	}
Steps to do this:

- Read the name you want into a string
- Copy the contents of the file into an array (you already did this)
- Set a boolean value called found to false
- Use a while loop to iterate over the contents of the array with the condition being while number of names read is less than 1000 and found = false. If the name is found, set found to true
- After the loop is finished, you check if found is true, and if it is you know that the name the user entered is in the array, otherwise it isn't
Thanks very much Smac89!! I'll work on this!
Smac89, your suggestion seems as though it would work but my placement is off. Would I place the bool after the users input?
Declare the boolean anywhere but definitely before the while loop runs and make sure to set it to false initially
Thanks again Smac89!
Update Smac89:

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

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <conio.h>


using namespace std;

int i;
int count = 0;
char n;


int main()
{
	string line;

	string harray[1000]; //name of array

	ifstream infile("names.txt"); // storage


	char n; 


	cout << "Please enter a first name in lowercase letters: ";

	cin >> n;

		bool found = false;

		while (i < 1000 && !found)

		{

			if (found = true)

			{

				cout << "We found this name in the file" << endl;
			}

			


				//below stores words in the array

				int count = 0;

				

				while ((infile >> line) && (count < 1000))
				{
					harray[count++] = line;
				}


				cout << "Array contents:\n";

				for (int i = 0; i < count; i++)
				{
					cout << harray[i] << "(" << i << ")" << endl;
				}

				cout << "Press return to continue\n";

				char junk;

				cin >> junk;

				return(0);
			}


I am far too new... I apologize but this just isn't working.
Last edited on
Your squiggly brackets don't seem to match up.
I use the code::blocks ide and when the the cursor is on a squiggly bracket the nearest matching one is also highlighted.
The other error is in
if (found = error)
= is an assignment operator in c++
if(found == error)
is probably what you wanted.
Also you use too many spaces and they lose their ability to block code in a useful way.
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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <conio.h>


using namespace std;

int i;
int count = 0;
char n;


int main()
{
    string line;
    string harray[1000]; //name of array
    ifstream infile("names.txt"); // storage

    char n;

    cout << "Please enter a first name in lowercase letters: ";
    cin >> n;
    bool found = false;

    while (i < 1000 && !found)
    {
        if (found == true)
        {
            cout << "We found this name in the file" << endl;
        }

        //below stores words in the array
        int count = 0;
        while ((infile >> line) && (count < 1000))
        {
            harray[count++] = line;
            cout << harray[i] << "(" << i << ")" << endl;
        }

        cout << endl << "Array contents:\n";
        for (int i = 0; i < count; i++)
        {
            cout << harray[i] << "(" << i << ")" << endl;
        }

        cout << "Press return to continue\n";
        char junk;
        cin >> junk;
    }

    return(0);
}


Last edited on
You both have been amazing! Thanks to the two of you, my program is working like a charm!!! I can't believe missing a "=" messed things up and had be baffled for so long!
Topic archived. No new replies allowed.