简体   繁体   English

如何将对象数组中的内容复制到C ++中的另一个数组中?

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

#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. 在此程序中,我必须将输入作为10个人的姓名和余额,并以对应于最高余额的名称和余额显示输出。 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++ 我正在使用Visual Studio Professional 2013,并且代码在Visual C ++中

You cannot assign char[] variable to another char[] variable. 您不能将char[]变量分配给另一个char[]变量。 You should use strcpy function to do that (and dont forget to include<cstring> first). 您应该使用strcpy函数执行此操作(并且不要忘记首先include<cstring> )。 See this how to copy char array to another char array in C? 看到此如何将char数组复制到C中的另一个char数组? 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM