|
Ah, you are correct. I completely forgot about the general getline function ...
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Beside what Zac said, another suspicious area is
cin>>MyText;
When you expected MyText is one char, your input could easily be more than one char, which would cause a memory problem.
Best,
Jun
|
|
|
|
|
Hello Jun.
Actually my input field is char* , and I was expecting a text rather than just one char. Besides, this is nothing more than an experimental project which turned out to be disastrous!!;P
Regards
|
|
|
|
|
Try this instead.
#include <iostream>
#include <iomanip>
int main(int argc, char *argv[])
{
using namespace std;
char *pChar = new char[2];
cin >> setw(2) >> pChar;
delete [] pChar;
return 0;
}
You're reading too many charcters: you've got room for a NULL terminator (a string of zero length!) but your reading more and getting a buffer overrun. My code tells the IO classes how big the buffer is so it will not overrun it.
If you only want to read a single char you could try doing this (in your code):
cin >> *MyText;
Steve
|
|
|
|
|
Thanks Steve! I appreciate your response.
Regards
|
|
|
|
|
In the following program the control code seems to be returning to the start of void::main() after the last line of code has been executed:
void main ()
{
CSentenceList LoadFile("c:/testfile.txt", true);
for (int iIndex = 1; iIndex <= LoadFile.Size; iIndex++)
{cout << LoadFile.display(iIndex) << endl;}
cout << LoadFile.search("john") << endl;
}
Presumably after a number of attempts the program crashes.
crt0.c
...
if ( !managedapp ) // return value is 0 (false)
exit(mainret); // this is where I think the program should exit
...
Any idea why?
Jon
|
|
|
|
|
Have you debugged the code? I would first check if any iIndex value causes "out of range" in your LoadFile.display() method.
Best,
Jun
|
|
|
|
|
Actually that was the problem thanks.
Jon
|
|
|
|
|
probably won't fix the crash, but main should be declared as:
int main(int argc, char *argv[])
or
int main(int argc, char **argv)
(note: this is strictly for style reasons)
-- modified at 15:17 Thursday 31st August, 2006
|
|
|
|
|
|
|
Chris Losinger wrote: probably won't fix the crash, but main should be declared as:
int main(int argc, char *argv[])
or
int main(int argc, char **argv)
Microsoft's compiler allows you to define main in any of the following ways (all completely acceptable):
int main()
int main(int argc, char** argv)
int main(int argc, char** argv, char** env)
void main()
void main(int argc, char** argv)
void main(int argc, char** argv, char** env)
While the 3-parameter version and the void versions are not portable, they are fine for Win32 Console programming.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Zac Howland wrote: Microsoft's compiler allows you to define main ...
oh, i know. and like i said, it probably wouldn't help fix the crash. just thought i'd point out a style issue.
|
|
|
|
|
Chris Losinger wrote: just thought i'd point out a style issue.
No problem. Of course, his entire problem goes away if the suggested changes for the new standard are approved with regard to string , where the following code would actually work:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input;
cin >> input;
}
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Hi, i want to create an SDI application without Frame And Menu bars.
And in this SDI application ,i split the window into 3 views.
How can i close all the views at a time by clicking one button in the top view?
|
|
|
|
|
Hello
I have written an application which is using the msflexigrid. The problem I am seeing is with a fast PC. Intel 3GHz P4 using Hyperthreading.
The problem is the cells do not always update. Below is a sample of code which updates a cell depending of the outcome of the function.
if(!(CTESTINTERFACE::SetupTxVcoTop()))
{
if(!(CTESTINTERFACE::SetupTxVcoTop()))
{
m_vco_parameter_grid.SetRow(2);
m_vco_parameter_grid.SetCol(3);
m_vco_parameter_grid.SetCellForeColor(RGB(255,0,0));
m_vco_parameter_grid.SetText("Failed");
UpdateWindow();
if(OnTestFailed("TxVcoTop") == FALSE)
return;
}
else
{
m_vco_parameter_grid.SetRow(2);
m_vco_parameter_grid.SetCol(3);
m_vco_parameter_grid.SetCellForeColor(RGB(0,255,0));
m_vco_parameter_grid.SetText("Passed");
UpdateWindow();
CMEASUREMENT::strPASS_FAIL.ialign_pass_count++;
}
}
else
{
m_vco_parameter_grid.SetRow(2);
m_vco_parameter_grid.SetCol(3);
m_vco_parameter_grid.SetCellForeColor(RGB(0,255,0));
m_vco_parameter_grid.SetText("Passed");
UpdateWindow();
CMEASUREMENT::strPASS_FAIL.ialign_pass_count++;
}
I have tried replacing UpdateWindow() with Refresh(),RedrawWindow(),Invalidate(), and calls directly to OnPaint();
The problem is randam. Some times it runs flowlessly.
Any idears would be appreciated.
Regards Desmond Mardle
|
|
|
|
|
I have a program that reads text input form users and echo it in a textbox, similiar to chat. When I compile it using debug configuration everything works fine and great, but when I switched to release configuration the program crashes and generates an application error after the 1st line of text is echoed to screen. I have some TRACE statement in the code but as far as I know the TRACE statement does nothing in release configuration.
Is there anything else that I should check for might be the cause of this? I know the release configuration does some optimization but I'm not sure why it works in debug configuration but not release?
Any suggestion is welcome!
|
|
|
|
|
|
Hi All
I'm newbe to multithreading. Readings info on MSDN for MFC classes it seems that doesn't exist a function, like the pThread libraries, to wait that a Thread started with AfxBeginThread to its return. Do you know if there is some existing function instead of waiting time using Sleep( n )?
I would like to use an MFC function instead of the Win32 ones.
Also pThread libs got a proper function to retrieve the result of a thread; MFC use the pointer provided as argunet when the thread is started. Is this the correct approach?
Thanks everybody
bye Manu
|
|
|
|
|
::WaitForSingleObject is the method you are looking for. MFC didn't wrap this method (mainly because there was no need to), so you have to use the Win32 API here.
The last question you had ... are you talking about the return value of the thread or what is returned from AfxBeginThread?
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Hi Zac,
thanks for your reply!
I mean the return value of the thread; the value of the function returned with 'return' at the end of its execution. When we start the thread we pass a LPVOID as argument of the function to execute in the thread.
Can I use this argument retrieve this value from the calling thread? In pThread there is a function dedicated.
Manu
|
|
|
|
|
The LPVOID argument allows you to pass in any data you might want to use within the thread. Do NOT use it to pass back a return value. Use ::GetExitCodeThread to get the return value for the thread.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Zac,
this means that I need to pass the hanlde returned by WinThread to WaitForSingleObject?
I read in MSDN that user should be careful when mixing MFC threading with the WIN32 one.
Manu
|
|
|
|
|
manustone wrote: I read in MSDN that user should be careful when mixing MFC threading with the WIN32 one.
MFC IS Win32. It is nothing but a thin wrapper around the Win32 API. What you have to be careful of is mixing _beginthread(ex) and CreateThread. Typically, you want to use one or the other for an entire application. WaitForSingleObject takes a handle parameter. You will pass in whatever thread handle you have and it will return when either the timeout value you specify has expired (in which case the return value is WAIT_TIMEOUT) or the object has been signaled (for threads, that means exited/returned/stopped), in which case the return value is WAIT_OBJECT_0.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
If you mean pthread_create() function returns an int for checking, AfxBeginThread() returns a CWinThread* pointer for checking. They are pretty much equivalent.
Could you be more specific about "waiting time using Sleep(n)"? Don't know what you meant.
Best,
Jun
|
|
|
|