2014-09-25 67 views
-3

這是我的code.i要擴展此代碼,要求用戶輸入名稱,地址,電話和鎮並打印出來。我做了主要部分的刪除等,但使用getchar我有有些困難。該怎麼辦?使用getchar數組的困難C

#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; 
} 
+2

你要麼有一個複製粘貼錯誤在您的文章,或缺少一個全功能的頭,因爲所有的代碼下面收盤'}'回報(0)之後','的' main()'在全局範圍內,大部分不能。 – WhozCraig 2014-09-25 19:49:55

+3

「我有一些困難」。具體有什麼困難? – AnT 2014-09-25 20:21:40

+0

你應該修復縮進...此外,它似乎你的問題是*「請有人在**輸入代碼**位置」爲我編寫代碼*,這通常不是在一個SO問題,特別是如果它是一個家庭作業問題。 – hyde 2014-09-25 20:49:06

回答

0
#include <stdio.h> 
#include <string.h> 
/****************************************/ 
/* Exercise arrays and strings   */ 
/****************************************/ 
// 
#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"); 
} 

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: 
//Extend the code so address,town and phonenumber are also read. 
//for address data use the a=rray: address[] 
//for town data use array: town[] 
//for phone data use array: phone[] 

strcpy(person, name); 

strcat(person, "\n"); 
strcat(person, address); 
strcat(person, "\n"); 
strcat(person, town); 
strcat(person, "\n"); 
strcat(person, phone); 
strcat(person, "\n"); 


//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); 

// 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; 
}