c - 对数组 C 使用 getchar 的困难

标签 c getchar strcat

这是我的代码。我想扩展此代码以要求用户输入姓名、地址、电话和城镇并将其打印出来。我做了主要部分的声明等,但在使用 getchar 时我遇到了一些困难。该怎么办?

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

最佳答案

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

关于c - 对数组 C 使用 getchar 的困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26046866/

相关文章:

c - 适合中等简单 TCP 服务器的良好架构

c - 如何将测试用例添加到 ltp

c - 为什么我们使用 scanf() 却需要 getchar()?

C++ getchar() 是否还有数据等待读取

c - 一项任务要求我用 2 个 scanf 收集一个名字

c - 从产生奇怪输出的字符串中删除字符

c - 如何将 'ls' 命令转换为 'cat' 命令?

c++ - mmap如何分配超过20Gb?

c - C中重定向后获取文件内容

c - fwrite 和 strcat 截断字符串并写入无意义的值