如何在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 respectivelyn";
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();
}
在这个程序中,我必须将输入作为 10 人的名称和余额,并将输出显示为与最高余额对应的名称和余额。除了代码本身显示为注释的错误外,我还仅在对错误进行注释的地方在同一行中获得了另一段代码。另一个错误是:
IntelliSense: expression must be a modifiable lvalue
我正在使用Visual Studio Professional 2013,代码是Visual C++
不能将char[]
变量分配给另一个char[]
变量。您应该使用strcpy
函数来执行此操作(并且不要忘记先include<cstring>
(。看到这里如何将字符数组复制到 C 中的另一个字符数组?以进一步解释。
如果它像这样改变,应该没问题:
#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 respectivelyn";
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();
}
相关文章:
- 有没有办法使用 strcpy 将字符串数组复制到另一个字符串或其他数组中?
- 如何将一个结构的字符数组复制到结构的另一个字符数组中?
- 将字符数组复制到 QByte 数组中,而无需传递指针
- std::transform 将一个结构数组复制到另一个结构数组
- 如何使用指针C++将元素从2D数组复制到1D数组
- 如何将 int4 数组复制到 int8 向量中
- 如何将多维字符数组复制到字符 * 中
- std::将三维数组复制到三维向量中
- 为什么我不能将 C 样式数组复制到 std::array?
- 有没有办法将最初作为字符串输入的数字字符数组复制到双向链表中
- 为什么当我想将(使用 memcpy)结构数组复制到字符数组时,字符数组为空
- 如何将数据从数组复制到C++特征矩阵或向量
- 将数组复制C++作为特征矩阵的向量
- 如何在 c++ 中将数组复制到文件
- 如何将对象数组作为构造函数中的参数传递,然后将传递的数组复制到另一个数组(仍在构造函数中)
- CUDA 将大于线程计数的数组复制到共享内存
- 可以使用 std::copy 将 3D 数组复制到矢量
- 如何在C++中将本地字符串数组复制到私有成员字符串数组
- 如何使用 std::copy 将一个 constexpr 数组复制到另一个 constexpr 数组
- 如何将 char 类型数组传递给函数并将第一个字符数组复制到第二个图表数组并使用 c++ 进行比较