c++

C++ Program to Change User Input Array Size Dynamically Full Example Project For Beginners


 

Welcome folks today in this blog post we will be building a c++ program to change user input array size dynamically. All the full source code of the application is shown below.

 

 

 

 

 

 

In order to get started you need to make a main.cpp file and copy paste the following code

 

 

main.cpp

 

 

#include<iostream>

using namespace std;

int main()
{
    int *array_pointer;     //Pointer to integer array
    int total_user_entries = 0;     //integer to store total item entries
    int loop_count = 0;      //counter for loop
    int temporary[50];      //temporary storage variable
    int flag = 0;

    cout << "nEnter item entries: n(Enter -1 to terminate input)n";

   while(flag == 0){

        cout << "nEnter item " << loop_count+1 << ": ";
        cin >> temporary[loop_count];                                 
        if(temporary[loop_count] == -1)
            flag = 1;

        loop_count++;

    }

    total_user_entries = loop_count - 1;


    array_pointer = new int[total_user_entries];        


    for(loop_count = 0; loop_count < total_user_entries; loop_count++){

        *(array_pointer+loop_count) = temporary[loop_count];          

    }


    cout << "n--Entered Items--";
    for(loop_count = 0; loop_count < total_user_entries; loop_count++){

        cout << "nItem " << loop_count+1 << ":  "<< *(array_pointer+loop_count);     

    }
    cout<<"nn";

    delete []array_pointer;    

    return 0;
}

Similar Posts:

    None Found

Leave a Reply

Your email address will not be published. Required fields are marked *