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.