Basically, I've got almost the whole code done, I'm just getting errors and I don't know what I should take out or add to these functions. I know they're linked in a way in that their language should be almost opposite of each other.
In the Insert function, it's supposed to check and see if the new entry already exists (if it does, then the function returns nothing) and if the entry doesn't exist, then it inserts the entry into the array.
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 0;
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*/
}
}
}
And here is the call from the header for the above function:
bool InsertItem(int, char*,char*,double,char*);
Now, this is the one really giving me a headache. I have been on a few other forums, trying to get help with this one but I'm confused by the help that was given to me. Modify is to first search for an entry. If the entry is found, the entry is modified. If the entry is not found, then the entry is inserted into the array.
bool ArrayRecord::Modify(int item, char* fName, char* lName, double gpa, char* pnum)
{
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*/
}
And here is the call in the header for modify.
bool Modify(int, char*, char*, double, char*);
And here are the errors
Error 1 error C2082: redefinition of formal parameter 'item'
Warning 2 warning C4800: 'StudentRecord *' : forcing value to bool 'true' or 'false' (performance warning)
I need examples with very easy language to understand and hopefully a very detailed explanation of what is going on and what I need to do. I am a beginner and I've been out of practice, so I've forgotten some stuff. I need clarification.
Couple things:
The loop in InsertItem will be executed only once, because location is nullified at the start. If you want to check item id before adding new item, the function body should looks something like this:
for (int location = 0; location < length; ++location)
if (list[location].id == item) return false;
list[length].id = item;
list[length].firstName = fName;
list[length].lastName = lName;
list[length].gpa = gpa;
list[length].phonenumber = pnum;
length++;
return true;
The second function should be different:
for (int location; location < length; ++location) {
if (list[location].id == item) {
// modify an item
list[location].firstName = fName;
list[location].lastName = lName;
list[location].gpa = gpa;
list[location].phonenumber = pnum;
return false;
}
}
// add new item
list[length].id = item;
list[length].firstName = fName;
list[length].lastName = lName;
list[length].gpa = gpa;
list[length].phonenumber = pnum;
length++;
return true;
There also would be good to have a check of maximum list length prior adding a new item to it or use std::vector
"Redefinition of formal parameter" is because you brought item in as an input to the function and then declared a second item with the same name (see the second line into ArrayRecord::Modify vs. the list of parameters supplied to the function). Consider trying to declare such a variable like this:
bool myFunc() {
int item;
int item;
// ...
return 0;
}
This is the same thing as having the variable item declared as a function parameter followed by declaring it as a local variable: you have two definitions of the same variable in the same scope.
Secondly, the warning is telling you that the function ArrayRecord::Modify returns a value of the bool type, but in one of your return statements within this function, you are returning a value which is not a bool type and has no "obvious" translation to the bool type. Specifically, the line if (list[location].id == item) return &list[location]; returns an address reference to a location within the list array (which will never be 0 if all goes well), and so the compiler must decide how to translate &list[location] into a true or false return value.
In response to the commented issues, I suggest fixing the "return value" issue mentioned above, followed by adding a "fallback return" in each function such as return 0; as the last statement in each function. While your code appears to cover all possible paths to the ends of the functions with return statements, the compiler disagrees, and your options are to either provide "default" return values in case everything else fails, or to turn off that particular warning in your compiler. The former is much recommended over the latter.
With the warning about failing to have return values in all code paths, note that
while (a < b) {
if (q)
return 1;
else
return 0;
}
is not guaranteed to return a value. The execution of the code might jump over while (a < b) { ... } when a>=b, and the compiler doesn't see any reason that a<b is a guaranteed starting condition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With