|
1) I wish to generate an error when i write:
unsigned int i;
cin >> i;
//the user inputs '2.5' ( a double, incompetible type)
or '-1' ( a signed int, incompetible type)
I used:
if ( !cin || cin.fail( ) )
{
cin.ignore(256, '\n');
cin.clear( );
cin.sync( );
//cout >> someError;
}
It never got inside this block. WHY?
2) /* STL string */ string s;
cin >> s; // The user inputs: "I hate C++ projects"
cout << s; // Output: "i"
Same Q: WHY?
and how do i ( without using another buffer )?
|
|
|
|
|
1. C++ will implicitly convert your number to the appropriate type in the stream operation. I would initially pull the number in as a string, parse it for correctness and then store it the actual variable.
RegEx can help you with this: http://www.boost.org/libs/regex/index.htm or you could write your own state machine to parse it. (But I'd avoid this unless you want the experience.)
2. This is working properly. The stream functions work by looking for whitespace as separators.
I'd use getline instead. But you will need another buffer. getline does not use std::string.
const int MAX = 200;
char buffer[MAX];
cin.getline(buffer, MAX);
string s = buffer;
|
|
|
|
|
Sorry, screwed up on #2.
You can do getline with a std::string.
const int MAX = 255;
string s;
s.resize(MAX);
cin.getline(&s[0], MAX);
cout << s << endl;
|
|
|
|
|
Hi,
I like to know how can I get a pointer to a particular Document.
I can iterate through my open document and get a pointer to anyone of them.
but at the same time I like to get a pointer to the view of that document as well.
the following code is how to get a pointer to a particular document.
CMyDoc* CMyView::GetDocumentIndex(int n)
{
int i=0;
int nDocCount=0;
CWinApp* pApp = AfxGetApp();
CMyDoc *pCurrDoc;
POSITION posTemplate = pApp->GetFirstDocTemplatePosition();
while (posTemplate)
{
CDocTemplate *pDocTemplate=pApp->GetNextDocTemplate(posTemplate);
if(NULL!=pDocTemplate)
{
POSITION posDoc = pDocTemplate->GetFirstDocPosition();
while(posDoc)
{
CDocument *pDoc = pDocTemplate->GetNextDoc(posDoc);
if(NULL!=pDoc)
nDocCount++;
if(nDocCount == n)
{
pCurrDoc =(CMyDoc*)pDoc;
//this is where I have to get a pointer to the View of pCurrDoc,,,but how?
}
}
}
i++;
}
return pCurrDoc;
}
Ehsan Behboudi
|
|
|
|
|
I like to know how can I get a pointer to a particular Document
Are you sure? The rest of your post describes problems with getting pointers to views. If this is the case, use CDocument::GetFirstViewPosition and GetNextView.
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|
Do I have to get position of the first CView,
then iterate through the rest fo the Views??
Ehsan Behboudi
|
|
|
|
|
|
I used this, but it doesn't work,, any ideas???
CMyView CMyDoc::GetView(int n)
{
int ViewCount=0;
POSITION pos = GetFirstViewPosition();
while (pos)
{
CMyView *ppView=GetNextView(pos);
if(NULL!=ppView)
{
POSITION posView = GetFirstViewPosition();
while(posView)
{
CMyView *pView = GetNextView(posDoc);
if(NULL!=pView)
ViewCount++;
if(nDocCount == n)
pCurrView =(CMyView*)pView;
}
}
}
return pCurrView;
}
Ehsan Behboudi
|
|
|
|
|
|
Hi,
I like to know, how can I open a blank document from my CView class.
I mean actually write the code for ID_FILE_NEW.
I tried the following code, but it doesn't work.
CWinApp* pApp = AfxGetApp();
pApp->OpenDocumentFile(NULL);
thanks
Ehsan Behboudi
|
|
|
|
|
Could anyone tell me if there's an easy way to convert date/time from CString to CTime?
I would, for example, like to convert the string "2001-07-26 14:37:00" to a CTime object. I could do this using string operations to parse out the year, month, etc.. This, however, does not feel like a good solution since it's not very flexible. I would have to include several if-statements in the parsing to be able to handle dates on other formats, like "2001-07-26", "2001-07-26T14:37", etc.
What would make me very happy is if there's a way to use formatting string to specify the format of the input. Like when I convert a CTime object to a CString using Format() with formatting codes like %Y, %m, etc.
Maybe somebody has written a class, derived from CTime, that could handle this?
Jimmy Ehrnström
|
|
|
|
|
You can use COleDateTime::ParseDateTime( string ) to convert the string into the underlying DATE object.
If you don't like to use COleDateTime objects and need the CTime object, you would have to convert between the two. Create a tm structure and populate the structure from the COleDateTime object and then using mktime( tm ) assign the result to the CTime object.
|
|
|
|
|
|
hi,
I'm building a web browser, and I need to provide Java Console Option as IE provides, How could I do it...?
Gaurav Lambole
webdunia.com (India) pvt.ltd.
|
|
|
|
|
Hello,
How can I place the entire contents of a RichEditView into a variable? I can do it if the user selects the text, but I would like to do it unselected.
Also, is there a limit to the size of a string stored in a CString variable?
Thanks!
Frank
|
|
|
|
|
I guess you could use GetRichEditCtrl to get the control associated with the view and then GetWindowText.
/Per
|
|
|
|
|
Thanks, I'll give that a try.
Frank
|
|
|
|
|
How can i link DB grid to DAO database ?
My month article: Game programming by DirectX by Lan Mader.
Please visit in: www.geocities.com/hadi_rezaie/index.html
Hadi Rezaie
|
|
|
|
|
Hi,
how to make a program startup screen where an image is displayed.
Please help me quick.
Thank You
|
|
|
|
|
From VC++ menu select Project/Add to project/Components and controls/VC++ components/Splash screen
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|
In the MSDN example program Wordpad there is a splash window. Maybe that will help you.
/Per
|
|
|
|
|
In VC++, is there any way to override the global "new" operator in a DLL and have other DLLs (which link together with this one) and the main program
(which links with all the project's DLLs) use this operator? If there is a way, I would appreciate seeing how it is done.
My attempts so far have produced the following conclusion:
If I add an overloaded "new" operator definition with an additional parameter,
e.g.
__declspec(dllimport) void * operator new(size_t , unsigned long);
There is no error, but as soon as I include the prototype definition of my "new" operator which contains only one argument:
__declspec(dllimport) void * operator new(size_t);
I get the following error:
error C2375: 'new' : redefinition; different linkage
C:\TEMP\a00238if(73) : see declaration of 'new'
If there is no way to do this in compile time, but there is a way in run-time, I would appreciate seeing how that is done, as well.
Thanks,
Jacob.
|
|
|
|
|
The simplest solution would be to implement real allocator as exported function living inside your DLL -- and -- provide overloaded inline operator new that simply calls that function. Operator new would be defined in .h file that all DLLs and EXE would #include.
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|
Hi,
first of all, thank you very much for your consideration.
Please help me! I want to get a word on the screen (eg. on a label, on a button...) by clicking right mouse on it.
I guess that I must do some hook to API functions or window's messages, but I don't know how to do it.
Please show me the way or send me an example.
Once more thank you very much.
trungnq.
|
|
|
|
|
Override the OnContextMenu or MouseDown and draw your text.
|
|
|
|