View Single Post
Join Date: Mar 2008
Posts: 356
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Re: Need help removing number from an array

 
0
  #6
Oct 21st, 2008
Yeah, I tried it a few times, and it didn't work.

Like for example:


current array...
4,23,65,34,82,37,12,17,24,36,82,51

user input: 2

34,82,37,12,17,24,36,82,51 (It deletes the first 3 numbers, but I don't want that)

instead of

4,23,34,82,37,12,17,24,36,82,51 (65 gets deleted from 2)





Here is my removeAt function:


  1. int removeAt (int numbers[], int length, int index)
  2. {
  3.  
  4. index = 0;
  5.  
  6. cout<<endl;
  7. cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl;
  8. cout<<"Enter the position of the item to be removed."<<endl;
  9. cout<<"Enter 0 for the first item and so on: ";
  10. cin>>index;
  11.  
  12. do // keeps looping until the user puts in correct information
  13. {
  14. if (index > length)
  15. {
  16. cout<<endl;
  17. cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl;
  18. cout<<endl;
  19.  
  20. cout<<"The current array..."<<endl;
  21.  
  22. for (int i = 0; i<length; i++)
  23. {
  24. cout<<numbers[i]<<" ";
  25. }
  26. cout<<endl;
  27. cout<<endl;
  28. cout<<"!!!! Index out of Range !!!!"<<endl;
  29. cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl;
  30. cout<<"You entered position "<<index<<", which is OUT OF RANGE."<<endl;
  31. cout<<"Enter the position of the item to be removed."<<endl;
  32. cout<<"Enter 0 for the first item and so on: ";
  33. cin>>index;
  34. cout<<endl;
  35. }
  36. }
  37. while(index > length);
  38.  
  39. cout<<"After removing the item at position "<<index<<", array is..."<<endl;
  40. cout<<endl;
  41. cout<<"The current array..."<<endl;
  42.  
  43. for (int i = index; i < length; i++)
  44. {
  45. numbers[i] = numbers[i+1];
  46. cout<<numbers[i]<<" ";
  47. }
  48.  
  49. cout<<endl;
  50. cout<<endl;
  51. cout<<"************************************************************";
  52. cout<<endl;
  53. cout<<endl;
  54.  
  55.  
  56. }
Reply With Quote