Could someone tell me how to write this code

Write a program that reads an arbitrary number of integers from user, until he/she choose to stop the input. The numbers should be stored in a vector1 which is dynamically allocated (ie.
a pointer to a vector space). The program shall read and handle each number in a loop, and then allocate new space for it, by increasing the vector one element at a time. (Pay attention to the Tips below!) Take use of the following simplified algorithm:initialize amount to 0 set a pointer to an integer vector to nullptr. read number as long as number is not a stop-value
reserve memory for a temporary vector, which is an element larger than the old one
copy all data from the old to the new vector – no special cases are needed for the first value
release memory space that the old pointer was pointing to
assign pointer (vector), the address value for the new vector save the number in the new larger vector
increase amount read number end
print the numbers deallocate the vector and reset it all to 0's

//code
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

int size;
int* newVector,newSize = size+1;
int temp;
int* mVector = NULL;
int currentSize = 0;


bool expand(int* &vector, int& size)
{
try
{
newVector = new int[newSize];
}
catch(std::bad_alloc)
{
return false;
}
int i;
for (i = 0; i<size; i++)
newVector[i] = vector[i];

delete[] vector;

vector = newVector;
size = newSize;
return true;
}

int main()

{

cout << "Enter integer numbers to store<cancel with 'stop': "<<endl;

char pszArry[256] = {'0'};

int nInput = 0;//The integers that client input

vector<int> vectorAllValue; //Save vector array of dynamic user input

while (1)

{

cin >> pszArry;

if (0 == strcmp(pszArry, "stop") )//Client choose stop

{

cout << "user chooses to exit !" <<endl;

break;

}

else//The client's input is int

{

nInput = atoi(pszArry);

vectorAllValue.push_back(nInput);

}

}
}

Who can help to see this code I written, whether meet the requirements or not?
Last edited on
It is best if you try this and then come back to us with some code to show. No-one is going to do your entire homework for you.
Topic archived. No new replies allowed.