I would like to write function that takes the input from the user and stores it in an array. my problem is that i do not know how long the input is going to be. i read up on dynamic arrays and pointers but got very confused.

e.g. if the user enter 123456, i would like to store that in an array of integer type that has 6 elements.

i.e int myArray[6]= {1,2,3,4,5,6};
do not know how to start so please excuse my not proving a sample of what i have done.

Recommended Answers

All 8 Replies

If you are certain that you will only be getting numbers you can read a single character at a time, convert it to an integer value, then append it to a std::vector< int > .
The vector is a dynamic container that will grow to suit your needs. If you must use an array ( int * ) then you could grow the array by multiple of 2 and fill as appropriate. Consider the following pseudocode:

int * array = new int[size];
while read number
   if total_read >= size
      int * new_array = new int[size * 2]
      copy array into new_array
      array = new_array
      size *= 2
   end if
   array[total_read] = number
end while

I am new to C++ so would appretiate it if you could make the code easier by xlpain what each step does.

I would like to write function that takes the input from the user and stores it in an array. my problem is that i do not know how long the input is going to be. i read up on dynamic arrays and pointers but got very confused.

e.g. if the user enter 123456, i would like to store that in an array of integer type that has 6 elements.

i.e int myArray[6]= {1,2,3,4,5,6};
do not know how to start so please excuse my not proving a sample of what i have done.

A pointer is a memory address.
A memory address can be thought of as a street address. It's just a number signifying where a house (or in our case, where an integer) is.

int A = 10;
int* B;      //B is a pointer that can store the memory address of an integer
B = &A;  //We can get the address of A by using &.  So now B stores the address of A.
cout << B;  //This will print the memory address, NOT 10.
cout << *B;  //the * will make it "go inside" the house at that address, and print 10.

You were right to study pointers, since you cannot do something like int myArray[input_size].

To make an array using pointers, aka a dynamic array, it is simple.

int* C;  //Make a pointer.  Right now it doesn't store an address yet.
C = new int[input_size];  //The word "new" means you want a new block of memory to use...
//or, in keeping with the analogy, a new series of addresses to empty houses that you can put stuff in.
//C now points to the first address in that block (more on this later)
//So now you can put stuff in that array the same way you would a regular array.
C[0] = 10;
C[1] = 3820;
cout << C;  //this will print out the address of the first element.  You might be thinking 
//how can 1 single pointer that stores a single address store an entire array?  It 
//doesn't.  How it works is simple... since all integers are the same size... when you type 
//C[3] it knows how far "down the street" it is from the first element.  
//So it merely calculates the distance in memory to look for each item.

thanx a mil. gona try read more and make sense of it.

1 more question, so the user entered the number, how do i reference any of the idexes that wereentered. say now i want to print the numbers entered.
say the user enters 123456 and i want to print them as
1
2
3
4
5
6

Same as a regular array
cout << your_dynamic_array[0] << endl;
cout << your_dynamic_array[1] << endl;
etc

HI,,,,

#include<iostream.h>
#include<conio.h>
void main()
{int a[100],b,c,d,e;
cout<<"enter how many elements u want to enter";
cin>>b;
for(c=1;c<=b;c++)
{cin>>a[c];
}                    //this is for entering the data in an array format
clrscr();
for(d=1;d<=b;d++)
{
cout<<a[d];
}                  //displaying the output which is stored in the arrays
getch();

i hope this might help u..have a nice time....

The reason i wanted to create an array of an unkinown number is that i want to convert nunmber from any base to any given base. i know that to convert any number to any base i need to first convert the number to base 10 then to the other base. I can convert from base 10 to any base (2 to 16). i cant figure out how to convert a number to base 10. i know how to do it mathematically. i have to multiply all the digits by the increasing powers( from 0 to the number of elemennts - 1) of the base to which i am converting then add add to get the converted number. I had thought of using multi-dimensional arrays but my problem is that i have to create two arrays; 1 to store the the entered nuber as an array then the other with the powers. i will then nmultiply the numbers in the array of numbers by the elements of the array powers. now i do not know how to create a dynamic array. can u help me out. just need help with the algorithm, i would like to do the actual code myself. if the is another way a newbie to c++ could do this and understand it plese show me how.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.