?
Solved

CString and cin >>

Posted on 1998-11-25
4
Medium Priority
?
1,148 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 300 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

801 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