|
write it yourself and then ask here if you have specific problems
|
|
|
|
|
Member 12968703 wrote: its say error expected '(' before 'else'..
The error message already told what is the problem: just read it and check the code that it points out, in this case the else statement: that particular line is incorrect and needs fixing.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
scanf("%p" , &password);
if(password=12345); {
printf("Your password is correct");
}
if else(password!=12345);
|
|
|
|
|
Also remove the semicolons following the right parens.
|
|
|
|
|
|
I mean the right parens on the lines with ifs.
The one on this line :
if(password=12345);
Will cause the printf to always execute.
|
|
|
|
|
I know, hence my , indicating my stupidity.
|
|
|
|
|
#include <stdio.h>
int main()
{
float number = 123456;
int pass;
printf("Please enter your password:\n");
scanf("%d",&pass);
if (pass==number)
{
printf("Your password is correct.\n");
}
else
{
printf("Your password is incorrect.\n");
}
}
i already write the program but i dont know how to make a prompted to re enter a password if i enter a wrong password..i really dont know..and i hope u can help me..
|
|
|
|
|
You do it be creating a loop, using a do , while or for statement. In pseudo code something like:
Set a retry count
While retry is not zero
Check password
If password is correct
Then break
Else
retry = retry - 1
If retry is zero
Then display error message
End While
See if you can turn that into C code.
|
|
|
|
|
Member 12968703 wrote: if (pass==number) Comparing an int to a float will rarely, if ever, work.
Member 12968703 wrote: but i dont know how to make a prompted to re enter a password if i enter a wrong password..i really dont know..and i hope u can help me.. To continue the prompting until the correct password is entered, you'll need to use a loop. Something like:
do
{
...
} while (password != 12345);
"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
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
if else(password!=12345);
You need to learn better the if-else structure.
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|
write a program to find voltage when enter current and resistor value using the following formula:voltage=current x resistor
#include<stdio.h>
float main()
{
float current,resistor,voltage;
printf("Enter the current value:");
scanf("%f",¤t);
printf("Enter the resistor value:");
scanf("%f",&resistor);
voltage = current*resistor;
{
printf("Total voltage=%2f V");
}
return 0;
}
i try this but i cant find the correct output..
please help me..i am a student..
|
|
|
|
|
You forgot to pass the voltage argument to printf :
printf("Total voltage=%f V\n", voltage);
Note also that the return value of main() is int by definition and can not be float .
|
|
|
|
|
That, and there's some weird line, although it may just be a case of messed up copy/pasting:
scanf("%f",¤t);
should be:
scanf("%f",¤t);
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|
Hello all & Thanks for reading this,
-I would have a question on you:
-I have created a small application on MFC/C++ that draw different geometric shapes on screen using the mouse (like Paint). These shapes can be moved and rotated,
so, I had to keep all these created shapes in a vector and draw all of them in OnDraw().
-My problem is, when I create many shapes (more than 50-60) and I try to move/rotate one of the shapes, these movements/rotations become very-very slow.
-Could you give me some tips in order to make my application faster when moving/rotating?
OnDraw(CDC* pDC)
{
for (int i = 0; i < numberObjects; i++)
objects[i]->DrawShape(pDC);
}
Many thanks!
|
|
|
|
|
You are basically building a version of windows within windows
Buffering won't really help you that much, the problem is you are redrawing 50 shapes when you move one shape (look at your code you do 1..numberObjects) and most of the time you don't need to redraw them. What you need to do is calculate what shapes overlap where your shape to move is now, and what shapes overlap the move shape in it's new position. Then you only need to draw those limited shapes. Much of the time you only needed to redraw probably 1-3 shapes but you are drawing everything.
You can then get even smarter like windows and do only redraw partial areas. Do you see the next step from above in those cases you do have to redraw from above often you don't have to redraw the entire thing just a limited area. So you are minimizing the redrawing.
If you aren't going to have thousands I can give a quick hack which is to put each shape on it's own child window. That child window has frames off and have no background color and simply draws that shape just like your code does on the one parent window. Do you get what will happen the window is transparent except the area you draw on, in the old days we called them sprites. To move the shape just issue a window MoveWindow command to that child window handle and it will move and do minimum overlapping window redraws for you for free ..... because that is what windows does with a window move
I will give you the other thing about that trick the selection of the shapes is a lot easier probably than you current code because windows will sort all the overlapping for you.
That all said the often better way to do this is to simply XOR draw a rectangle representing the object and move just the XOR box with the mouse. When you get to where you want it you do the release and only then redraw everything. I assume you know the trick with XOR drawing if not
Combining GDI and GDI+ to Draw Rubber Band Rectangles[^]
In vino veritas
modified 26-Jan-17 0:42am.
|
|
|
|
|
Nice tips @Leon, many thanks for your help!
|
|
|
|
|
No worries and since you came back can I offer you a piece of code that can very quickly reject all the trivial shape areas that do not require drawing when moving in your current code.
You need to set the "dragging" flag to TRUE when you are moving a shape and set it back to FALSE when done dragging. I don't have the structures so you need to feed in x1,y1,x2,y2 into the two /* xxxxx */. So for each Area x1,y1 needs to be top left corner, x2,y2 lower right corner of the on screen minimum box around the shape. It puts an extra very fast test on redrawing a shape if the flag is set and I will leave you to work out what it does but it is commented
BOOL CanAreasOverlap (int Area1_x1, int Area1_y1, int Area1_x2, int Area1_y2, int Area2_x1, int Area2_y1, int Area2_x2, int Area2_y2){
if (Area1_x1 > Area2_x2) return (FALSE);
if (Area1_x2 < Area2_x1) return (FALSE);
if (Area1_y1 > Area2_y2) return (FALSE);
if (Area1_y2 < Area2_y1) return (FALSE);
return (TRUE);
}
BOOL dragging = FALSE;
OnDraw(CDC* pDC){
for (int i = 0; i < numberObjects; i++){
if ((!dragging) || CanAreasOverlap(, )) {
objects[i]->DrawShape(pDC);
}
}
}
When you drag a shape and go to redraw
In vino veritas
|
|
|
|
|
If constructor is throwing any execpetion after allocating some memory (using new) on heap then how can we free that allocated memory.
|
|
|
|
|
Ideally you avoid any activity that would throw an exception in the constructor. Instead of using new on the constructor. Mark the pointers as nullptr and then have a separate init function.
But you might try
new (std::nothrow)
which will eat the exception and instead return nullptr.
or you can do something like.
try {
ptr = new char[100];
} catch(std::bad_alloc& e) {
}
But overall, is a bad idea to throw on a constructor.
|
|
|
|
|
Move the memory allocation to a class of its own so that the new happens in its constructor and delete happens in its destructor. That should be the only responsibility of the allocating class.
In the class whos constructor throws, create an object of the allocating class on the stack.
Since destructors are only called for objects that are fully constructed, the allocating class destructor is always guaranteed to be called.
«_Superman_»
I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) (October 2009 - September 2013) Polymorphism in C
|
|
|
|
|
|
I need to send a string or char to a "POS Receipt Printer" via USB.
My problem is how to establish a communication with the printer by USB port in C++ (I'm using MFC)
I tried this:
HANDLE dev = CreateFile(
"\\\\.\\USB001",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(dev!=INVALID_HANDLE_VALUE)
{
DWORD dwWritten = 0;
LPCSTR lpszTest = "Hello world!";
::WriteFile(dev, lpszTest, strlen(lpszTest), &dwWritten, NULL);
::CloseHandle(dev);
}
but, it returns by GetLastError() a value = 2 (Can not find the specified file)
Also, I tried this:
HANDLE dev;
CString s("\\\\.\\USB001");
LPWSTR p=(LPWSTR)(LPCTSTR)s;
if(OpenPort(p,(PHANDLE)&dev))
{
CString str = "Hello world!";
LPBYTE pByte = new BYTE[str.GetLength()+1];
memcpy(pByte,(VOID*)LPCTSTR(str),str.GetLength());
LPDWORD r;
r=0;
WritePort(dev,pByte,6,r);
delete [] pByte;
}
But I got frustrated,
due that OpenPort and WritePort belongs to #include <winsplp.h> and <winspool.h> , so it generates a lot of dependencies (libs,dlls) that escapes my knowledge.
If anyone can help me, I will be really grateful.
Yany
|
|
|
|
|
To open a handle to a USB device, you have to use the SetupDixxx APIs to enumerate and find the device.
Here is an example for enumerating USB devices - http://www.velleman.eu/images/tmp/usbfind.c[^]
Once the correct VID/PID pair is found, you can use the DevIntfDetailData->DevicePath in a call to CreateFile .
«_Superman_»
I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) (October 2009 - September 2013) Polymorphism in C
|
|
|
|