How to edit a record

Can someone give me fix my function for editing a record? I'm struggling, 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
int Search(Student List[], int Size, string Target)
{
    for(int i = 0 ; i < Size ; i++)
        if (Target == List[i].LastName)
            return i;
    return -1;
}
void Edit_Record(Student List[])
{
    string Target;
    Student Temp2;
    int Index;
    int Size = 0;
    
    cout << "Please enter the student Last Name you want to edit: ";
    cin >> Target;
    Index = Search(List,Size,Target);
    
    if(Index!=-1){
    for(int i = Index; i < Size ; i++){
   
        List[i].FirstName = Temp2.FirstName;
        List[i].LastName = Temp2.LastName;
        List[i].Quiz = Temp2.Quiz;
        for(int j = 0; j < Size ; j++)
            List[i].Test[j] = Temp2.Test[j];
        
        cout<<"Procede to change the student's information"<<endl;
        
        cout << "Enter Student" << List[i].LastName + ", " + List[i].FirstName << "'s new information" << endl;
        cout << "Enter the new First Name: ";
        cin >> List[i].FirstName;
        cout << "Enter the new Last Name: ";
        cin >> List[i].LastName;
        cout << "Enter the new Quiz Score: ";
        cin >> List[i].Quiz;
            cout << setw(20) <<"Scores for Six Tests:" << endl ;
            for (int j = 0 ; j < 6 ; j++)
            {
                cout <<setw(7)<< " Test # " << j+1 << ": " ;
                cin >> List[i].Test[j];
            }
        }
    }
    
}
Last edited on
please help
since you wrote please ;)

I'll just gonna tell you what you're doing right now and I hope this helps:

You create an object of type "Student" named Temp2
We can't see the class definition of Student but I guess after creating the object all Member variables are uninitialized.
=> We don't know what is inside FirstName for example.
(btw. variable names should always start with a lowercase)

What you're doing now is Searching in your list for a Student with a given LastName
(that is correct)

Your for loop begins in your List at the position of the Student with the LastName we found earlier AND (as well!) iterates afterwards over all the other students coming after this Student as well)
(I don't think this is intended)

Then you do List[i].FirstName = Temp2.FirstName;
And the same with LastName and so on..

As I said before "Temp2" is a Student with uninitialized variables.
So we overwrite whatever the Student out of List[i].FirstName was with some undefined value out of Student "Temp2"

Then we actually overwrite the Student at List[i] again this time with
whatever we type in
cin >> List[i].FirstName; // and so on with LastName...

Now since we are in a loop and loop over the remaining students as well
=> we do all that again with the other Students


I guess what you wanna do is: just type in new values for List[i] student (so keep the big cin >> ... stuff in the for loop
remove whatever you do with Temp2 (that seems to be unnecessary code)
actually remove the whole loop and just use List[index] instead of List[i] to only modify this one student

Have fun ;)
Last edited on
Like this you mean? I still can't get it to work

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
void Edit_Record(Student List[], Student & Temp)
{
    int Size = 0;
    string Target;
    int Index;
    cout << "\nEnter a name to search for (Last Name e.g. Smith): ";
    getline(cin,Target);
    Index = Search(List,Size,Target);
    if (Index!=-1){
        cout<<"Procede to change the student's information"<<endl;
        
        cout << "Enter Student" << List[Index].LastName + ", " + List[Index].FirstName << "'s new information" << endl;
        cout << "Enter the new First Name: ";
        cin >> List[Index].FirstName;
        cout << "Enter the new Last Name: ";
        cin >> List[Index].LastName;
        cout << "Enter the new Quiz Score: ";
        cin >> List[Index].Quiz;
            cout << setw(20) <<"Scores for Six Tests:" << endl ;
            for (int j = 0 ; j < 6 ; j++)
            {
                cout <<setw(7)<< " Test # " << j+1 << ": " ;
                cin >> List[Index].Test[j];
            
            }
    }
    else
        cout << "\nThis student information does not exist on the list. Try again.\n";
    
}
can someone help me
Looks like you are coming from java
cout << "Enter Student" << List[Index].LastName + ", " + List[Index].FirstName << "'s new information" << endl;

Also could you be a little more clear.

can't get it to work


What exactly is not working?
Last edited on
It runs and when I select it on the menu it works and prompts for everything but doesn't make any changes when I print it
Topic archived. No new replies allowed.