Example of an insert and update function in an array help (C ++)

Basically, I have almost all the code, I just get errors and I don't know what I should extract or add to these functions. I know that they are connected in such a way that their language should be almost the opposite.

In the Insert function, it must check and see if a new record exists (if it is, the function returns nothing), and if the record does not exist, then it inserts this record 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 really giving me a headache. I have been on several other forums trying to get help with this but am confused by the help that has been provided to me. Edit First - Search for the entry. If an entry is found, the entry will be updated. If no entry is found, 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 equal to the id value in the current location?
    if (list[location].id == item) return &list[location];
    else location++;
}
/*Do a search if it found then modify the selection but if it not found then insert a new info*/

}

      

And here is the call in the header for the change.

    bool Modify(int, char*, char*, double, char*);

      

And here are the mistakes

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 light language to understand and hopefully a very detailed explanation of what is happening and what I need to do. I am a beginner and I have not practiced so I forgot something. I need clarification.

+3


source to share


2 answers


A couple of things:

The loop in the InsertItem will only run once, because the location is reset at the beginning. If you want to check the ID of an element before adding a new element, the body of the function should look 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; 

      

It would also be nice to check the maximum length of the list before adding a new element to it, or use std :: vector

+2


source


A "formal parameter override" is what you entered item

as a function entry and then declared a second one item

with the same name (see the second line in ArrayRecord::Modify

versus the parameter list fed into the function). Try to declare a variable like this:

bool myFunc() {
  int item;
  int item;
  // ...
  return 0;
}

      

This is the same as a variable item

declared as a parameter to a function, followed by declaring it as a local variable: you have two definitions of the same variable in the same scope.

Second, the warning tells you that the function is ArrayRecord::Modify

returning a value of a type bool

, but in one of the return statements inside that function, you are returning a value that is not a type bool

and has no "obvious" conversion to a type bool

. In particular, the string if (list[location].id == item) return &list[location];

returns a reference to an address within the array list

(which will never be 0

if all goes well), and so the compiler must decide how to translate &list[location]

to true

> or false

.



In response to the commented issues, I propose to fix the "return value" problem mentioned above and then add a "return 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 functions using operators return

, the compiler disagrees and your options should either provide "default" values ​​to return if all else fails, or disable that particular warning in your compiler. The first one is recommended over the last one.

With the warning that there are no return values ​​in all your code, note that

while (a < b) {
  if (q)
    return 1;
  else
    return 0;
}

      

return value is not guaranteed. Code execution can jump over while (a < b) { ... }

when a>=b

, and the compiler sees no reason why the a<b

initial condition is guaranteed.

+1


source







All Articles