|
Vaclav_Sal wrote: Just noticed (in preview) that all #include lines have only #include in it! Yes, because as has been suggested before, you should put your code blocks between <pre> tags (use the code button) like:
#define WINVER 0x0500
#include <C:\Program Files\Microsoft SDKs\Windows\v7.0A\Include\Winuser.h>
#include <stdio.h>
Vaclav_Sal wrote: AS I stated in OP - dbt.h DOES NOT define that function, only makes refrerence to it. The link to the documentation that I provided earlier, clearly states that this function is defined in winuser.h and suggests including windows.h to get it. That will also ensure that you get the correct WINVER value for the platform you are building on.
Also, judging by the fact that you have to provide the full path for C:\Program Files\Microsoft SDKs\Windows\v7.0A\Include\Winuser.h , your installation of Visual Studio has not been set up correctly, as this directoy should be one of the default include locations for all projects.
Use the best guess
|
|
|
|
|
I have a question regarding the following program. This program uses random variables and asks the user to enter the correct sum for 5 times. Until, we enter an integer the program runs well but as soon as we enter any double value or a character, the program gives incorrect message for the whole loop and comes out of the loop automatically. Why does this happen? Can anyone help me?
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
int num1=0;
int num2=0;
int correctanswer=0;
int useranswer=0;
srand(static_cast<int>(time(0)));
for (int x=1; x<6; x++)
{
num1=1+rand()%(100-1+1);
num2=1+rand()%(10-1+1);
correctanswer= num1+num2;
cout<<"What is the sum of "<<num1<<" and "<<num2<<"?"; cin>>useranswer;
if (useranswer==correctanswer)
cout<<"Excellent! Correct Answer";
else
cout<<"Sorry!the correct answer is: "<<correctanswer; cout<<endl<<endl;
}
system("pause");
return 0;
}
|
|
|
|
|
The problem is that in case of a wrong answer you leave garbage in your iostream plus the error flags of the iostream may remain set.
A slitly better but still buggy version:
#include<iostream>
#include<ctime>
#include <limits>
using namespace std;
int funccccc()
{
int num1=0;
int num2=0;
int correctanswer=0;
int useranswer=0;
srand(static_cast<int>(time(0)));
for (int x=1; x<6; x++)
{
num1=1+rand()%(100-1+1);
num2=1+rand()%(10-1+1);
correctanswer= num1+num2;
for (;;)
{
cout<<"What is the sum of "<<num1<<" and "<<num2<<"?"; cin>>useranswer;
if (!cin.fail())
break;
cin.clear();
cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
cout << "Invalid value!" << endl;
}
if (useranswer==correctanswer)
cout<<"Excellent! Correct Answer" <<endl;
else
cout<<"Sorry!the correct answer is: "<<correctanswer; cout<<endl<<endl;
}
system("pause");
return 0;
}
The bug in this version is the following:
Lets say you answer 5.6 for the first question. In this case your first cin>>useranswer statement will read out "5" into the useranswer by ignoring everything from the input stream from the '.' character. It evaluates the first question with your answer '5' and then it asks the next question. Since we still have characters in the input stream the cin>>useranswer statement of the next question tries to read it as an integer. Unfortunately it fails and our error handler clears the error flags and discards everything from the input stream till the next newline character. From the next question everything is OK. If you simply enter a garbage string that doesn't start with a digit then it works perfectly. The bug of this solution occurs if you enter a garbage string whose first character is a digit like "5sfldfhwei".
You could correct this by always reading in everything till the next newline, stripping the spaces from the left and right side of the string and trying to interpret it as it is without ignoring any characters in the string. I'm not a big iostream fan/user and I don't know how to interpret a string as an integer with iostream without ignoring any characters from the right side of the string.
|
|
|
|
|
Thank you so much pasztorpisti. Your reply was very helpful. I appreciate your help. Thanks once again
|
|
|
|
|
|
I test your process in VC6 and it running OK.
|
|
|
|
|
Hi,
Reading the below answers regarding serial ports made me wonder how safe these (below) calls would be considered. There's no need to pass anything to the constructor as in the reasons I read for using functors, and I only use them in this form because I wrote then when using STL and they're nice one liners really.
hPort = CPortOpener()(hWnd, wszPort); and
CEditPortSettings()(hWnd, hPort, wszPort); which use for instance:
class CEditPortSettings
{
public:
CEditPortSettings(void);
long operator()(HWND hWnd, HANDLE hPort, std::wstring szPort)
{
...
return anyerror;
}
|
|
|
|
|
It is exactly as safe as a normal function call with the same signature in this case. Functors are better than normal function/method calls only if you want to pass a function to a template method because using function pointers is not comfortable in an object oriented language that doesn't support delegates (delegate == function pointer that has the ability to point to the method of an object).
Regarding your constructor declaration: CEditPortSettings(void);
You have to say (void) in the parameter list of a function only in C. In C if you don't use void in case of an empty parameter list then the function is automatically vararg. In C++ the same isn't true so using void is unnecessary. I see (void) parameter list quite rarely in C++ code.
|
|
|
|
|
what does that offer over a normal function call? besides increased unreadability...
|
|
|
|
|
|
Hi Guys,
I am using GetModuleFileName API in my program. It returns me the name of my executable. However, I see that under different environments, it give me output like
-- Win XP
output: test.exe
-- Win 7
output: TEST.EXE
not sure why the API gives me different outputs. Please help.
|
|
|
|
|
Here is an excerpt from the documentation for GetModuleFileName -
The string returned will use the same format that was specified when the module was loaded. Therefore, the path can be a long or short file name, and can use the prefix "\\?\".
So the output will depend on how your EXE was loaded.
|
|
|
|
|
"So the output will depend on how your EXE was loaded."
Can you please help in elaborating it more?
|
|
|
|
|
To add more context, we are using CreateProcess(...) API to launch the exe file. However, even though the code is same but GetModuleFileName returns me different output.
|
|
|
|
|
|
|
I went though it, but able to get the reason.
|
|
|
|
|
And you still have not explained what your problem is.
Use the best guess
|
|
|
|
|
I would like to know if this particular API has any known issues.
|
|
|
|
|
Then you should read the documentation and Google for other questions.
Use the best guess
|
|
|
|
|
hello;
in a matrix of size m * n which contains only '0 'and '1', I want to calculate the sequence of two elements in each row.
example:
If the first row of the matrix is: 0,0,0,0,0,1,1,1,0,0,0,0,1,1,0,0
the first row of the matrix result should be: 5 3 4 2 2
That is to say: five zero, followed by 3 one, followed by four zeros, etc. ....
and so on for each row
|
|
|
|
|
M77ATTAT wrote: I want to... Ok, so what do you have so far (even if pseudo code on paper)?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
You need two additional variables, representing the last number seen (or the initial state) and a count of the number times you've seen this number. Reset these before processing a row.
|
|
|
|
|
yes indeed. I just found the solution as follows:
int* printArray(int *input,int max)
{
int *res;res=(int*)malloc(max*sizeof(int));for(int i = 0; i<max; i++) {res[i]=0;}
int current_kind = -1;
int count = 0;int cp=0;
for(int i = 0; i<max; ++i) {
if (current_kind!=input[i])
{
if (count!=0) res[ cp]=count;
current_kind = input[i] ;
count = 1;cp++;
} else { count++; }
}
res[cp]=count;
return res;
}
after, just use this function to calculate the matrix row by row.
thanks for suggestion !
|
|
|
|
|
I am opening a COM port in a class constructor - no problem.
I can send some data at this point too - no problem.
BUT, if I try to send data from any other method in the class it appears to work ok (I get the expected ERROR_IO_PENDING), but subsequently my code suffers an unpredictable and undebuggable crash. Any ideas anyone (please ask for more info' if needed)...
Some code;
CSerialDevice::CSerialDevice(
CString& strDeviceId,
CString& strDeviceName,
CString& COMPort,
CString& BaudRate,
CString& Format,
ComWrapper_c<CConnection15> Db, GobList_t& rGobList,
CSerialIOInterface* pInterface )
: pOurInterface( pInterface ),
m_hCommPort( NULL )
{
AddDigitalItems( strDeviceName, strDeviceId, Db, rGobList );
AddAnalogueItems( strDeviceName, strDeviceId, Db, rGobList );
if ( !ConfigureSerialPort( COMPort, BaudRate, Format ) )
{
pOurInterface->AddErrorToStatus( "Failed to configure port for " + strDeviceName );
}
}
bool CSerialDevice::ConfigureSerialPort(
CString& strCOMPort,
CString& strBaudRate,
CString& strFormat )
{
m_hCommPort = CreateFile(
strCOMPort,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0 );
DCB dcb = {0};
dcb.DCBlength = sizeof(DCB);
if ( !GetCommState( m_hCommPort, &dcb ) )
{
return false;
}
dcb.BaudRate = atoi( strBaudRate );
dcb.ByteSize = atoi( strFormat.Left( 1 ) );
int parity = NOPARITY;
if ( strFormat.Mid( 1 ) == 'N' )
{
parity = NOPARITY;
}
else if ( strFormat.Mid( 1 ) == 'E' )
{
parity = EVENPARITY;
}
else if ( strFormat.Mid( 1 ) == 'O' )
{
parity = ODDPARITY;
}
dcb.Parity = parity;
int stopBits = atoi( strFormat.Right( 1 ) );
switch ( stopBits )
{
case 0 : stopBits = ONESTOPBIT; break;
case 2 : stopBits = TWOSTOPBITS; break;
default : stopBits = ONESTOPBIT; break;
}
dcb.StopBits = stopBits;
if ( !SetCommState( m_hCommPort, &dcb ) )
{
return false;
}
COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = MAXDWORD;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.ReadTotalTimeoutConstant = 0;
timeouts.WriteTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 0;
if ( !SetCommTimeouts( m_hCommPort, &timeouts ) )
{
return false;
}
Sleep( 100 );
int loops = 40;
while ( loops-- )
{
OVERLAPPED ovlWrite = {0};
WriteFile( m_hCommPort, mTxBuf, 7, NULL, &ovlWrite ); Sleep( 100 );
}
return true;
}
void CSerialDevice::TransmitData( int bytesToSend )
{
OVERLAPPED ovlWrite = {0};
WriteFile( m_hCommPort, mTxBuf, bytesToSend, NULL, &ovlWrite );
}
|
|
|
|
|