I am trying to build an address book program. It has two options so far, it reads the addresses and prints them. they are accessible through a menu in the main function. Everything works fine but for the address I need more than 1 word, separated by space, so I tried using the getchar()
function. The problem is that the compiler simply skips it. I am sure it's not the compiler, since I've tried several different ones. all resulting with the same problem. The problem-causing getchar()
is in the InputContact
function.
Any ideas/suggestions are highly appreciated.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define M 41 // Number of Chars for Firs-Last Name
#define N 101 // Number of Chars for Address
#define C 7 // Number of Chars for Postal Code
#define P 11 // Number of chars to be held in Phone Number
#define MAX 10 //maximum number of records
struct contact
{
char firstname[M];
char lastname[M];
char address[N];
char postalcode[C];
char phone[P];
};
typedef struct contact Contact;
int InputContact(Contact * , int);
void PrintContact(Contact [MAX] , int);
void SaveContact(const Contact [MAX] , const char*);
int OpenContact(Contact*, const char *);
void SearchContact(Contact[MAX] , char[M] , int);
int main ()
{
Contact ContactList[MAX];
int size = 0;
int ans;
char c;
c = getchar();
printf("%c" , c);
printf (">");
scanf("%d" , &ans);
while (1)
{
switch (ans)
{
case 1: size = InputContact(ContactList , size);
break;
case 2: PrintContact(ContactList , size);
break;
default: return 0;
}
printf ("\n>");
scanf("%d" , &ans);
}
}
/*
Inputs the Contact Data from the Keyboard
Takes in the contact list by reference. and populates it with input from the keyboard.
Returns Void
*/
int InputContact(Contact *ptrCon , int size)
{
char temp; // flag
char ans[2];
int i = size; //counter
int j = 0;
ans[0]='y';
printf("%d" , i);
char c;
int k = 0;
while (( i < MAX )&&(ans[0]=='y'))
{
printf("%s" , ans);
printf("\nAdding a new Contact:\n");
printf("First Name:");
scanf("%s" , ptrCon[i].firstname);
printf("Last Name: ");
scanf("%s" , ptrCon[i].lastname);
printf("Address: ");
// gets the address (FOR SOME REASON IT WILL NOT WORK WITHIN THIS PROGRAM, works fine if i test this code as an independent program or in the main function.
// the getchar() function is just skipped by the compiler.
while ( ((c = getchar()) != '\n' ) && k < N) // checks that the char is not newline and that the string is still smaller than the max size
{
ptrCon[i].address[k++] = c;
}
ptrCon[i].address[k] = '\0'; // puts the null terminator
//scanf("%s" , ptrCon[i].address);
printf("Postal Code: ");
scanf("%s" , ptrCon[i].postalcode);
printf("Phone Number: ");
scanf("%s" , ptrCon[i].phone);
printf("Add another contact?\n ");
scanf("%s",&ans);
i++;
}
return i;
}
/*
Prints the Contact List
Takes in the EmployeeRecord unsing pass by value
Returns Void
*/
void PrintContact(Contact ContactList[MAX] , int size)
{
if (size == 0)
{
printf("contact list is empty");
}
else
{
printf("LASTNAME FIRSTNAME ADDRESS POSTAL CODE PHONE NUMBER\n");
for (int i = 0; i < size;i++)
{
printf("%s\t%s\t%s\t%s\t%s\n" , ContactList[i].firstname , ContactList[i].lastname , ContactList[i].address , ContactList[i].postalcode , ContactList[i].phone );
}
}//end else
}