| | |
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:
Solved Threads: 0
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:
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
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> #include <fstream> #include <conio.h> using namespace std; char * pChar[255]; pChar = new char[255]; void newPointer() { delete pChar; pChar = new char[255]; } int main(int argc, char *argv[]) { cout << "Enter a line of text: "; cin.getline(pChar, 255); void newPointer(); cout << "Enter another line of text: "; cin.getline(pChar, 255); delete pChar; cin.get(); return EXIT_SUCCESS; }
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
for this part of code
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
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
c++ Syntax (Toggle Plain Text)
char * pChar[255]; pChar = new char[255];
c++ Syntax (Toggle Plain Text)
char * pChar; pChar = new char[255];
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
If your thread is solved please mark it as solved
using namespace std; you do not need to write std::something in your program.If your thread is solved please mark it as solved
•
•
Join Date: Dec 2007
Posts: 360
Reputation:
Solved Threads: 69
•
•
•
•
C++ Syntax (Toggle Plain Text)
... int main(int argc, char *argv[]) { .... void newPointer(); // that's NOT a function call!!! ... }
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
Please use code tags - Please mark solved threads as solved
![]() |
Similar Threads
- Pointer assignment question (C++)
- How to extract words from char type string (C++)
- Creating a Basic String Database (C++)
- C++ Stream_Var.getline function (C++)
- Function with pointer? (C++)
Other Threads in the C++ Forum
- Previous Thread: can't build wxwidgets in cygwin
- Next Thread: saving file as .doc w/ formatting, page orientation
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop directshow dll download dynamic encryption error file forms fstream function functions game getline 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 node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





