Hello,
This routine accepts six alphanumeric characters, e.g. A1B 2C3.
and is part of a mail program.
This is what I want to do? Input in lowercase: a1b 2c3, press <Enter>.
Output results expected uppercase: A1B 2C3.
This routine does the job. However when I implement it in my
mail program, the results are in lowercase, why is that; and
how do I resolve this problem?
Also, can you tell me if I'm using malloc() correctly?
I'm new to malloc(), and have read, if not used correctly
will only give the size of a pointer(ie. 4 bytes); and
as such will only hold four structures in the list? What is
the best method for holding the lists?
I'm only posting the relevant parts.
Thank you.
/*************************************************************/
/***************************************************************************************/
This routine accepts six alphanumeric characters, e.g. A1B 2C3.
and is part of a mail program.
This is what I want to do? Input in lowercase: a1b 2c3, press <Enter>.
Output results expected uppercase: A1B 2C3.
This routine does the job. However when I implement it in my
mail program, the results are in lowercase, why is that; and
how do I resolve this problem?
Also, can you tell me if I'm using malloc() correctly?
I'm new to malloc(), and have read, if not used correctly
will only give the size of a pointer(ie. 4 bytes); and
as such will only hold four structures in the list? What is
the best method for holding the lists?
I'm only posting the relevant parts.
Thank you.
Code:
/*Test program*/
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int getzip(char zipcode[])/*Prints ZIPcode in Uppercase.*/
{
int i, bad_zip;
char *p, buffer[8];
do
{
bad_zip = 0;
printf("Enter ZIPcode:");
fflush(stdout);
if(fgets(buffer, sizeof(buffer), stdin) != NULL)
{
if((p = strchr(buffer, '\n'))!= NULL)
*p = '\0';
}
for(i = 0; buffer[i]; i++)
{
buffer[i] = toupper(buffer[i]);
}
printf("%s", buffer); /*debugging.*/
return 0;
bad_zip = 1;
} while(bad_zip);
}
int main(void)
{
char zipcode[8];
getzip(zipcode);
return 0;
}
Code:
/*Main program*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <windows.h>
/*Global structure of name and address.*/
struct mail_struct
{
char name[25];
char address[25];
char city[25];
char province[30];
char zipcode[8];
char phone[18];
char email[25];
}list;
/*Global file pointer.*/
FILE *fp;
/*Defined constants.*/
#define MAX 250
#define FILENAME " MAIL.DAT"
/*Prototype all of the program's functions.*/
void disp_menu(void);
int enter_na(void);
int change_na(void);
int print_na(void);
void add_to_file(struct mail_struct item);
void clrscr(void);
void pause_sc(void);
void flush_newline(char *line);
int pr_data(struct mail_struct item);
void get_new_item(struct mail_struct *item);
int getzip(char zipcode[]);
/********************************************************************/
int main(void)
{
int ch;
struct mail_struct *list[MAX];
malloc(sizeof(list));
if(list == NULL)
{
perror("Unable to allocate memory");
exit(EXIT_FAILURE);
}
do
{
disp_menu();
if(scanf(" %d", &ch)!= 1)
{
perror("Call to scanf failed");
exit(EXIT_FAILURE);
}
else
{
while(getchar() != '\n');
}
switch(ch)
{
case (1) : {enter_na();
break;}
case (2) : {change_na();
break;}
case (3) : {print_na();
break;}
case (4) : {break;}
default :{ perror("*** You need to enter 1 through 4***");
exit(1);}
}
}
while (ch != 4);
free(list);
return 0;
}
/***********************************************************************************/
int enter_na(void)
{
clrscr();
struct mail_struct item;
int ch;
do
{
printf("\nEnter Name: ");
if(fgets(item.name, sizeof(item.name), stdin) != NULL)
{
flush_newline(item.name);
}
printf("\nEnter Address: ");
if(fgets(item.address, sizeof(item.address), stdin) != NULL)
{
flush_newline(item.address);
}
printf("\nEnter City: ");
if(fgets(item.city, sizeof(item.city), stdin) != NULL)
{
flush_newline(item.city);
}
printf("\nEnter Province:");
if(fgets(item.province, sizeof(item.province), stdin) != NULL)
{
flush_newline(item.province);
}
getzip(item.zipcode); /*Call to zipcode function.*/
printf("\nEnter Phone Number:");
if(fgets(item.phone, sizeof(item.phone), stdin)!= NULL)
{
flush_newline(item.phone);
}
printf("\nEnter Email:");
if(fgets(item.email, sizeof(item.email), stdin)!= NULL)
{
flush_newline(item.email);
}
add_to_file(item); /*Write new information to disk file*/
printf("\n\nDo you want to enter another name");
printf(" and address? (Y/N) ");
ch = getchar();
getchar(); /*Discard carriage return.*/
}
while (toupper(ch) == 'Y');
return 0;
}
/**********************************************************************************/
int getzip(char zipcode[]) /*Does not print in uppercase.*/
{
int i, bad_zip;
char *p, buffer[8];
do
{
bad_zip = 0;
printf("\nEnter ZIPcode:");
if(fgets(buffer, sizeof(buffer), stdin) != NULL)
{
if((p = strchr(buffer, '\n'))!= NULL)
*p = '\0';
}
for(i = 0; buffer[i]; i++)
{
buffer[i] = toupper(buffer[i]);
}
return 0;
bad_zip = 1;
} while(bad_zip);
return 0;
}