![]() |
| | #1 |
| Registered User Join Date: Nov 2001
Posts: 3
| Code:
void Menu()
{
char* phone;
char* name;
GetPhone(&phone);
GetName(&name);
// use the following printf function to test my code.
// check bottom of msg for output and what my problem.
printf("%d - %s - %d - %s", &name, name, &phone, phone);
}
void GetName(char** nameRef)
{
int ch;
char buffer[100]; /* buffer used to get input from the user.
if the user enters in 105 characters for name
the buffer will reset to 5
*/
printf("(Name must be between 3-20 characters in length)\nEnter a name to be added to the list: ");
fgets(buffer, sizeof(buffer), stdin); // gets input from stdin and puts in buffer
while ((ch = getchar()) != '\n' && ch != EOF);
*nameRef = buffer;
//printf("%d - %s", &nameRef, *nameRef);
if((strlen(*nameRef)-1 < 3) || (strlen(*nameRef)-1 > 20))
{
printf("Name has to be between 3 and 20 characters long.\nPress [Enter] key to be prompted again.");
GetName(nameRef);
}
}
void GetPhone(char** phoneRef)
{
int ch;
char buffer[50];
printf("Enter the phone number to be added to the list: ");
fgets(buffer, sizeof(buffer), stdin);
//while((ch = getchar()) != '\n' && ch != EOF);
*phoneRef = buffer;
printf("%d - %s", &phoneRef, *phoneRef);
if(strlen(*phoneRef)-1 != 9)
{
printf("The phone number you entered has too many digits.\nMust be in either xxxx xxxx or xxxx-xxxx formats.\nReplace x with numbers.");
GetPhone(phoneRef);
}
if(!((buffer[4] == ' ') || (buffer[4] == '-')))
{
printf("Must be in xxxx xxxx or xxxx-xxxx formats. Replace x with numbers.");
GetPhone(phoneRef);
}
if(validate_phone(*phoneRef) != 0)
{
printf("The phone number you have entered contains non-numeric data. Try again.");
GetPhone(phoneRef);
}
}
int validate_phone(char* phoneRef)
{
unsigned x;
for (x = 0; x < strlen(phoneRef); x++)
{
if(phoneRef[x] == phoneRef[4])
break;
if(!isdigit(phoneRef[x])) return 1;
}
return 0;
}
601364 - paul - 601368 - 5074-0658 problem is when i put a printf statement in the GetPhone and GetName functions and notice that the reference address of the name pointer is the same as the one for phone reference. problem is whenever i try and get the string entered in GetName i get weird ASCII characters. What i'm try to do is create a simple phone book using a link list structure to hold the data, as a exercise to try and understand pointers. Thx in advance catalyst |
| catalyst is offline | |
| | #2 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,628
| Code: void GetName(char** nameRef)
{
int ch;
char buffer[100];
Either make your buffer static, or use dynamic memory allocation (malloc or calloc). If you use *alloc functions, you will need to use 'free' on them before your program exits. Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| what is wrong in this simple code | vikingcarioca | C Programming | 4 | 04-23-2009 07:10 AM |
| what is wrong with this code please | korbitz | Windows Programming | 3 | 03-05-2004 10:11 AM |
| I cant find what is wrong with this code | senegene | C Programming | 1 | 11-12-2002 06:32 PM |
| Anyone see what is wrong with this code? | Wise1 | C Programming | 2 | 02-13-2002 02:01 PM |
| very simple code, please check to see whats wrong | Unregistered | C Programming | 3 | 10-10-2001 12:51 AM |