How to copy contents in an array of object into another array in C++?

Abhishek
#include "stdafx.h"
#include<iostream>
using namespace std;
class bank
{
public: char name[20];
public: float balance;
public: void get()
{
    cout << "\nEnter name and balance in Account respectively\n";
    cin >> name >> balance;
}
public: void display()
{
    cout << "Name: " << name << "    Balance: " << balance;
}
};
void main()
{
    int i = 0;
    bank b[10];
    bank max;
    bank temp;
    for (i = 0; i < 10; i++)
    {
        b[i].get();
    }
    for (i = 0; i < 10; i++)
    {
        if (b[i + 1].balance > b[i].balance)
        {
            max.balance = b[i + 1].balance;
            max.name = b[i + 1].name;//Error 1 error C2106: '=' : left operand must be l-value
        }
        else
        {
            temp.balance = b[i + 1].balance;
            temp.name = b[i + 1].name;//Error 2 error C2106: '=' : left operand must be l-value
            b[i + 1].balance = b[i].balance;
            b[i + 1].name = b[i].name;//Error 3 error C2106: '=' : left operand must be l-value
            b[i].balance = temp.balance;
            b[i].name = temp.name;//Error 4 error C2106: '=' : left operand must be l-value
            max.balance = b[i + 1].balance;
            max.name = b[i + 1].name;//Error 5  error C2106: '=' : left operand must be l-value
        }

    }
    max.display();
}

In this program I've to take input as name and balance of 10 people and display the output as name and balance corresponding to the highest balance. Besides the errors shown in the code itself as comments, I'm also getting another code at same those lines only where I've put a comment regarding the error. That other error is:

IntelliSense: expression must be a modifiable lvalue

I'm using Visual Studio Professional 2013 and the code is in visual C++

Schro

You cannot assign char[] variable to another char[] variable. You should use strcpy function to do that (and dont forget to include<cstring> first). See this how to copy char array to another char array in C? for further explanation.

if it changes like this, It should be ok:

#include<iostream>
#include<cstring>
using namespace std;
class bank
{
    public: char name[20];
    public: float balance;
    public: void get()
    {
        cout << "\nEnter name and balance in Account respectively\n";
        cin >> name >> balance;
    }
    public: void display()
    {
        cout << "Name: " << name << "    Balance: " << balance;
    }
};
void main()
{
    int i = 0;
    bank b[10];
    bank max;
    bank temp;
    char tempstr[20]; // use this for temporary string
    for (i = 0; i < 3; i++)
    {
        b[i].get();
    }
    for (i = 0; i < 2; i++) // you should change the upper bound to 9
                            // so it not crash when i = 9
    {
        if (b[i + 1].balance > b[i].balance)
        {
            max.balance = b[i + 1].balance;
            strcpy(max.name, b[i+1].name); // use strcpy
        }
        else
        {
            temp.balance = b[i + 1].balance;
            strcpy(temp.name, b[i+1].name); // use strcpy
            b[i + 1].balance = b[i].balance;
            strcpy(b[i+1].name, b[i].name); // use strcpy
            b[i].balance = temp.balance;
            strcpy(b[i].name, temp.name); // use strcpy
            max.balance = b[i + 1].balance;
            strcpy(max.name, b[i+1].name); // use strcpy
        }

    }
    max.display();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to copy the contents of one 2d array to another? (VBA)

From Dev

How to copy contents of an array to a variable

From Dev

Transferring contents of an array to another in C

From Dev

Trying to copy contents of array

From Dev

Copy array of strings into another array in C

From Dev

How to properly copy contents of one char array into another when passed to function, WITHOUT using STL?

From Dev

How to copy array value to another array

From Dev

how to copy elements that are in an array but not in another on a third array

From Dev

how to copy the elements of an array to another array

From Dev

c pointers and arrays, copying contents into another array

From Dev

c pointers and arrays, copying contents into another array

From Dev

Copy the contents of array or memory block to another specific type, using Delphi

From Dev

Not able to copy the contents of one array to another in Angular 5

From Dev

How to copy a Object<T> to an array

From Dev

Is it possible to copy contents of a HttpServlet Request object into an array list?

From Dev

How do I fill a column of an array with the contents of another array?

From Dev

How do I fill a column of an array with the contents of another array?

From Dev

How to copy array keys to another array, and replace values with empty array?

From Dev

Copy elements of one array to another in C

From Dev

How to copy contents from one file to another word by word in C?

From Dev

How to copy a character array into another character array in 'c' using pointers and without using string library

From Dev

How to copy an array to another with some logic

From Dev

How to copy an array to another with some logic

From Dev

Copy contents of a unique pointer array in the copy constructor

From Dev

Copy contents of a unique pointer array in the copy constructor

From Dev

How to copy contents of Excel UsedRange into an array for faster reading?

From Dev

How to copy a csv file contents and rewrite with additional array in the same file?

From Java

Copy array items into another array

From Dev

How to print the contents of an array to a label in c#

Related Related

  1. 1

    How to copy the contents of one 2d array to another? (VBA)

  2. 2

    How to copy contents of an array to a variable

  3. 3

    Transferring contents of an array to another in C

  4. 4

    Trying to copy contents of array

  5. 5

    Copy array of strings into another array in C

  6. 6

    How to properly copy contents of one char array into another when passed to function, WITHOUT using STL?

  7. 7

    How to copy array value to another array

  8. 8

    how to copy elements that are in an array but not in another on a third array

  9. 9

    how to copy the elements of an array to another array

  10. 10

    c pointers and arrays, copying contents into another array

  11. 11

    c pointers and arrays, copying contents into another array

  12. 12

    Copy the contents of array or memory block to another specific type, using Delphi

  13. 13

    Not able to copy the contents of one array to another in Angular 5

  14. 14

    How to copy a Object<T> to an array

  15. 15

    Is it possible to copy contents of a HttpServlet Request object into an array list?

  16. 16

    How do I fill a column of an array with the contents of another array?

  17. 17

    How do I fill a column of an array with the contents of another array?

  18. 18

    How to copy array keys to another array, and replace values with empty array?

  19. 19

    Copy elements of one array to another in C

  20. 20

    How to copy contents from one file to another word by word in C?

  21. 21

    How to copy a character array into another character array in 'c' using pointers and without using string library

  22. 22

    How to copy an array to another with some logic

  23. 23

    How to copy an array to another with some logic

  24. 24

    Copy contents of a unique pointer array in the copy constructor

  25. 25

    Copy contents of a unique pointer array in the copy constructor

  26. 26

    How to copy contents of Excel UsedRange into an array for faster reading?

  27. 27

    How to copy a csv file contents and rewrite with additional array in the same file?

  28. 28

    Copy array items into another array

  29. 29

    How to print the contents of an array to a label in c#

HotTag

Archive