473,732 Members | 2,204 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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),st din);
}

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 1734
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),st din);
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
1489
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 each time I open the file to write it, I want to add the in content below the last content I just added. Example: <Comments_Section> <comment> <name>Joe</name> <comment_text>Hello</comment_text>
5
1554
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 looks like this. <news> <pageContent> <heading></heading> <author></author>
1
2425
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, level FROM comments WHERE articleid=16 ORDER BY thread, timestamp,level
18
1839
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, are jump back on the 'safe' M$ bandwagon using a dot net language? Cross platform is NOT an issue, but COMPLETE control/compatability with MsSql Server (current and future versions) certainly is. Quick History
2
1585
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 fun it is to ride in a one-horse open sledge //yeah, jingle bells.... into
1
2305
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
18084
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 display data in a "GridView" which is tied to an "ObjectDataSource". In turn, this ObjectDatasource gets it's data from a strongly-typed business object within my code.
3
1394
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, 03/03/2006, 'customer has a big nose' 1, 79, 04/04/2006, 'it's raining' 1, 8, 05/05/2006, 'man, i keep selling this vehicle' 2, 412, 02/02/2006, 'I break for lunch in 10 minutes'
0
1908
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 template could not be parsed as it is not well-formed. Please make sure all XML elements are closed properly. XML error message: The element type "Variable" must be terminated by the matching end-tag "". so please help me out and check this coding if...
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8774
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9307
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9181
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.