How to Search a .txt Loaded into an Array

I have successfully loaded my text file into an array. However, when I use a function to search the array for a certain element and then echo another one, it doesn't seem to display anything at all.

I feel that the problem is that the "currentIndex" in my int loadData function is not referenced in my shopping function.

For anyone willing to assist me, here is the code in question:

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
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

struct Item {
  string code;
  string name;
double price;
};

int currentIndex;
const int MAX_ITEMS = 100;
Item items[MAX_ITEMS];

void greeting ();
int loadData(Item items[]);
void shopping(Item items[]);
void endProgram ();


int main () {

    greeting ();

    loadData(items);
    
    shopping(items);
        
    endProgram ();
}

void greeting () {
    cout << "Hello, Welcome to my Grocery!" << endl;
}

int loadData (Item items[]) {
    int currentIndex = 0;
	
    string inFileName;
    ifstream inFile;
    string input;
    
    //Retrieving file...
    cout << "Please enter the pathname to the backup file: ";
    cin >> inFileName;
    inFile.open(inFileName.c_str());
    
	while (!inFile.is_open()) {
        cout << "Please enter input file name (or q to quit): ";
        cin >> inFileName;
        inFile.open(inFileName.c_str());
        if (inFileName == "q") {
            exit(0);
        }
    
    }
    
    cout << "File opened successfully. " << endl;

    //Input into Array
	while (getline(inFile, input)) { 
          string str = input;
          string code1, name1;
          double price1;
          code1 = items[currentIndex].code = str.substr(0,9);
          name1 = items[currentIndex].name = str.substr(10,26);
          price1 = items[currentIndex].price = atof(str.substr(35,6).c_str());
    
     //Echo the file in terms of Array
          cout << code1 << "" << name1 << "" << price1 << endl;          
    }
	
    system("Pause");   
}

//Searching Array using imputs
void shopping(Item items[]){
     string getName;
     while (getName != "q"){
           cout << "Please enter barcode (Press q to exit): ";
           getline(cin,getName);
           for(int i = 0; i < currentIndex; i++) {
             if(items[i].code.find(getName) == 0) {
             cout << items[i].code<<items[i].name << endl;   
             }                                              
           }  
     }                
}
          
void endProgram () {
    cout << "Thank you for shopping at my Grocery! " << endl;
    system ("Pause");
    exit(1);

	return;
}


Thanks in advance for any help that I receive. I really appreciate it.

-Gin
Last edited on
so what does a line of data look like ?
You have a global variable currentIndex at line 16.
In addition, inside function loadData () there is a local variable with the same name.

This local variable "hides" the global one within the function, so it is the local version (defined on line 42) which is used.


When that function terminates, the local variables no longer exist.

Thus later on in function shopping() reference is made to currentIndex. This time it is the global version which is used. Since this is the very first time that this particular variable has been referred to at all, it still has the value which it had at the start of the program.

The "quick and dirty" solution is to delete the new definition at line 42.
It is still necessary to initialise it to zero. You could do that where the variable is first defined.

A better solution is not to use global variables, but instead to pass this variable as a parameter to any function which needs to use it. That would mean passing by reference to function loadData() in order that the value can be updated and retained after the function ends.
Last edited on
Topic archived. No new replies allowed.