please comment

H

hellfire

Below is a pretty usless progam but being a begginer
and working form a book i need some one to comment
on it IE: is it set out right easy to follow i tried
to use every thing i know to data your comments will
be appricated thanks for your time

/* VB1.C PROGRAM JUST TO MAKE DESISIONS */
#include<stdio.h>

/* DECLARE VARIABLES */
char line[3];
char sex;
char choice;

/* DECLARE FUNCTION PROTOTYPES */
void get_line(void);
void get_sex(void);
void get_choice(void);
int main(void)

{
printf("Enter m for male f for female: ");
get_sex();

if(sex == 'm')
{
printf("are you a master y for yes n for no; ");
get_choice();

if(choice == 'y')
printf("you are a Master:\n");
else
printf("You are a Mr:\n");
}

else if(sex == 'f')
{
printf("are you a miss y for yes n for no: ");
get_choice();

if(choice == 'y')
printf("You are a Miss:\n");
else
printf("You are a Mrs:\n");
}
return 0;
}
/* FUNCTION DEFINITIONS */
void get_line(void)
{
fgets(line, sizeof(line),stdin);
}

void get_sex(void)
{
get_line();
sscanf(line, "%c", &sex);

while(sex != 'm' && sex != 'f' )
{
printf("%c Not a valid entery try again:",sex);
get_line();
sscanf(line, "%c", &sex);
}
}

void get_choice(void)
{
get_line();
sscanf(line, "%c", &choice);

while(choice != 'y' && choice != 'n' )
{
printf("%c Not a valid entery try again:",choice);
get_line();
sscanf(line, "%c", &choice);
}
}
 
K

Kevin Goodsell

hellfire said:
Below is a pretty usless progam but being a begginer
and working form a book i need some one to comment
on it IE: is it set out right easy to follow i tried
to use every thing i know to data your comments will
be appricated thanks for your time

Please try to use proper punctuation when posting here. If we can't
understand what you are saying, we can't help. Also, if you aren't
willing to put forth the effort to make your messages coherent, many
people will not be willing to put forth the effort to try to figure out
what you are saying, and will simply skip your message. Keep in mind
that this is a technical discussion forum, not a social forum. It's one
of the more formal corners of the Internet.
/* VB1.C PROGRAM JUST TO MAKE DESISIONS */
#include<stdio.h>

/* DECLARE VARIABLES */
char line[3];
char sex;
char choice;

Global variables are best avoided. These in particular seem to be wholly
unnecessary.
/* DECLARE FUNCTION PROTOTYPES */
void get_line(void);
void get_sex(void);
void get_choice(void);

I would expect a function with a name beginning with the word "get" to
return something. For example:

char *get_line(void);
char get_sex(void);
char get_choice(void);
int main(void)

{
printf("Enter m for male f for female: ");

Output in C is often line-buffered. That means that the I/O system will
collect characters in a buffer until the end of a line, and at that
point will send the characters to their next destination. That means
that your prompt may not be seen right away. To fix this, either add a
newline:

printf("Enter m for male f for female:\n");

or flush the output:

printf("Enter m for male f for female: ");
fflush(stdout);
get_sex();

The organization here is a little strange. I would expect get_sex() to
take on the entire task of determining the sex of the subject. How it
does this is not important to the rest of the program. It just so
happens that it reads it from stdin, but it could just as easily read it
from a file, an environment variable, or choose it randomly. So, main()
should probably not be printing the prompt - that should be a job for
get_sex(). main() doesn't know or care how get_sex() is going to
determine the sex, and shouldn't make any assumptions about it.

Also, you need to get the sex out of get_sex() somehow. You already know
this, and have been using a global variable for it. Trust me when I say
this is a bad idea. Return the value instead.

char sex; /* Add at the beginning of main */
/* ... */
sex = get_sex();
if(sex == 'm')

Some people would suggest writing this the other way around:

if ('m' == sex)

in order to avoid a common bug: using '=' instead of '=='.
{
printf("are you a master y for yes n for no; ");
get_choice();

Same comments as for sex above (flush the output, probably move the
prompt into get_choice(), return the value).
if(choice == 'y')
printf("you are a Master:\n");
else
printf("You are a Mr:\n");
}

else if(sex == 'f')
{
printf("are you a miss y for yes n for no: ");
get_choice();

And again.
if(choice == 'y')
printf("You are a Miss:\n");
else
printf("You are a Mrs:\n");
}
return 0;
}
/* FUNCTION DEFINITIONS */
void get_line(void)
{
fgets(line, sizeof(line),stdin);

What if the user enters a longer line? What if end-of-file is encountered?

When you aren't using global variables anymore, getting a line out of a
function can be tricky. Some options are to pass in a pointer to an
array in which to store the line, to return a pointer to a local static
array, or to return a pointer to a dynamic array:

void get_line(char buf[], size_t len)
{
fgets(buf, len, stdin);
}

char *get_line(void)
{
static char buf[100];
fgets(buf, sizeof(buf), stdin);
return buf;
}

char *get_line(void)
{
char *p = malloc(100);
if (p != NULL)
{
fgets(p, 100, stdin);
}
return p;
}

These all omit error checking and recovery. Each has advantages and
disadvantages. The first is probably preferable in most cases. The
caller is responsible for freeing the dynamic memory in the third case.

-Kevin
 
S

stau

Generally speaking your program is correct but baddly designed, even for
a short program like this. Try reading some good programming books.
Sorry for not commenting your code, but I'm trying to get to bed,
maybe tomorrow.

ciao.
 
H

hellfire

Thanks for your comments i will look into them
but some of the things you have said are above
my head at the momment. Thanks for your help
i will try out your suggestions ans see how they
work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,125
Messages
2,570,748
Members
47,302
Latest member
MitziWragg

Latest Threads

Top