|
jon_80 wrote: char* passwordEntry(int allowedSize)
{
const int size = 100;
if (allowedSize > size)
{
cout << "Password size is too large. Size limit is " << size << endl;
allowedSize = size;
}
char password[size];
cout << "Enter your password:";
cin.getline(password,size);
if (strlen(password)>allowedSize)
{throw password;}
return password;
}
You return the address of a temporary variable. The scope of password is limited to the function, so when you return it, the memory is not protected anymore.
|
|
|
|
|
I think that I am returning a copy of the variable to a char*, is this the problem?
Jon
|
|
|
|
|
You return the address of the array of char (don't forget that char* is a pointer). This array is created locally in the function (so the memory is reserved in the function). When you reach the end of the function, the array comes out of scope (because its scope is limited to the function). Thus the memory will be freed automaticaly. The contents may still be the same but they are not protected anymore (meaning that anything can override your array). And of course, the pointer that you return still point at the same location (not protected).
You have to take care of the warnings generated by your compiler. Normally, this should have appeared in the warnings ('warning: returning address of temporary variable', or something like that).
|
|
|
|
|
Yes you're right I was overlooking the warning, thanks for the explanation.
Jon
|
|
|
|
|
You really should start using the std::string class. Many of your problems will go away quickly by doing so (or even by using the MFC CString class).
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
|
|
|
|
|
Just a recommendation:
#include <iostream>
#include <string>
#include <exception>
using namespace std;
string passwordEntry(int allowedSize)
{
const int SIZE = 100;
if (allowedSize > SIZE)
{
cout << "Password size is too large. Size limit is " << SIZE << endl;
allowedSize = SIZE;
}
string password = "";
char buffer[SIZE] = {0};
cout << "Enter your password:";
cin.getline(buffer, size);
password = buffer;
if (password.length() > allowedSize)
{
throw exception("Invalid password size!");
}
return password;
}
void main ()
{
const int LEGAL_PASSWD_SIZE = 8;
string password = "";
try
{
password = passwordEntry(LEGAL_PASSWD_SIZE);
}
catch (const exception& e)
{
cout << e.what() << endl;
password = "*********";
}
cout << "Password is stored as " << password.c_str() << endl;
}
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
|
|
|
|
|
Can anyone tell me how to make a Power ON/OFF button on a VC++ application that controls a radio set AR5000 through its serial interface. The command 'QP' on procomm can turn the radio set OFF and a CR LF will turn it ON.
|
|
|
|
|
|
In my application I have created a propertypage.In that property page I have displayed three edit boxes.
For example in that edit boxes the user will enter name,sex,age.
Taking those three values as column names I have to create a table in the database.
while creating my project I have chosen Database view with file support.
I used ODBC to connect to a datasource.
Can u plz help me.
Thanks in advance.
|
|
|
|
|
Hi,
it seems you have posted the question quite back sometime.
ok. you can use CRecordSet like classes to create tables. before that you may need to connect to the data base using CDatabase class. you can see some samples in MSDN itself.
IMHO, It's better not to attach the database with some controls. because on every change it will update the database. it will slowup the entire application if your application has enough size and others are accessing the database.
you can take the values at the time of update. that would be better.
This is not a solution for your problem but I was trying to explain a good practice.
-Sarath.
The more you can dream the more you can do - Michael Korda"
|
|
|
|
|
Sometimes this [^]would help you
-Sarath.
The more you can dream the more you can do - Michael Korda"
|
|
|
|
|
No need to repost. See here.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Thank you very much for all of you
|
|
|
|
|
Hi,
Can any one help me to write postscript roundedREct function?
Also please write the logic behind the RoundedREct() API in VC++.
Actually i want a dra a rounded rectangle and i have Rectangle parameter and the Horizontal & vertical Radius.
Please help out.
Thanks
Prabbash
|
|
|
|
|
I have not time to do some code. you can do it by Arcs and Lines.
|
|
|
|
|
I would like to control a LCD character display using C++. How is this best done? Is there code available for this kind of communication?
The display will be connected to parallel port, serial port or USB port.
Thanks!
|
|
|
|
|
xkrja wrote: Is there code available for this kind of communication
Lots of. What you need first is the description of your LCD device. Google for API or your display name and C++, you'll find a lib that wraps the serial port for talking with it.
~RaGE();
I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus
|
|
|
|
|
Thanks for your answer. I've searched for hours after libraries but haven't found any. Are there really libs for such small displays (for example 16x2 characters)?
Thanks again!
|
|
|
|
|
You have to find the documentation for the display. Then you can write a program that uses the Serial or Parallel Port API's to send signals that the display understands.
Your best bet is to try an amateur robotics website like http://www.seattlerobotics.org/[^]
--------------------------------
"All that is necessary for the forces of evil to win in the world is for enough good men to do nothing" -- Edmund Burke
|
|
|
|
|
Most such displays are serial devices, and are easily programmed. As such there is no real need for a library.
The pixel based devices are often somewhat harder to program, but not by much. For those you'll have to render text yourself (preferably to a back buffer which you transfer to the display - in full or partial updates if speed is of concern).
--
A Stern Warning of Things to Come
|
|
|
|
|
Ok, thanks for your answer!
|
|
|
|
|
With Windows98 I could set the paper size and everything was ok. After I install WindowsXP my settings are ignored.
This is the code that works on Windows98:
.....
CPrintDialog *printDlg =new CPrintDialog(FALSE, PD_ALLPAGES | PD_RETURNDC, NULL);
// Initialize some of the fields in PRINTDLG structure.
printDlg->m_pd.nMinPage = printDlg->m_pd.nMaxPage = 1;
printDlg->m_pd.nFromPage = printDlg->m_pd.nToPage = 1;
// Display Windows print dialog box.
if (printDlg->DoModal() != IDOK)
return;
LPDEVMODE lp = (LPDEVMODE) ::GlobalLock(printDlg->m_pd.hDevMode);
ASSERT(lp);
lp->dmPaperSize = 256;
lp->dmPaperLength = 3050;
lp->dmPaperWidth = 2970;
::GlobalUnlock(printDlg->m_pd.hDevMode);
HDC hdc = printDlg->CreatePrinterDC();
CDC *pDC = new CDC;
pDC->Attach (hdc); // attach a printer DC
pDC->StartDoc("test");
......
|
|
|
|
|
Hi,
I wonder if anybody knows which is the control used by the Microsoft Visual Studio .NET in the properties window. I mean the one you can group by categories (and toggle the category expansion) or alphabetically.
Thanks in advance.
|
|
|
|
|
i want to create a dialog base application.
and i put one button in a dialog . i create one more dialog and in this i want to add icons with text as one control(and i want to work it as button)how can i do it?.
then in by clicking the button in main dialog this dialog will be displayed as menu..just as attaching to button i want to diaplay it form down to up.
|
|
|
|
|
kiranin wrote: Display Dialog as Menu
Why don't you create a pop up menu that has icons and text?
That would be much more easier and efficiently done.You can show the menu when you click on the button.
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|