473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

please comment

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);
}
}

Nov 14 '05 #1
3 1710
hellfire wrote:
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #2
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.
Nov 14 '05 #3
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.
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: gene.ellis | last post by:
Hello everyone. Put very simply, I need to have the ability to write to open a .txt file, write to it, and then close it. I know how to do this, but the part I am not sure how to accomplish is that...
5
by: gene.ellis | last post by:
Hello everyone, I am really having a lot of trouble understanding the DOM and I doing something quite basic. I need to the DOM (in PHP 5) to modify an XML file. For example the XML basically...
1
by: MultiTaskinG | last post by:
I want to retrieve all comment stored from my web users ordered BY THREAD and BY TIMESTAMP (INT 11) with a single query (if is possible) now I launch this query: SELECT thread, timestamp,...
18
by: spiffo | last post by:
The Main Issue in a nutshell I am a corporate developer, working for a single company. Got a new project coming up and wondering if I should stay with Python for this new, fairly large project,...
2
by: Sathyaish | last post by:
I am writing a program that will take a string that has a C source file and it would format all the multi-line comments like these: //Jingle bells, jingle bells, jingles all the way //O what...
1
by: Mark Hollander | last post by:
Hi All, Could you please help me convert this code so that it will run in VB.NET Thank You Mark Hollander Private Type USER_INFO Name As String
8
by: Greg Lyles | last post by:
Hi all, I'm trying to develop an ASP.NET 2.0 website and am running into some real problems with what I thought would be a relatively simple thing to do. In a nutshell, I'm stuck on trying to...
3
by: roberthornsby | last post by:
Hi, Please can you help me with this query which I am struggling with? Here is a simplified version of the table I am trying to work with VehicleId, PurchaseId, PurchaseDate, Comment 1, 1, ...
0
by: magicofureyes | last post by:
Hello Guys im a just a new user and i dnt knw much abt Xml i want to upload a new template in Blogger so got some free coding but when i save this code in Blogger template it say '''' Your...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.