Hello. I seem to be having a bit of problem with my if statements in a for loop.
My code involves asking the user for the name of an element (chemistry), searching a text file line by line for an element that matches the input, and outputting other information regarding that element.
One line of the text file looks like this: Helium,He,2,4.00
I've tried running the code without the if loop and it outputs normally (If I ask it to output elementname it will output the first element Hydrogen, which is correct.) but I am not sure what goes wrong when I add the if statement. For some reason, it always follows the "else" statement (It always outputs Invalid search even when I input the right elements).
When you enter element name it starts searching for element, compares first element (Hydrogen) with your string, sees that they aren't equal and runs else statement, where it first outputs "invalid search" and hen terminates loop.
If you delete break from else, you will see your program outputting "invalid search" n-1 times where n is the element number.
Also you should change line 18 to use getline too, or it probably will not find anything too.
If you want to incorporate this code into larger program and run search multiple times, it is better to load file into container first and then run searches on it without need to access file over and over. And instead of using several arrays you should define a structure holding element details.