Solved

CString and cin >>

Posted on 1998-11-25
4
1,145 Views
Last Modified: 2013-11-20
  I have a MFC console app that takes input from stdin via cin >> input; input is a char * and has been initialised in a constructor(input = "dummy";). Later in the app, I try to build a search string as shown below:
 
        CString str;
      CString pre = "'";
      CString post = "'";
      CString field = "PASSWORD = ";

//select record for this user_no

      pre += password;
      pre += post;
      str = field + pre;
      the_rec.m_strFilter = str;

The problem is that the CStrings are not receiving their expected values. e.g field does not show "PASSWORD = ", it is a null string. Others have various combinations of the input char *.
   When I comment out cin >> input; and simply assign a value to input, everything works OK. Is there some restriction on using stdin with CStrings or is there anything else which might cause this?
      
0
Comment
Question by:mspx
  • 2
4 Comments
 
LVL 8

Expert Comment

by:Answers2000
ID: 1325328
CString has a constructor for const char * (LPCSTR) not for char *, that's why

if you want >> to work for CString, overload >>

istream& operator>> ( istream& is, CString& str )
{
   // <-- insert code for unpack whatever input into a CString
   return is;
}

A good way to get a char * buffer to the CString data is to use LockString (member of CString) giving you an LPSTR (char *), then UnlockString when you're done

Finally when you see LPCTSTR that's LPCSTR in 8 bit char set (ANSI) - i.e. const char * - it takes into acccount wide characters when compiling for UNICODE.  Similarly LPTSTR and LPSTR and char *.

0
 

Author Comment

by:mspx
ID: 1325329
  I'm not sure if I have made the problem clear. cin >> input is inserting data into a char *, not a CString. I have used a char * so that I can use the C string functions which meet my needs better than the CString string functions in this case. As I said, when i comment out cin >> input and use input = "aaaaaaetc.", the CStrings later in the program are assigned correctly. It appears as though merely using cin >> input somehow affects the way unrelated objects are assigned(CStrings in this case).
The only use for input(a char *) in the context of the CStrings concerned is that it is used to assign a value to password(also a char *).

The overloaded operator function you propose takes a CString & reference as one argument. As I said, the argument needs to be a char * so I can use C functions.

Am I missing the point?
0
 

Author Comment

by:mspx
ID: 1325330
see comment above
0
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 100 total points
ID: 1325331
if you have the following code:

  char* input = "hello";
  cin >> input;

this is invalid!

Instead you will need to do the following:

  char[MAX_PATH] input;
  strcpy(input, "Hello");

  cin >> input;

The reason is that in the first case, input has been initialized to a pointer to some area of memory that should not be overwritten. However, when the code cin >> input; is encountered, the library is trying to put the characters entered into that space which will definitely write over something else in that area. This causes your program to behavior erratically because its behavior is undefined.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
centeredAverage challenge 8 186
modThree challenge 4 110
MaxSpan challenge 9 124
scoreUp challenge 14 69
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question