Searching a Strucutre Array

Hello, I'm having trouble with returning a matching value from a function that is passed a structure array and a value to be matched. I have tried various combinations of code and have had no luck with reading about examples that do not use c-string library functions. The program is filling the structure with the data from a txt file successfully, so this is definitely an issue with my codeSearch function. Thank you for your time.

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
main() {
	// Declarations 
	int choice, eleWeight, queryStub1 =0, queryStub2 =0;
	string eleCode;
	
	// Calling function to fill struct array
	loadArray(chemArray, NUM_CHEM);
	choice = displayMenu(choice); 
	if (choice == 1) {
		cout << "Enter the element's symbol (code)" << endl;
		cin >> eleCode;
		queryStub1 = codeSearch(chemArray, eleCode);
		if (queryStub1 == -1) {
			cout << "The element was not found, please restart and try again" << endl;
		}
		else
			cout << chemArray[queryStub1].chemCode << endl;
			cout << chemArray[queryStub1].chemName << endl;
			cout << chemArray[queryStub1].chemNum << endl;
			cout << chemArray[queryStub1].chemWeight << endl;
		}


int codeSearch(struct ChemInfo chemArray[], string eleCode) {
	// Declarations
	int queryStub1 = -1;
	for (int index =0; index < NUM_CHEM; index++) {
		if (chemArray[index].chemCode == eleCode) {
			queryStub1 = index;
			break;
		}
	}
	return queryStub1;	
}
The rest off the code if it helps.
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
// Program Description: practice with structures
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// Declaring maxinum size of periodic table
const int NUM_CHEM = 103;
// Declaring structure for chemical compounds
struct ChemInfo {
	string chemCode;
	string chemName;
	int chemNum;
	int chemWeight;
};
// Creating an array with ChemInfo structure
ChemInfo chemArray[NUM_CHEM];
// Declaring prototypes for functions
void loadArray(struct ChemInfo chemArray[], int NUM_CHEM);
int displayMenu(int);
int codeSearch(struct ChemInfo chemArray[], string eleCode);
int weightSearch(struct ChemInfo chemArray[], int eleWeight);
// End of prototypes
// Start of main
main() {
	// Declarations 
	int choice, eleWeight, queryStub1 =0, queryStub2 =0;
	string eleCode;
	
	// Calling function to fill struct array
	loadArray(chemArray, NUM_CHEM);
	choice = displayMenu(choice); 
	if (choice == 1) {
		cout << "Enter the element's symbol (code)" << endl;
		cin >> eleCode;
		queryStub1 = codeSearch(chemArray, eleCode);
		if (queryStub1 == -1) {
			cout << "The element was not found, please restart and try again" << endl;
		}
		else
			cout << chemArray[queryStub1].chemCode << endl;
			cout << chemArray[queryStub1].chemName << endl;
			cout << chemArray[queryStub1].chemNum << endl;
			cout << chemArray[queryStub1].chemWeight << endl;
		}
	
	if (choice == 2) {
		cout << "Enter the element's weight (element number)" << endl;
		cin >> eleWeight;
		queryStub2 = weightSearch(chemArray, eleWeight);
		if (queryStub2 == -1) {
			cout << "The element was not found, please restart and try again" << endl;
		}
		else
			cout << chemArray[queryStub2].chemCode << endl;
			cout << chemArray[queryStub2].chemName << endl;
			cout << chemArray[queryStub2].chemNum << endl;
			cout << chemArray[queryStub2].chemWeight << endl;
	}
}
// End of main
// Functions:
// Function for populating structure array from file
void loadArray(struct ChemInfo chemArray[], int NUM_CHEM) {
	ifstream theFile;
	theFile.open("periodic.txt");
	int index = 0;
	while (theFile >> chemArray[index].chemCode >> chemArray[index].chemName >> chemArray[index].chemNum >> chemArray[index].chemWeight)
	index++;
}
// End of loadArray function
// Function for displaying a menu with options for searching the periodic table
int displayMenu(int) {
	int choice = 0;
	cout << "\t\Periodic Table Menu\n\n"
	 << "1. Search for a chemical based on the element symbol (code)\n"
	 << "2. Search for a chemical based on the  atomic weight(element number)\n"
	 << "Enter your choice: "; 
	cin >> choice;
	if (choice == 1) {
		return choice;
	}
	else if (choice == 2) {
		return choice;
	}
	else 
		cout << "Please enter a valid number option" << endl;
		cin >> choice;
}
// End of Display function
// Function for searching for element based on chemical symbol
int codeSearch(struct ChemInfo chemArray[], string eleCode) {
	// Declarations
	int queryStub1 = -1;
	for (int index =0; index < NUM_CHEM; index++) {
		if (chemArray[index].chemCode == eleCode) {
			queryStub1 = index;
			break;
		}
	}
	return queryStub1;	
}
// End of function
// Function for searching for element based on chemical weight
int weightSearch(struct ChemInfo chemArray[], int eleWeight) {
	// Declarations
	int queryStub2 = -1, index = 0;
	bool found = false;
	while (index < NUM_CHEM && !found) {
		if (chemArray[index].chemNum == eleWeight) {
			found = true;
			queryStub2 = index;
			break;
		}
	index++;
	}
	return queryStub2;	
}
Well, I made up a (shorter) table of elements ... and your code appears to "work".


Snickelfritz wrote:
The program is filling the structure with the data from a txt file successfully

How do you conclude that? I don't see anywhere in your code where you write out in full the data that you believe you have read.
There are very good reasons from routine loadArray() why that might not be the case - for example you aren't necessarily reading precisely NUM_CHEM elements, you might in fact be trying to read into an array element one past the end of the array, and there are very good physical reasons why weight might be a double, not an int.
Write a few lines of code in main() to check that your data is loaded correctly.

Snickelfritz wrote:
so this is definitely an issue with my codeSearch function

There doesn't seem to be much wrong with that function.



A number of other problems from a first glance:
- It should be int main(), not just main()

- function displayMenu doesn't necessarily return anything (look at the end of it); nor is its only argument specified.

- function displayMenu is called bizarrely on line 32:
choice = displayMenu(choice);

- In the following lines, only the first two are concerned with the else. This isn't Python: you need braces to block several statements, not just indentation.
1
2
3
4
5
		else
			cout << chemArray[queryStub1].chemCode << endl;
			cout << chemArray[queryStub1].chemName << endl;
			cout << chemArray[queryStub1].chemNum << endl;
			cout << chemArray[queryStub1].chemWeight << endl;

The same happens on lines 55-58 and (presumably, although not fatally) on line 88.

- \P is not a valid escape sequence on line 75.

Your compiler should (with the appropriate compiler options) be flagging many of these: don't ignore warnings.

- On the Chemistry side, atomic number is NOT atomic weight. Moreover, since atomic weight is a weighted average over isotopes I would expect weight to be a double, not an int.

- in conjunction with the above, procedure weightSearch() should really be called numberSearch().



Your code is over-commented (don't state the "bleedin' obvious"), but you could do with at least putting a few line spaces between routines.

With the following periodic.txt file (note the ints in the final column) AND changing NUM_CHEM to 4
H     Hydrogen     1    1
He    Helium       2    4
Li    Lithium      3    7
Be    Beryllium    4    9

the code produced
Periodic Table Menu

1. Search for a chemical based on the element symbol (code)
2. Search for a chemical based on the  atomic weight(element number)
Enter your choice:
1
Enter the element's symbol (code)
Li
Li
Lithium
3
7

I still think your loadArray() function is not correctly dealing with the end of file, though. You should read into a dummy Cheminfo variable first, then copy it to the array only if it was successfully read. Otherwise you try to read into something beyond the end of the array.
Last edited on
Thank you for the constructive criticism, I was ignoring a warning about the /P, and I thought I knew the file was loading correctly, because I had placed output statements after the array loading function and they outputted the correct information before I removed them. The function name for weight is funky because that's the way the my professor wanted it named in the assignment ( confusing I know). I've also been bit by the python and it tends to come back in my code.
I've got her running great now, thank you sir!
Topic archived. No new replies allowed.