Struct help

Define a struct called part_rec that has three elements, partnum(string), value(float), quantity(int). Write a function that deletes an element by partnum(unique) from the sorted array of part_rec and keeps the array sorted. It should work for arrays of various sizes. The array is sorted in ascending order.
------------------------------------------------------------------------------

I can define the struct, simple enough. But i am unsure how to delete an element from the array and still keep it sorted. Also how would one cout a struct? If anyone has any ideas I'd love to hear them.

Thanks

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
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

struct part_rec
{
	string partnum;
	float value;
	int quantity;
};

void deletePart(part_rec arr[], part_rec partnum)
{
   
}



void main()
{
	part_rec arr[10] ={{"5", 10, 2},
					   {"3", 16, 5},
					   {"7", 11, 3}};

	


	system("pause");
}
Last edited on
Show your code.
Updated upon request
well, you can't delete an element of an array because arrays are not dynamic, however you can copy one element over another.

for example your function could check the elements of the array for a string partnum == "3", then when it finds a match it goes into a loop that copies the next element over the current one until the end of the array is reached. Basically moving each element up one.
Thing is I dont know how to check the array for said value and everything i try errors out because it cannot convert sting to part_rec or something else to part_rec
Last edited on
well you have an array called arr.

try...
cout << arr[0].partnum;

or...

if(arr[0].partnum == "5") {
cout << "hi";
}
try...
cout << arr[0].partnum;

One can make it much more interesting:
1
2
3
4
5
6
7
8
std::ostream& operator<<(std::ostream& os, const part_rec& obj)
{
  // write obj to stream
  return os;
}


cout << arr[0];

Similar approach applies to part_rec::operator< too.
Topic archived. No new replies allowed.