[英]Using dynamic memory allocation to add elements into an array
我应该写一些代码来要求用户输入一个数组(1.3 4 5.2 16.3 9.99 7.21 4.5 7.43 11.21 12.5)。
之后,我创建了一个更大的数组(两倍大),将所有元素从旧数组复制到新数组,然后要求用户继续向新数组中再输入5个元素:1.5 4.5 9.5 16.5 7.5 11.5,然后打印出最终数组(15个元素)。
这是我的代码:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
double* read_data(int& size)
{
int max = 10;
double* a = new double[max]; // allocated on heap
size = 0;
cout << "Enter the array: " << endl;
while (cin >> a[size])
{
size++;
}
if (size >= max)
{
double* temp = new double[max * 2];
for (int i = 0; i < size; i++)
{
temp[i] = a[i];
}
delete[] a;
a = temp;
max = max * 2;
}
return a;
}
int main ()
{
int input1, input2, input3, input4, input5;
int size = 0;
double* arr = read_data(size);
cout << "Please enter 5 more elements: " << endl;
cin >> input1 >> input2 >> input3 >> input4 >> input5;
arr[10] = input1;
arr[11] = input2;
arr[12] = input3;
arr[13] = input4;
arr[14] = input5;
cout << "The final array is: " << endl;
for (int i = 0; i < 15; i++)
{
cout << arr[i];
}
system("pause");
return 0;
}
它不允许我再输入5个元素,而且我也不知道为什么。 请帮忙。
您正在等待输入流进入“假”状态,这种情况在输入文件字符末尾时最明显。 因此,在将代码输入到终端时,如果键入-d,则应该在while循环之后看到代码移至该段中。
为了解决这个问题,您将必须为从用户处获得的数组指定一个限制,以免覆盖内存超出范围。
因此,将您的代码更改为此
#include <iostream>
#include <string>
using namespace std;
double* read_data(int& size)
{
int max = 10;
double* a = new double[max]; // allocated on heap
size = 0;
cout << "Enter the array: " << endl;
while (size < 10 && cin >> a[size])
{
size++;
}
if (size >= max)
{
double* temp = new double[max * 2];
for (int i = 0; i < size; i++)
{
temp[i] = a[i];
}
delete[] a;
a = temp;
max = max * 2;
}
return a;
}
int main ()
{
int input1, input2, input3, input4, input5;
int size = 0;
double* arr = read_data(size);
cout << "Please enter 5 more elements: " << endl;
cin >> input1 >> input2 >> input3 >> input4 >> input5;
arr[10] = input1;
arr[11] = input2;
arr[12] = input3;
arr[13] = input4;
arr[14] = input5;
cout << "The final array is: " << endl;
for (int i = 0; i < 15; i++)
{
cout << arr[i] << ' ';
} cout << endl;
system("pause");
return 0;
}
请注意,我是如何在“ cin
”语句之前添加对size变量的检查的,这称为短路( https://en.wikipedia.org/wiki/Short-circuit_evaluation )。 我本人并不认为这种好风格,但我在此处包括了它,以向您展示如何在while循环中使用输入语句,如果使用不当,可能会导致不良行为。
我还添加了一个cout << endl;
刷新流缓冲区,以便输出在pause
之前进入屏幕
我只是读了您在评论中所说的内容,并建议使用以下代码来退出“ q”时退出。
#include <iostream>
#include <string>
using namespace std;
double* read_data(int& size)
{
int max = 10;
double* a = new double[max]; // allocated on heap
size = 0;
cout << "Enter the array: " << endl;
char character;
while (size < 10 && cin >> character)
{
if (character == 'q') break;
a[size] = character - '0';
size++;
}
if (size >= max)
{
double* temp = new double[max * 2];
for (int i = 0; i < size; i++)
{
temp[i] = a[i];
}
delete[] a;
a = temp;
max = max * 2;
}
return a;
}
int main ()
{
int size = 0;
double* arr = read_data(size);
cout << "Please enter 5 more elements: " << endl;
for (int i = size; i < size + 5; ++i) {
cin >> arr[i];
}
// add 5 to the size
size += 5;
cout << "The final array is: " << endl;
for (int i = 0; i < size; i++)
{
cout << arr[i] << ' ';
} cout << endl;
system("pause");
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.