Link to home
Start Free TrialLog in
Avatar of ptwlee888
ptwlee888

asked on

how do i use getchar in this program

i want to know how to use getchar with this  program and im not sure what else is needed in this program for it to work?
This is a program that needs to read in the first name, last name, address , city,zip code and state  and printing them out after reading them i need to assure each array has a limit of characters but i dont know how to do it

im also having problems with my structure , i dont know how to relate it with pointer pointing to it

#include<stdio.h>
#include<string.h>

#define  last 25
#define first 30
#define phone 15
#define add 75
#define CITY 50
#define ZIP 11
#define STATE 3

struct customer {
   char lastName[last];
   char firstName[first];
   int customerNumber;
   char phoneNumber[phone];
   char address[add];
   char city[CITY];
   char state[STATE];
   char zipCode[ZIP];
}my_s;

typedef struct customer Customerinfo;



int main()

{  
int i;  
 int Inputsize;
   int *inputpointer;
        inputpointer = (int*)malloc(Inputsize*sizeof(int));
    char st;
   
    st=&my_s.lastName;
   
    printf("Please enter the ammount of customers you would like to input:");
    scanf("%d\n", Inputsize);
   
   
  for(i=0; i<=Inputsize;i++)
   
   {  printf(" Enter last name of customer please: ");
      scanf("%c\n",&my_s.firstName);
     
     
          printf("Enter first name of customer please: ");
       getchar(lastName);
             
       
                 printf("Enter the address of the customer:");
                 getchar(address);
                 
                printf("Enter the phone number for this customer:");
                 scanf("%d", phoneNumber);
                      printf("Enter city ");
                      getchar(city);
                     
                         printf("Enter State:");
                          getchar(state);
                         
                       printf("Enter zipcode");
                          getchar(zipcode);
                         
               
               }
               
    for(i=0;i<= Inputsize;i ++)
    {
      printf(" Customer Record\n Name:%s", my_s.firstName);
      printf("%s\n",my_s.lastName);
      printf("Customer number:%d\n",my_s.customerNumber);
      printf("Phone number :%s\n", my_s.phoneNumber);
      printf(" address:%s\n", my_s.address);
      ptrinf("%s",my_s.city);
      printf("%s", my_s.state);
      printf("%s", my_s.zipCode);
     
      }
     
       free(inputpointer);
      return 0;
     
      }      
Avatar of imladris
imladris
Flag of Canada image

The getchar function reads a single character from stdin. That doesn't seem like what you would reasonably want in those places. The gets, or its safer cousin fgets would seem far more appropriate:


          printf("Enter first name of customer please: ");
          fgets(my_s.firstName,first,stdin);

             
Avatar of ptwlee888
ptwlee888

ASKER

thanks imaladris ill try it out
have a quick question here

would this set up of mine be able to read a customer information and write it out , and then read another one again ? does these for loops i nested here make sense?
if not how do you think i should change it
There are many things wrong with this code:

Inputsize is never set to a value.
Inputpointer is allocated (to some indeterminate size) but never used.
You prompt for last name, but read the input (a single character) into first name.
After the first input, you fail to read answers into the appropriate slot of the my_s structure.
Most people enter telephone numbers with embedded parenthesis, whitespace, and hyphens,
yet your scanf attempts to parse that as an integral value.
ptrinf() wants to be printf().
st is defined as a char, but you are assigning to it a pointer to char. (Doesn't really matter since
it is never used.)
As imladris points out, you probably want to use fgets() rather than scanf() or getchar().

Inputsize appears to want to be the number of customer records you wish to read and write,
however you [attempt to] store all values for each customer into a single structure and
definitely print out the contents of that single structure.  If functioning properly the code
would print out the data of the last customer entered for each iteration of the output loop.
It looks like Inputpointer wants to be allocated as an array of customer structs rather than
array of ints.  

Although not strictly syntax or logic errors, the following are C language conventions:
By convention, macros defining constants are all caps.
By convention, the first letter of variable names is lower case.


fgets(.....) doesnt work , i cant input a string of characters into it , it just skips it and goes to the other printf


this is what i have right now and it doesnt read through all the arrays i want it to read thru
can someone help me figure out fgets?


#include<stdio.h>


#define  LAST 25
#define FIRST 30
#define PHONE 15
#define ADD 75
#define CITY 50
#define ZIP 11
#define STATE 3

