| | |
pointers problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 7
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jul 2005
Posts: 1,688
Reputation:
Solved Threads: 266
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.
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
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.
Although its not tested, its the general idea.
C++ Syntax (Toggle Plain Text)
int base= 10; int * Array = new int[base]; bool end = flase; int i = 0; while(!end) { cout <<" Enter a number : " ; int append = 0; cin >> append; Array[i] = append; i++; if( i >= base - 1) //check if I is at its end index { base += 10; int * tmp = new int[base]; //copy Array into tmp; delete [] Array; Array = new int [base]; //copy tmp into Array //delete tmp; } cout << " Continue <1 = yes, 0 = No > ; cin >> end; }
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?![]() |
Similar Threads
- Purpose of Pointers? (C++)
- C++ Object Pointers Problem (C++)
- i really hate pointers (C++)
- C++ pointers problem (C++)
Other Threads in the C++ Forum
- Previous Thread: ignore w/o using ignore function
- Next Thread: Redefined name?
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






