Using a char pointer and cin.getline()

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2009
Posts: 8
Reputation: Deme is an unknown quantity at this point 
Solved Threads: 0
Deme Deme is offline Offline
Newbie Poster

Using a char pointer and cin.getline()

 
0
  #1
May 7th, 2009
Well, what i'm trying to do is use cin.getline with my char pointer of 255 characters, take a look at the code below:

  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <conio.h>
  5.  
  6. using namespace std;
  7.  
  8. char * pChar[255];
  9. pChar = new char[255];
  10.  
  11. void newPointer()
  12. {
  13. delete pChar;
  14. pChar = new char[255];
  15. }
  16.  
  17.  
  18. int main(int argc, char *argv[])
  19. {
  20. cout << "Enter a line of text: ";
  21. cin.getline(pChar, 255);
  22. void newPointer();
  23.  
  24. cout << "Enter another line of text: ";
  25. cin.getline(pChar, 255);
  26. delete pChar;
  27.  
  28. cin.get();
  29. return EXIT_SUCCESS;
  30. }

Am I doing something wrong or what? If you want I can copy and paste the error codes I get from it, they're confusing. Thanks, I appreciate all the help I can get.
=D
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 219
Reputation: NathanOliver is an unknown quantity at this point 
Solved Threads: 37
NathanOliver's Avatar
NathanOliver NathanOliver is offline Offline
Posting Whiz in Training

Re: Using a char pointer and cin.getline()

 
0
  #2
May 7th, 2009
for this part of code
  1.  
  2. char * pChar[255];
  3. pChar = new char[255];
you do not want to declare pchar with its elements. you save that for the new operator part. so you would want to do this
  1. char * pChar;
  2. pChar = new char[255];
also when you delete an array you always want to place the array operator before the name of the array. otherwise you will just delete the head of the array but the rest of it will still be sitting there in memory taking up resources until your application exits.
make sure you alway use delete [] variable_name; also it is always a good idea to set your pointer to null after deleting it. deleting a pointer that has already been deleted is guaranteed to crash your system but deleting a null pointer is safe.
if you write using namespace std; you do not need to write std::something in your program.
If your thread is solved please mark it as solved
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: Using a char pointer and cin.getline()

 
0
  #3
May 8th, 2009
Originally Posted by Deme View Post
  1. ...
  2. int main(int argc, char *argv[])
  3. {
  4. ....
  5. void newPointer(); // that's NOT a function call!!!
  6. ...
  7. }
I marked another problem with a comment!!!
Last edited by jencas; May 8th, 2009 at 7:22 am.
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 8
Reputation: Deme is an unknown quantity at this point 
Solved Threads: 0
Deme Deme is offline Offline
Newbie Poster

Re: Using a char pointer and cin.getline()

 
0
  #4
May 8th, 2009
Okay, thank you guys for helping me out, I appreciate it much.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC