Searching class array for a specific word

It fails to search the file. I cannot pinpoint the problem. Here is the code for reference:



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
int main()
{

	Element e [112];  
	int num_elements;
	num_elements = 0;

	loadElements (e, num_elements);
	menu (e, num_elements);

	return 0;
}

/************************************************************
*                 void loadElements                         *
*   This function will load an empty array with             *
*   data from a file.                                       *
*                                                           *
************************************************************/
void loadElements ( Element element_array [], int& num_elements )
{
	fstream inFile;
	inFile.open ( "element_data.txt" , ios:: in );

	Element e1;
	string s;       //placeholder for string
	double d;       //placeholder for double
	int i;          //placeholder for int

	num_elements = 0;  //counter variable

	if (inFile.is_open())
	{
		while ( inFile.good() )
		{
			inFile >> s;
			e1.setName (s);

			inFile >> s;
			e1.setSymbol (s);

			inFile >> i;
			e1.setAtomic_number (i);

			inFile >> d;
			e1.setAtomic_mass (d);

			inFile >> d;
			e1.setMelting_pt (d);

			inFile >> d;
			e1.setBoiling_pt (d);

			inFile.ignore();
			getline(inFile, s);

			e1.setClassification (s);
	
			inFile >> s;
			e1.setCrystal_struct (s);

			inFile >> d;
			e1.setDensity (d);

			inFile.ignore();
			getline(inFile, s);
			e1.setColor (s);


			e1.computeProtonsElectrons(); //method used to calculate the
		                             //numbers of protons & electrons

			e1.computeNeutrons();         //method used to calculate the 
		                             //number of neutrons in an element

			element_array [num_elements] = e1;
			num_elements ++;         

		}
	}
		inFile.close();
	
  }


// This is called in the menu function!! Can provide that 
//code if needed as well (:

/************************************************************
*                     searchName                            *
*   This function will search an array of elements          *
*   based on a name entered by the user                     *
*                                                           *
************************************************************/
void searchName ( Element e[], int num_elements )
{

	bool found = false;
	string input;

	cout << "What word are you searching for? ";
	cin >> input;

		for ( int number = 0 ; number < 121 ; number ++)
		{
			while (input == e[number].getAtomic_number ())
			{
				found = true;
				cout << "Element found!";
				displayElement (e[number]); // displays the element
				break; 
			}

			while (e[number].getName () != input)
			{
				cout << "Element not found!" << endl;
				break;
			}
		}
}

What does the declaration of getAtomic_number () look like?
closed account (DSLq5Di1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
		for ( int number = 0 ; number < 121 num_elements; number ++)
		{
			while (input == e[number].getAtomic_number ()) // string == int??
			{
				found = true;
				cout << "Element found!";
				displayElement (e[number]); // displays the element
				break; 
			}

			while (e[number].getName () != input) // what is the purpose of these nested while loops?
			if (input == e[number].getName())
			{
				cout << "Element found!" << endl;
				return /*found*/;
			}
		}
		cout << "Element not found!" << endl;
Sorry, I fixed that. But its the loadElements function that is giving me trouble. It just crashes the program!
But its the loadElements function that is giving me trouble. It just crashes the program!


is markedly different than

It fails to search the file. I cannot pinpoint the problem.


Please be complete and accurate when you say you're having an issue. By crashes do you mean you get a segmentation fault? Unhandled exception? It hangs and becomes unresponsive?
Topic archived. No new replies allowed.