Unsorted Array with functions erroring

Okay, I'm trying to figure out what's going wrong with this program. I thought I figured out the problem, but no it is not doing it. I've wracked my brain for this for over a week and I can't figure out what's making it stupid. I'll also include the header file. Also here are the errors that come up in the complier:
1
2
3
Warning	1	warning C4800: 'StudentRecord *' : forcing value to bool 'true' or 'false' (performance warning)	c:\users\christina\documents\visual studio 2013\projects\array\arrayrecord.cpp	47	1	Array
Error	2	error C2562: 'ArrayRecord::Modify' : 'void' function returning a value	c:\users\christina\documents\visual studio 2013\projects\array\arrayrecord.cpp	129	1	Array
	3	IntelliSense: return value type does not match the function type	c:\Users\Christina\Documents\Visual Studio 2013\Projects\Array\ArrayRecord.cpp	129	41	Array


Here is the main program:
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
  //Implementation of ArrayRecord class
#include <iostream>
#include "ArrayRecord.h"
using namespace std;

ArrayRecord::ArrayRecord()
{
	length = 0;
	currentPos = -1;
}

void ArrayRecord::MakeEmpty()
{
	length = 0;
	currentPos = -1;
}

bool ArrayRecord::IsFull()
{
	if(length == MAX_ITEMS) return 1;
	else return 0;
}

int ArrayRecord::LengthIs()
{
	return length;
}

const StudentRecord* ArrayRecord::RetrieveItem(int item)
{
	int location = 0;
	while(location<length) //while loop is for searching
	{
		if(list[location].id == item) return &list[location];
		else location++;
	}
	return 0;
}

bool ArrayRecord::InsertItem(int item, char* fName, char* lName, double gpa, char* pnum)
{
	int location = 0;
	// int item;

	while (location < length)
	{
		if (list[location].id == item) return &list[location];

		else {

			list[length].id = item;
			list[length].firstName = fName;
			list[length].lastName = lName;
			list[length].gpa = gpa;
			list[length].phonenumber = pnum;
			length++;
			return 1;
			/*for the duplicate id prevention search id first if found then return function immediately otherwise start insertion*/
		}
	}
}

void ArrayRecord::DeleteItem(int item)
{	
	int location = 0;
	while(location < length)
	{
		if(item == list[location].id)
		{//Hit, delete
			list[location] = list[length-1];
			length--;
			return;
		}
		location++;
	}
}

void ArrayRecord::ResetList()
{
	currentPos = -1;
}

bool ArrayRecord::IsLastItem()
{
	return (currentPos >= length-1);
}

const StudentRecord* ArrayRecord::GetNextItem()
{
	if(IsLastItem()) return 0;
	currentPos++;
	return &list[currentPos];
}

void ArrayRecord::PrintAll()
{
	for(int i=0;i<length;i++) cout<<list[i].id<<" "<<list[i].firstName<<" "<<list[i].lastName<<endl;
}

void ArrayRecord::Swap(int id1, int id2)
{
	int location1 = 0, location2 = 0;
	while (location1 < length)
	{
		if (list[location1].id == id1) break;
		else location1++;
	}
	while (location2 < length)
	{
		if (list[location2].id == id2) break;
		else location2++;
	}
	if (location1 == length || location2 == length)
	{
		return;
	}
	StudentRecord temp;
	temp = list[location1];
	list[location1] = list[location2];
	list[location2] = temp;
}

void ArrayRecord::Modify(int id,char*,char*,float,char*)
{
	int location = 0;
	int item; 
	while (location < length)
	{
		if (list[location].id == item) return &list[location];
		else location++;
	}
	/*Do a search if it's found then modify the selection but if it's not found then insert a new info*/
}


And here is the header:
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
//Specification for ArrayRecord class

#ifndef ARRAYRECORD_H
#define ARRAYRECORD_H
#define MAX_ITEMS 100
#include <string>
using namespace std;

struct StudentRecord
{
	int id;
	string lastName;
	string firstName;
	double gpa;
	string phonenumber;

};

class ArrayRecord
{
protected:
	StudentRecord list[MAX_ITEMS];
	int length;
	int currentPos;
public:
	ArrayRecord();
	void MakeEmpty();
	bool IsFull();
	int LengthIs();
	const StudentRecord* RetrieveItem(int);
	bool InsertItem(int, char*,char*,double,char*);
	void DeleteItem(int);
	void ResetList();
	const StudentRecord* GetNextItem();
	bool IsLastItem();
	void Swap(int id1, int id2);
	void Modify(int id, char*, char*, float, char*);
	void PrintAll();
};





#endif 

Error	2	error C2562: 'ArrayRecord::Modify' : 'void' function returning a value	c:\users\christina\documents\visual studio 2013\projects\array\arrayrecord.cpp	129	1	Array


The code tries to return a value from a function defined to return nothing (void).

1
2
3
4
5
6
7
8
9
10
11
void ArrayRecord::Modify(int id,char*,char*,float,char*)
{
	int location = 0;
	int item; 
	while (location < length)
	{
		if (list[location].id == item) return &list[location];
		else location++;
	}
	/*Do a search if it's found then modify the selection but if it's not found then insert a new info*/
}
Last edited on
So, I should get rid of the &list[location]? Or do I break it?
Does the function that will call Modify need a returned value?

You're also missing the names of the parameters on this line (other than id)
void ArrayRecord::Modify(int id,char*,char*,float,char*)
Last edited on
When Modify is called, and there is a record, then it modifies the record
It doesn't sound like you need to return a value unless you want to confirm that an update took place (bool - true/false) or you need to know what location was updated back in the calling function?

Also - item is uninitialized - how do you know if there is a match here?
if (list[location].id == item)
Okay a few things. For the parameters of Modify, what I will need to change is this, right? if (list[location].id == item) return &list[location];

I would also like modify to show what record was updated. But I'm not sure how to start that.
Also, item being uninitialized, will it need to be set to 0 or 1? I'm not getting exactly what you mean by the it being matched.
If modify is going to return the location, the return type can't be void - it needs to be the type of whatever value you want to return.

For the parameters of modify - you need to replace the name below with whatever that variable is going to be called in the function.

1
2
3
4
5
6
7
8
9
10
11
12
void ArrayRecord::Modify(int id,char* name,char* name,float name,char* name)
{
	int location = 0;
	int item; //item has no valid value
	while (location < length)
	{
//item has no valid value - how do you know it's equal to the id value in the current location?
		if (list[location].id == item) return &list[location];
		else location++;
	}
	/*Do a search if it's found then modify the selection but if it's not found then insert a new info*/
}
Last edited on
Topic archived. No new replies allowed.