struct customer {
   char lastname[LAST];
   char firstname[FIRST];
   int customernumber;
   char phonenumber[PHONE];
   char address[ADD];
   char city[CITY];
   char state[STATE];
   char zipcode[ZIP];
}my_s;

typedef struct customer Customerinfo;



int main()

{  Customerinfo* st;
   int i;  
   int Inputsize;
   int *inputpointer;
        inputpointer = (int*)malloc(Inputsize*sizeof(int));
 
    printf("Please enter the ammount of customers you would like to input:");
    scanf("%d\n", &Inputsize);
   
   
  for(i=0; i<Inputsize;i++)
   
   {  
   fflush(stdin);
   printf(" Enter last name of customer please:\n ");
      fgets(my_s.lastname,LAST,stdin);
   
     printf("Enter first name of customer please:\n");
         fgets(my_s.firstname,FIRST,stdin);
         
              printf("Enter customer number:\n");
             scanf("%d", my_s.customernumber);
                 
                 printf("Enter the address of the customer:\n");
                 fgets(my_s.address,ADD,stdin);
                 
                     
                     printf("Enter customer phone number:\n");
                   fgets(my_s.phonenumber,PHONE,stdin);
                     
                     
                   printf("Enter city:\n");
                   fgets(my_s.city,CITY,stdin);
                     
                       
                         printf("Enter State:\n");
                         fgets(my_s.state,STATE,stdin);
                         
                             
                          printf("Enter zipcode\n");
                         fgets(my_s.zipcode,ZIP,stdin);
                         
               
               
       printf(" Customer Record\n Name:%s", my_s.firstname);
      printf("%s\n",my_s.lastname);
      printf("Customer number:%d\n",my_s.customernumber);
      printf("Phone number :%s\n", my_s.phonenumber);
      printf(" address:%s\n", my_s.address);
      printf("%s",my_s.city);
      printf("%s", my_s.state);
      printf("%s", my_s.zipcode);
     
   }
     
       free(inputpointer);
      return 0;
     
      }        
   
   
                           
               
     
   
       
       
       
       
     
> fgets(.....) doesnt work , i cant input a string of characters into it , it just skips it and goes to the other printf

Then you are doing something wrong.  Here is an example of use.  Note the documentation,
Error checking, input parameter validation, avoidance of buffer overruns and general robustness.
This is an example of professional programming.

/*
   NAME
        ask4it

   SYNOPSIS
        #include <stdio.h>
        #include <string.h>
        #include <errno.h>

        int error = ask4it(const char *prompt, char *replybuff, int replysize)

   DESCRIPTION
        Prompt the user on stdout for information. Collect the user's
        reply from stdin and return the reply in the supplied buffer.
        Reading of the reply stops when a newline character is found,
        at end-of-file or error.  The newline, if any, is discarded.
        At most, replysize-1 bytes will be returned in replybuff.
        A `\0' character is appended to end the reply string.
        If an error occurs or end-of-file is reached without reading
        any data, replybuff will contain a 0-length string.

   RETURN VALUES
        If successful, 0 is returned.
        If end-of-file on the input stream was reached, EOF is returned.
        If an error occurred, a value consistent with errno will be returned.

        A zero-length string will be returned in replybuff if an error
        occurred, end-of-file was reached on input without reading data,
        or the user simply hit return with no other input.

   ERRORS
        [EOF]           End of file on input
        [EINVAL]        Invalid replybuff or replysize values passed
        Any errno that may be set by an error in fgets().

   BUGS & SIDE EFFECTS
        An internal buffer limits the maximum length of the reply to 511 bytes.
*/
int ask4it(const char * prompt, char *replybuff, int replysize)
{
  char buff[512];

  if ((replybuff == NULL) || (replysize <= 0))
    return EINVAL;

  /* issue prompt to the user */
  printf("%s: ", prompt);

  /* get reply string from stdin */
  if (fgets(buff, sizeof(buff), stdin)) {

    /* strip off the trailing NL included by fgets() */
    int len = strlen(buff);
    if (buff[len-1] == '\n')
      buff[len-1] = '\0';

    /* copy the answer to the supplied buffer avoiding buffer overflow */
    strncpy(replybuff, buff, replysize-1);
    replybuff[replysize-1] = '\0';

  } else {
    /* return empty buffer on error or EOF */
    *replybuff = '\0';
  }

  if (feof(stdin))
    return EOF;
  if (ferror(stdin))
    return errno;
  return 0;
}

ASKER CERTIFIED SOLUTION
Avatar of imladris
imladris
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial