If statement inside for loop with respect to elements of a string array

I've looked all over the internet, have asked a much more experienced programmer about it (he directed me to a cplusplus reference page which didn't help much), and scoured the forums. After a couple of hours trying to figure this out myself, I created an account here and am hoping somebody can help me.

The following is the portion of my program giving me issues:

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
#include <iostream>
#include <string>

using namespace std;

// some global variables required //
const string eDICT[119] = 
{"\0", "Hydrogen", "Helium" , "Lithium" , "Berylium" , "Boron" , "Carbon" , "Nitrogen" , "Oxygen"
, "Fluorine" , "Neon" , "Sodium" , "Magnesium" , "Aluminum" , "Silicon" , "Phosphorous"
, "Sulfur" , "Chlorine" , "Argon" , "Potassium" , "Calcium" , "Scandium" , "Titanium"
, "Vanadium" , "Chromium" , "Manganese" , "Iron" , "Cobalt" , "Nickel" , "Copper"
, "Zinc" , "Gallium" , "Germanium" , "Arsenic" , "Selenium" , "Bromine" , "Krypton"
, "Rubidium" ,"Strontium" , "Yttrium" , "Zirconium" , "Niobium" , "Molybdenum" , "Technetium"
, "Ruthenium" , "Rhodium" , "Palladium" , "Silver" , "Cadmium" , "Indium" , "Tin" , "Antimony"
, "Tellurium" , "Iodine" , "Xenon" , "Cesium" , "Barium" , "Lanthanum" , "Cerium" , "Praseodymium"
, "Neodymium" , "Promethium" , "Samarium" , "Europium" , "Gadolinium" , "Terbium" , "Dysprosium"
, "Holmium" , "Erbium" , "Thulium" , "Ytterbium" , "Lutetium" , "Hafnium" , "Tantalum" , "Tungsten"
, "Rhenium" , "Osmium" , "Iridium" , "Platinum" , "Gold" , "Mercury" , "Thalium" , "Lead"
, "Bismuth" , "Polonium" , "Astatine" , "Radon" , "Francium" , "Radium" , "Actinium" , "Thorium"
, "Protactnium" , "Uranium" , "Neptunium" , "Plutonium" , "Americium" , "Curium" , "Berkelium" , "Californium"
, "Einsteinium" , "Fermium" , "Mendelevium" , "Nobelium" , "Lawrencium" , "Rutherfordium" , "Dubnium"
, "Seaborgium"	, "Bohrium"  , "Hassium" , "Meitnerium" , "Darmstadtium" , "Roentgenium" , "Copernicium" 
, "Ununtrium" , "Flerovium" , "Ununpentium" , "Livermorium" , "Ununseptium" , "Ununoctium"
} ;

int main()
{

string element1, cap_input;

getline(cin,element1);
string cap_notation(string element1);
cap_input = cap_notation(element1); 
string first_reactant(string cap_input); // compares user input to the values inside of eDICT [1 ~ 119]

cout << first_reactant (cap_input); /* doesn't print this out; crashes */

return 0;
}

string cap_notation (string element1)
{
	char temp;
	
	temp = toupper(element1[0]); 
	element1[0] = temp;
	
	for (int i=1; element1[i] != '\0'; i++)
	{
	
		temp = tolower(element1[i]); 
		element1[i] = temp;

	}
	
	return element1;
}


string first_reactant (string cap_input)
{
	string error = "I'm sorry, you didn't enter a valid element\n";

	for (int x = 1; x == 119; x++)
	{
	if (cap_input.compare(eDICT[x]) == 0) // this crashes the program
	return eDICT[x];
        else
        return error;
        }
}



When I modify the string first_reactant(string cap_input) function to this :

1
2
3
4
5
6
7
8
9
10
11

string first_reactant (string cap_input)
{
	string error = "NOPE";

	if (cap_input.compare(eDICT[1]) == 0) 
	return eDICT[1];
	
	else 
	return error;
}


The comparison is made and prints a return value for first_reactant(cap_input).

This leads me to believe that it is not in my options to write the following:

1
2
3
4
5
6
7
8
9

        for (int x = 1; x == 119; x++)
	{
	if (cap_input.compare(eDICT[x]) == 0) // since this crashes the program
	return eDICT[x];
        else
        return error;
        }


I need to compare the user input to every element of my array. Does this mean that I have to write out a statement for all 119 elements, or is there an error in my code that I haven't spotted?

Any insight and criticism would be appreciated, thanks.
Last edited on
Lines 32, 34: These are function prototypes and belong outside main

Lines 59,72: These two functions have the same signature. The linker should have given you a "duplicate function" error.

Line 67: You have an extra }

Lines 65 and 78: These are syntactically correct. The more common idiom is simply:
 
  if (cap_input == eDICT[x])


Line 76: You're indexing out of bounds. eDICT is declared to have 119 elements. Those elements are 0 - 118. eDICT[119] is out of bounds.
Sorry. I'm new to the forums and didn't realize that there was a "preview post" button before submitting so I made quite a few mistakes on the initial submission (they are fixed now)

I used

 
if (cap_input == eDICT[x]


and it still crashes the program despite checking up to eDICT[118].

1
2
3
4
5
6
7
8
9
10
11
12
13

string first_reactant (string cap_input)
{
	string error = "I'm sorry, you didn't enter a valid element\n";

	for (int x = 1; x == 118; x++)
	{
	if (cap_input == eDICT[x]) // this still crashes the program
	return eDICT[x];
        else
        return error;
        }
}


Last edited on
That will return error on the first miscompare. The return error; should be outside the for loop.

Your termination condition on the loop is incorrect. A for loop will continue only while the condition (2nd term) is true. Your loop will exit after the first iteration because x == 118 is false and as coded there is no return value after your loop.

1
2
3
4
5
6
7
8
9
string first_reactant (string cap_input)
{    string error = "I'm sorry, you didn't enter a valid element\n";

    for (int x = 1; x < 119; x++)
    {   if (cap_input == eDICT[x]) // this still crashes the program
            return eDICT[x];
    }
    return error;  // outside the loop
} 




Last edited on
I see.
Thank you very much for your help AbstractionAnon :).
Problem solved
Topic archived. No new replies allowed.