【问题标题】:difficulties using getchar for arrays C将 getchar 用于数组 C 的困难
【发布时间】:2014-09-25 19:46:23
【问题描述】:

这是我的代码。我想扩展此代码以要求用户输入姓名、地址、电话和城镇并将其打印出来。我做了主要部分 declerations 等,但在使用 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;
}

【问题讨论】:

  • 您的帖子中存在剪切粘贴错误,或者缺少整个函数头,因为在 main() 中的 return(0); 之后关闭 } 之后的所有代码都在全局范围内范围,而且大部分都不能。
  • “我有一些困难”。具体有哪些困难?
  • 您应该修复缩进...另外,您的问题似乎是“请有人在在此处输入代码位置为我编写代码” ,这在 SO 问题中通常不好,特别是如果它是家庭作业问题。

标签: c getchar strcat


【解决方案1】:
#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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 1970-01-01
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多