>Well, with the password that isn't a big deal, but that name in the program is really a filepath, so any ideas on that?
Matt,
Leave them as pointers, but allocate the memory for them.
Code sample
// Initialize required memory variables
int q ;
char* name = (char *)malloc(200) ;
char ch ;
char* password = (char *)malloc(20) ;
bool breturn = false ;
// Get values from the screen input
if (password != NULL )
{
puts("Password!");
}
printf("Password:");
if (name != NULL )
{
printf("Name:");
scanf("%s",name);
for (q=0;q<200 && !breturn;q++)
{
ch = getch();
if (ch =='\r')
{
breturn=true;
}
else
{
password[q] = ch;
}
}
}
cout << password << endl ;
cout << breturn << endl ;
free(password) ;
free(name) ;
}
malloc() allocates memory for a pointer to a memory space. By preallocating the memory, you control where things are stored in memory. This is both a strength and a weakness of C++ in that you have control over where you put things, but the downside is you pretty much have to control them. If you allocate memory, you must also free() it. If you neglect to free() the memory you've allocated, the system will allocate new memory spaces each time you hit the malloc() call. The result is a memory leak that if you were to go back and run the program over and over would eventually cause the heapstack to overload, and the system to crash.
I tested this and it ran without error. The prompts wheren't quite what you were after, but I'm sure you can work that little glitch out.
Good luck, hope this helped.
Regards,
Jason Tryon
Jason Tryon
Senior Systems Analyst / Technical Lead
eBusiness / iPage