Comparison

Hello everyone,
I am writing a program that will load a text file containing the periodic table of elements into an array of struct's and then use the data to search for certain things in the array.
Right now I am attempting to craft a search function that will search the structs in the array for which ever string is entered, in this case the element code of an element, (Au for gold, as an example) and return the position number to be used in displaying what ever is desired.
I am having problems getting the search function (codeSearch()) to work properly, and any advice in the matter would be greatly appreciated.
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct periodic
{
       string code;
       string name;
       int chemNum;
       double chemWeight;
};
const int NUM_ELM = 103;

void getData(ifstream& inFile, periodic table[], int NUM_ELM);
void Menu();
int binarySearch( periodic table[], int NUM_ELM, int atomicNum);
int codeSearch(periodic table[], int NUM_ELM, string code  );

int main()
{
    ifstream fileIn;
    periodic table[NUM_ELM];
    int option, atomicNum, index;
    string code;
    
    fileIn.open("periodic.txt");
    
    if (fileIn.fail() )
    {
                      cout << "Problem opening file";
                      exit(-1);
    }
    getData(fileIn, table, NUM_ELM);
    
    do{
                    Menu();
                    cin >> option;
                    while (option < 1 || option > 3)
                    {
                          cout <<"ENTER A VALID MENU OPTION";
                          cin >> option;
                    }
                    if (option != 3)
                    {
                               switch(option)
                               {
                                             case 1:
                                                  cout << "Enter the atomic number of the element desired" << endl;
                                                  cin >> atomicNum;
                                                  index = binarySearch(table, NUM_ELM, atomicNum);
                                                  if (index > 0)
                                                  {
                                                            cout << " **************************************** " << endl;
                                                            cout << "\t" << table[index].code << " " << table[index].name <<
                                                            " " << table[index].chemNum << " " << table[index].chemWeight << endl;
                                                            cout << " **************************************** " << endl;
                                                  }
                                                  else
                                                      cout << "ERROR: ELEMENT NOT FOUND" << endl;
                                                  break;
                                             case 2:
                                                  cout << "Enter the element code of the element desired" << endl;
                                                  cin >> code;
                                                  index = codeSearch(table, NUM_ELM, code);
                                                  if (index > 0)
                                                  {
                                                            cout << " **************************************** " << endl;
                                                            cout << "\t" << table[index].code << " " << table[index].name <<
                                                            " " << table[index].chemNum << " " << table[index].chemWeight << endl;
                                                            cout << " **************************************** " << endl;
                                                  }
                                                  else
                                                      cout << "ERROR: ELEMENT NOT FOUND" << endl;                                                  
                                                  break;
                               }
                    }
                                                            
                                                  
    }while (option != 3);
    
    fileIn.close();
    
    system("pause");
    return 0;
}

void getData(ifstream& inFile, periodic table[], int NUM_ELM)
{
     for (int i = 0; i < NUM_ELM; i++)
     {
         inFile >> table[i].code >> table[i].name >> table[i].chemNum >> table[i].chemWeight;
     }
     
}

void Menu()
{
     cout << "\n\t\tPeriodic Table of Elements Menu\n\n"
          << "1. Search for an element using the elements atomic number\n"
          << "2. Search for an element using the elements element code\n"
          << "3. Exit the Program\n";
}

int binarySearch( periodic table[], int NUM_ELM, int atomicNum)
{
        int first = 0,                 
        last = NUM_ELM - 1,       
        middle,                    
        position = -1;             
    bool found = false;            

    while (!found && first <= last)
    {
        middle = (first + last) / 2;     
        if (table[middle].chemNum == atomicNum)      
        {
            found = true;
            position = middle;
        }
        else if (table[middle].chemNum > atomicNum)  
            last = middle - 1;
        else
            first = middle + 1;          
    }
    return position;
    
}

int codeSearch(periodic table[], int NUM_ELM, string code  )
{
    string temp;
    int index = 0;
    int position = -1;
    bool found = false;
    while (index < NUM_ELM && !found)
    {
          table[index].code = temp;
          if(temp.compare(code))
          {
                                found = true;
                                position = index;
          }
          index++;
    }

    return position;
}
be more specific, what exactly is going wrong? compile issue, wrong output, no output. We need some details to work off of.
Maybe temp = table[index].code; instead of table[index].code = temp;
Yea, I already got it figured out a few hours after I posted this, I completely changed the search code around.
Topic archived. No new replies allowed.