Possible to continue count past first int found?

closed account (iT5o8vqX)
Is it possible to continue counting the key past the first time int is found?
the key appears in this specific document 3 times
I would like to access the three times and bring them to the main but I can't seem to figure out a way to do this. Is it even possible without
changing the function from "int" to something else?

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
#include <iostream>
#include <string>
#include <fstream>  //you must include this library if you wish to do file i/o
#include <stack>
using namespace std;

class order_record
{
public:
	string cell_number;
	string item_number;
	double quantity;
	double price;
	int processing_plant;
	double tax_rate;
	double order_tax;
	double net_cost;
	double total_cost;
};

void initialize(order_record * & INV, int & count, int & size);
int search(order_record * &INV, int  count, string key);

void initialize(order_record * & INV, int & count, int & size)
{
	count = 0;
	size = 25;
	INV = new order_record[size];
	ifstream in;
	in.open("purchase_data8.txt");

	if (in.fail())
	{
		cout << "INPUT file did not open correctly" << endl;
		cout << "Program cannot execute." << endl;
		//pops up if file was not open and
		//there for the rest of program cannot execute
	}
	else
	{
		while (!in.eof() && count < size)
		{
			//if (is_full(count, size))
			//{
			//	double_size(INV, count, size);
			//}
			//reads data that was read in file one record at a time
			//prints into new file
			//using ofstream objecty
			in >> INV[count].cell_number;
			in >> INV[count].item_number;
			in >> INV[count].quantity;
			in >> INV[count].price;
			in >> INV[count].processing_plant;
			count++;
		}
	}
	in.close();
}


int search(order_record * &INV, int  count, string key)
{
	//integer function that has three formal parameters
	//array of records INV; input parameter count; input parameter key;
	//which is the cell phone for record you are searching for
	//function return the location of key in INV
	//if it is there is same
	//otherwise -1 is returned
	//searches through doc
	//key 9546321555 at 0, 1 , 11
	//key 9042224556 at 9, 19, 23

	for(int i =0; i<count; i++)
	{
		//cout << "Loop FOR i: " << i <<  '\n'; 

		if(INV[i].cell_number == key)
		{

			//cout << "Cell Numbers " << INV[i].cell_number << '\n';
			cout << "Loop IF i: " << i <<  '\n';
			//returns singular because int
			//if you want next in line then search has to be called again
			//made to skip the one previously it found and
			//then continue searching for the next int
			return i;
		}
	}
	return -1;
}


int main() {
	int count = 0; //initializing count
	int size;
	string key;
	order_record * INV = 0; //declaring the dynamic array

	//Test 1:
	cout << "**********************************************************************\n";
	//void initialize(order_record * & INV, int & count, int & size);
	cout << "Test 1: Testing initialize, double_size, process, is_full" << '\n';
	initialize(INV, count, size);
	cout << "End of Test 1" << '\n';
	//cout << "**********************************************************************\n";

	for(int i =0; i <= count; i++){

		//singular int from the search function?
		//cout << "Key in the main FOR loop search: " << i << '\n';
		if(i != count)
		{
			//omits i but neg statment so only i
			i = search(INV, count, key);
			cout << "Key in the main IF loop search: " << i << '\n';
			//is it possible to continue counting
			//the key past the first time int is found?

		}
		break;
	}
}


Help I've tried every combination I can think of and my brain is officially fried. Thank you in advance.

Maybe something like this (untested) :

