pointers problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 7
Reputation: surfer2009 is an unknown quantity at this point 
Solved Threads: 0
surfer2009 surfer2009 is offline Offline
Newbie Poster

pointers problem

 
0
  #1
Oct 12th, 2009
i am doing a problem . take character dynamic of size 10.when user enter 11th element.it increment the array size to 20. when user enters 21st element array size increases to 30 and so on


int main(){
char *p;
p=new char [];
int size;


for(int i=0 ;i<10; ++)
cin>>p;
size=strlen(p);


i only manage to do this .i m a new in programming. please help to complete this code
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 266
Lerner Lerner is offline Offline
Posting Virtuoso
 
1
  #2
Oct 12th, 2009
Please learn how to use code tags when posting code to this board. It is a bit of a hassle, but given it preserves the spacing you (should be) use in writing the code it is well worth it. There are multiple locations on the board where you can learn how to do it. See the announcements list at the top of this board, click/hover over the word code at the top to of the Message box where you enter the text of your code, or even the watermark message within the Message box.

This:

p=new char [];

should be this:

int capacity = 10;
p=new char [capacity];

to declare an array of capacity 10 using dynamic memory. Remember that someplace in your code you should be releasing the memory you just requested in writing that line!

To keep track of how many elements the user has entered you should be using a counter variable.

To expand the array when the counter exceeds the capacity of the array you should be using a variable called capacity to keep track of that value, too.

To repeatedly expand the array you should probably wrap the protocol to expand the array in a function and call it when needed, passing it the array to expand and a reference to current capacity of the array.

Within the function declare a new array of current capacity. Copy contents of current array into the new array. Increase capacity by 10. Release the memory in the original array. Declare new memory for the enlarged array. Copy the memory from the new array back to the enlarged array. Release the memory from the new array. Note: it's been a while since I've done this, since I usually use STL vectors to do all of this behind the scenes so I forget if the current array should be sent to the function by reference (since the memory address of the first element of the array will change in the function) or not.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,283
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 158
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #3
Oct 12th, 2009
Use vectors if you could, if not then something like this will work :
Although its not tested, its the general idea.
  1. int base= 10;
  2. int * Array = new int[base];
  3. bool end = flase;
  4. int i = 0;
  5.  
  6. while(!end)
  7. {
  8. cout <<" Enter a number : " ;
  9. int append = 0;
  10. cin >> append;
  11. Array[i] = append;
  12. i++;
  13. if( i >= base - 1) //check if I is at its end index
  14. {
  15. base += 10;
  16. int * tmp = new int[base];
  17. //copy Array into tmp;
  18. delete [] Array;
  19. Array = new int [base];
  20. //copy tmp into Array
  21. //delete tmp;
  22. }
  23. cout << " Continue <1 = yes, 0 = No > ;
  24. cin >> end;
  25. }
  26.  
1) What word becomes shorter if you add a letter to it? 
      [ Solved by : niek_e, Paul Thompson, SgtMe]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
      [*solved by : murtan]
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC