This is my code.i want to extend this code to ask user to enter name,address,phone and town and print it out.i did the main parts declerations etc but while using getchar i have some difficulties.what to do?
#include <stdio.h>
#include <string.h>
//
#define MAX_NAME 20 //max. name length (Including newline)
#define MAX_ADDRESS 40 //etc.
#define MAX_TOWN 40
#define MAX_PHONE 11
#define MAX_PERSON MAX_NAME+ MAX_ADDRESS+ MAX_TOWN+ MAX_PHONE
// function readin()
// complete it yourself.
int readin(char s1[], char s2[], int max)
// What should the function do:
// function readin() reads a number of characters from the keyboard.
// When the maximun of max characters is reached or when
// a newline is read from the keyboard the reading stops.
// The characters are put in the string s2.
// The string s2 should be terminated with a new line and the '\0' character
//
// The string s1 is printed on the screen, so the user knows what to do.
// Return value = number of characters read from keyboard or if
// the number of characters read is larger then max:
// the function should return -1
{
// Declarations :
int i;
printf("%-20s",s1); // put string s1 on the screen
//Extend the function here:
//Read from input and put the characters in array s2[]
for (i=0;i<max ; i++) {
s2[i]=getchar();
if (s2[i]=='\n') {
s2[i--]='\0';
break;
}
}
//Reading stops when character is '\n' (new line) or when the max numbers of characters is read.
//If the number of characters is larger then max: return the value -1, else
//return the number of characters read.
if (i == max) {
return -1;
}
return strlen(s2); //strlen: number of characters in string
}
/***************************************************************/
/* Main program */
/* This program reads personal data from the keyboard */
/* The next data is read: */
/* Name */
/* Address */
/* Town */
/* Phone number */
/* if the number of characters for the name or address exceeds */
/* the reserved length of the string the program must stop */
/***************************************************************/
int main(int argc, char* argv[])
{
char c,name[MAX_NAME+1],address[MAX_ADDRESS+1],town[MAX_TOWN+1];
char person[MAX_PERSON+1],phone[MAX_PHONE+1];
//A string is terminated with '\0' so reserve one position more
int n_name,n_address,n_town,n_phone;
//number of characters read for each item
//Reading the name:
n_name=readin("Name: ",name, MAX_NAME);
if (n_name == -1) {
printf("\nError: name too long\n");
return (0);
}
n_address=readin("Address: ", address, MAX_ADDRESS);
if (n_address == -1) {
printf("\nError: Address is too long\n");
return (0);
}
n_town=readin("Town: ", town, MAX_TOWN);
if (n_town == -1) {
printf("\nError: Town is too long\n");
return (0);
}
n_phone=readin("Phone Number: ", phone, MAX_PHONE);
if (n_phone == -1) {
printf("\nError: Phone Number is too long\n");
return (0);
}
// if the number of characters for the name is too high
//stop the program:
//stop the program
else
n_name = getchar();
while (n_name!= '\n' && n_name <20) {
putchar(n_name);
}
//Extend the code so address,town and phonenumber are also read.
//for address data use the array: address[]
//for town data use array: town[]
//for phone data use array: phone[]
//
//Put all the strings together in one string: person[] and
//print this string on the screen:
//hint: Use strcpy() and strcat()
printf("\nThe personal data:\n%s",person);
enter code here
// Print the total numbers of characters read.
printf("\nThe total number of characters read:%d\n",n_name+n_address+n_town+n_phone);
getchar();
return 0;
}