1
2
for (int i = 0; i < count && (i = search(INV + i, count - i, key) != -1; ++i)
    cout << "Key found at position " << i << '\n';

closed account (iT5o8vqX)
Tried it but nothing. I kept getting errors:
Invalid arguments '
Candidates are:
int search(order_record * &, int, std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>)
'
AND

invalid initialization of non-const reference of type 'order_record*&' from an rvalue of type 'order_record*'

I tried re-arranging them because I kinda can see what you were going for but nothing.

thank you
Just change line 22 and line 62 to read
int search(order_record* INV, int count, string key)
The difference is the type of the first parameter

closed account (iT5o8vqX)
Returns just the first element from the key but not the second or third.
Yes, you'll have to combine that w/ @dutch's suggestion
closed account (iT5o8vqX)
This is what I tried:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

	for(int i =0; i < count && (i = search(INV + i, count - i, key) != -1); i++){

		//singular int from the search function?
		//cout << "Key in the main FOR loop search: " << i << '\n';

		//if(i != count)
		//{
			//omits i but neg statment so only i
			i = search(INV, count, key);
			cout << "Key in the main IF loop search: " << i << '\n';
			//is it possible to continue counting
			//the key past the first time int is found?

		//}
		break;
	}


And then i tried adding the if into the code but it loops twice in the IF from the search function. It only shows up once in the main function.
It needs to be as I showed it in my post. You certainly don't want to call search a second time in the body of the loop. And you don't want the break either.

And you need to change the signature like mbozzi showed.
closed account (iT5o8vqX)
I did everything but I'm still not getting it?

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
#include <iostream>
#include <string>
#include <fstream>  //you must include this library if you wish to do file i/o
#include <stack>
using namespace std;

class order_record
{
public:
	string cell_number;
	string item_number;
	double quantity;
	double price;
	int processing_plant;
	double tax_rate;
	double order_tax;
	double net_cost;
	double total_cost;
};

void initialize(order_record * & INV, int & count, int & size);
int search(order_record * INV, int  count, string key);

void initialize(order_record * & INV, int & count, int & size)
{
	count = 0;
	size = 25;
	INV = new order_record[size];
	ifstream in;
	in.open("purchase_data8.txt");

	if (in.fail())
	{
		cout << "INPUT file did not open correctly" << endl;
		cout << "Program cannot execute." << endl;
		//pops up if file was not open and
		//there for the rest of program cannot execute
	}
	else
	{
		while (!in.eof() && count < size)
		{
			//if (is_full(count, size))
			//{
			//	double_size(INV, count, size);
			//}
			//reads data that was read in file one record at a time
			//prints into new file
			//using ofstream objecty
			in >> INV[count].cell_number;
			in >> INV[count].item_number;
			in >> INV[count].quantity;
			in >> INV[count].price;
			in >> INV[count].processing_plant;
			count++;
		}
	}
	in.close();
}


int search(order_record * INV, int  count, string key)
{
	//integer function that has three formal parameters
	//array of records INV; input parameter count; input parameter key;
	//which is the cell phone for record you are searching for
	//function return the location of key in INV
	//if it is there is same
	//otherwise -1 is returned
	//searches through doc
	//key 9546321555 at 0, 1 , 11
	//key 9042224556 at 9, 19, 23

	for(int i =0; i<count; i++)
	{
		//cout << "Loop FOR i: " << i <<  '\n';

		if(INV[i].cell_number == key)
		{

			//cout << "Cell Numbers " << INV[i].cell_number << '\n';
			cout << "Loop IF i: " << i <<  '\n';
			//returns singular because int
			//if you want next in line then search has to be called again
			//made to skip the one previously it found and
			//then continue searching for the next int
			return i;
		}
	}
	return -1;
}


int main() {
	int count = 0; //initializing count
	int size;
	string key;
	order_record * INV = 0; //declaring the dynamic array

	//Test 1:
	cout << "**********************************************************************\n";
	//void initialize(order_record * & INV, int & count, int & size);
	cout << "Test 1: Testing initialize, double_size, process, is_full" << '\n';
	initialize(INV, count, size);
	cout << "End of Test 1" << '\n';
	//cout << "**********************************************************************\n";

	//Test 4:
	cout << "**********************************************************************\n";
	cout << "Test 4: Testing bool search" << '\n';
	//key 9546321555 at 0, 1 , 11
	//key 9042224556 at 9, 19, 23
	//int i = 0;
	//int repeat[i] ; //initializes to size of og array
	cout << "Please input key: " << '\n';
	cin >> key;
	//once search gives location check
	//if there are any others in array


	for(int i =0; i < count && (i = search(INV + i, count - i, key) != -1); i++){

		//singular int from the search function?
		//cout << "Key in the main FOR loop search: " << i << '\n';

		if(i != count)
		{
			//omits i but neg statment so only i
			cout << "Key in the main IF loop search: " << i << '\n';
			//is it possible to continue counting
			//the key past the first time int is found?

		}
		//break;
	}

	cout << "BYE BYE" << endl;
	return 0;
}



if it helps this is the data:
9546321555 452-XLY 2 70.82 503
9546321555 742-6Z3 300 10.14 47
5612971340 924-YUT 8 23.00 51
3051234567 9L3-11T 47 50.12 200
7542346622 453-TTT 92 25.25 110
3054432762 213-ABC 15 105.02 111
9544321011 321-XMZ 112 32.41 92
8776219988 444-SSS 100 12.12 86
9042224556 290-P23 297 9.62 500
7877176590 23Z-00Q 32 9.62 201
5617278899 893-42T 642 0.81 72
9546321555 008-LL5 90 45.80 18

the result should be 0, 1 and 11.
I'm sorry if i'm not understanding and putting the code not where you are telling me to.


You're right. It was incorrect. Sorry about that. :)
Try this:

1
2
3
4
5
6
    for (int i = 0, start = 0;
         start < count && (i = search(INV + start, count - start, key)) != -1;
         start += i + 1)
    {
        cout << "Found at position: " << start + i << '\n';
    }

The problem is that the i that's returned is relative to the current starting position, so we need to keep track of the starting position separately and use it to compute the actual position.
There was also an error in my placement of the parens around the search call.
Last edited on
closed account (iT5o8vqX)
Thank you so much! Coding is so detail-oriented that even the slightest difference changes the entire program. You just relieved me of a problem I've been dealing with for the past two weeks. Thank you so much!
Topic archived. No new replies allowed.