C
Cmorriskuerten
HI,
this not a really useful program, only learning, i'm a newbie, it uses
functions like: malloc, realloc strcat and I just want to know, if it is good
programming or program style:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define PUFFER_SIZE 150 /*Is this the right place for definition?*/
void inputString(void);
void outputString(void);
int newLine(void);
char *string; /*was it a good desicion to use as global var!?*/
int main(void)
{
inputString();
outputString();
return (0);
}
void inputString(void)
{
char puffer[150];
puts("Type sth.: ");
gets(puffer);
/*I know I should test if malloc == NULL, realloc also, later*/
string = malloc((strlen(puffer)+1) * sizeof(char)); /*Or should I use realloc
with NULL*/
strcpy(string, puffer);
while(newLine() == TRUE)
{
puts("Type sth.: ");
fflush(stdin); /*Is there a better way to do this!?*/
gets(puffer);
string = realloc(string, (strlen(string) + strlen(puffer)+1));
strcat(string, puffer);
}
}
void outputString(void)
{
puts(string);
fflush(stdin); /*Is there a better way to do this!?*/
getchar();
}
int newLine(void)
{
int ch;
puts("\nContinue? [y/n]: ");
fflush(stdin); /*Is there a better to do this!?*/
ch = getchar();
printf("\n");
while(ch != 'y' && ch != 'Y' && ch != 'n' && ch != 'N')
{
/*Here comes a error message*/
puts("\nContinue? [y/n]: ");
fflush(stdin); /*Is there a better to do this!?*/
ch = getchar();
printf("\n");
}
if(ch == 'y' || ch == 'Y')
return (TRUE);
else
return (FALSE);
}
Tia,
Chris
this not a really useful program, only learning, i'm a newbie, it uses
functions like: malloc, realloc strcat and I just want to know, if it is good
programming or program style:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define PUFFER_SIZE 150 /*Is this the right place for definition?*/
void inputString(void);
void outputString(void);
int newLine(void);
char *string; /*was it a good desicion to use as global var!?*/
int main(void)
{
inputString();
outputString();
return (0);
}
void inputString(void)
{
char puffer[150];
puts("Type sth.: ");
gets(puffer);
/*I know I should test if malloc == NULL, realloc also, later*/
string = malloc((strlen(puffer)+1) * sizeof(char)); /*Or should I use realloc
with NULL*/
strcpy(string, puffer);
while(newLine() == TRUE)
{
puts("Type sth.: ");
fflush(stdin); /*Is there a better way to do this!?*/
gets(puffer);
string = realloc(string, (strlen(string) + strlen(puffer)+1));
strcat(string, puffer);
}
}
void outputString(void)
{
puts(string);
fflush(stdin); /*Is there a better way to do this!?*/
getchar();
}
int newLine(void)
{
int ch;
puts("\nContinue? [y/n]: ");
fflush(stdin); /*Is there a better to do this!?*/
ch = getchar();
printf("\n");
while(ch != 'y' && ch != 'Y' && ch != 'n' && ch != 'N')
{
/*Here comes a error message*/
puts("\nContinue? [y/n]: ");
fflush(stdin); /*Is there a better to do this!?*/
ch = getchar();
printf("\n");
}
if(ch == 'y' || ch == 'Y')
return (TRUE);
else
return (FALSE);
}
Tia,
Chris