Hi all! Newbie here.
Below is an example from Teach Yourself C in 21 Days. My apologies
if it is a bit long.
What I don't understand is how the "get_data" function can call the
"continue_function", and if NO is returned to "get_data", display_report
executes and the program ends? Basically I am having trouble understanding
the program flow within the "if" loop in the "main" function.
The "if" loops only initiates if cont=YES, so what happens if a function
in the loop(in this case get_data) gets cont=NO? Does this really mean
that if "something" within an "if" loop doesn't meet the condition of the
loop,
the result is that the next statement below the "something" gets executed
and the the "if" loop is exited?
Hope I made myself clear.
Thanks in advance!
================================================== =============================
#include <stdio.h>
#define MAX 100
#define YES 1
#define NO 0
/*--------------------*/
/* variables */
/*--------------------*/
long income[MAX]; /* to hold incomes */
int month[MAX], day[MAX], year[MAX]; /* to hold birthdays */
int x, y, ctr; /* For counters */
int cont; /* For program control */
long month_total, grand_total; /* For totals */
/*--------------------*/
/* function prototypes*/
/*--------------------*/
int main(void);
int display_instructions(void);
void get_data(void);
void display_report(void);
int continue_function(void);
/*--------------------*/
/* start of program */
/*--------------------*/
int main(void)
{
cont = display_instructions();
if ( cont == YES ) /* <================================ LOOK HERE!
*/
{
get_data();
display_report();
}
else
printf( "\nProgram Aborted by User!\n\n");
return 0;
}
/*-----------------------------------------------------------*
* Function: display_instructions() *
* Purpose: This function displays information on how to *
* use this program and asks the user to enter 0 *
* to quit, or 1 to continue. *
* Returns: NO - if user enters 0 *
* YES - if user enters any number other than 0 *
*-----------------------------------------------------------*/
int display_instructions( void )
{
printf("\n\n");
printf("\nThis program enables you to enter up to 99 people\'s ");
printf("\nincomes and birthdays. It then prints the incomes by");
printf("\nmonth along with the overall income and overall average.");
printf("\n");
cont = continue_function();
return( cont );
}
/*-------------------------------------------------------------*
* Function: get_data() *
* Purpose: This function gets the data from the user. It *
* continues to get data until either 100 people are *
* entered, or until the user enters 0 for the month.*
* Returns: nothing *
* Notes: This allows 0/0/0 to be entered for birthdays in *
* case the user is unsure. It also allows for 31 *
* days in each month. *
*-------------------------------------------------------------*/
void get_data(void)
{
for ( cont = YES, ctr = 0; ctr < MAX && cont == YES; ctr++ )
{
printf("\nEnter information for Person %d.", ctr+1 );
printf("\n\tEnter Birthday:");
do
{
printf("\n\tMonth (0 - 12): ");
scanf("%d", &month[ctr]);
}while (month[ctr] < 0 || month[ctr] > 12 );
do
{
printf("\n\tDay (0 - 31): ");
scanf("%d", &day[ctr]);
}while ( day[ctr] < 0 || day[ctr] > 31 );
do
{
printf("\n\tYear (0 - 2002): ");
scanf("%d", &year[ctr]);
}while ( year[ctr] < 0 || year[ctr] > 2002 );
printf("\nEnter Yearly Income (whole dollars): ");
scanf("%ld", &income[ctr]);
cont = continue_function(); /*
<===================================== LOOK HERE! */
}
/* ctr equals the number of people that were entered. */
}
/*----------------------------------------------------------*
* Function: display_report() *
* Purpose: This function displays a report to the screen *
* Returns: nothing *
* Notes: More information could be printed. *
*----------------------------------------------------------*/
void display_report()
{
grand_total = 0;
printf("\n\n\n"); /* skip a few lines */
printf("\n SALARY SUMMARY");
printf("\n ==============");
for( x = 0; x <= 12; x++ ) /* for each month, including 0*/
{
month_total = 0;
for( y = 0; y < ctr; y++ )
{
if( month[y] == x )
month_total += income[y];
}
printf("\nTotal for month %d is %ld", x, month_total);
grand_total += month_total;
}
printf("\n\nReport totals:");
printf("\nTotal Income is %ld", grand_total);
printf("\nAverage Income is %ld", grand_total/ctr );
printf("\n\n* * * End of Report * * *\n");
}
/*----------------------------------------------------------------*
* Function: continue_function() *
* Purpose: This function asks the user if they wish to continue.*
* Returns: YES - if user wishes to continue *
* NO - if user wishes to quit *
*----------------------------------------------------------------*/
int continue_function( void )
{
printf("\n\nDo you wish to continue? (0=NO/1=YES): ");
scanf( "%d", &x );
while( x < 0 || x > 1 )
{
printf("\n%d is invalid!", x);
printf("\nPlease enter 0 to Quit or 1 to Continue: ");
scanf("%d", &x);
}
if(x == 0)
return(NO);
else
return(YES);
}
================================================== ================================ 7 2038
Buck Rogers said: Hi all! Newbie here.
People have been posting using the name "Buck Rogers" for around four years.
I sincerely hope all those articles are not from you, because if you've
taken four years to get this far in your studies, it might be time to call
it a day. If you're a fresh-faced learner who is just starting out, though,
then forget I said anything (but possibly pick a name that is more likely
to be unique!).
Below is an example from Teach Yourself C in 21 Days. My apologies if it is a bit long.
It is also badly-written, so - if you *must* use that book - be prepared to
unlearn some bad habits and some misconceptions later on.
What I don't understand is how the "get_data" function can call the "continue_function", and if NO is returned to "get_data", display_report executes and the program ends? Basically I am having trouble understanding the program flow within the "if" loop in the "main" function.
"if" is not a loop construct. It's selection, not iteration.
The "if" loops only initiates if cont=YES, so what happens if a function in the loop(in this case get_data) gets cont=NO?
On receiving a return value of NO from continue_function(), get_data()
terminates its loop on the next test, i.e. just about immediately, and
exits. Control now returns to the caller, within main(). The statement just
processed was get_data(), so control now flows to the next statement along,
which is display_report(). Therefore, display_report() is executed.
Does this really mean that if "something" within an "if" loop doesn't meet the condition of the loop, the result is that the next statement below the "something" gets executed and the the "if" loop is exited?
Hope I made myself clear.
Thanks in advance!
================================================== ============================= #include <stdio.h>
#define MAX 100 #define YES 1 #define NO 0
/*--------------------*/ /* variables */ /*--------------------*/
long income[MAX]; /* to hold incomes */ int month[MAX], day[MAX], year[MAX]; /* to hold birthdays */ int x, y, ctr; /* For counters */ int cont; /* For program control */ long month_total, grand_total; /* For totals */
/*--------------------*/ /* function prototypes*/ /*--------------------*/
int main(void); int display_instructions(void); void get_data(void); void display_report(void); int continue_function(void);
/*--------------------*/ /* start of program */ /*--------------------*/
int main(void) { cont = display_instructions();
if ( cont == YES ) /* <================================ LOOK HERE! */ { get_data(); display_report(); } else printf( "\nProgram Aborted by User!\n\n");
return 0; } /*-----------------------------------------------------------* * Function: display_instructions() * * Purpose: This function displays information on how to * * use this program and asks the user to enter 0 * * to quit, or 1 to continue. * * Returns: NO - if user enters 0 * * YES - if user enters any number other than 0 * *-----------------------------------------------------------*/ int display_instructions( void ) { printf("\n\n"); printf("\nThis program enables you to enter up to 99 people\'s "); printf("\nincomes and birthdays. It then prints the incomes by"); printf("\nmonth along with the overall income and overall average."); printf("\n");
cont = continue_function(); return( cont ); } /*-------------------------------------------------------------* * Function: get_data() * * Purpose: This function gets the data from the user. It * * continues to get data until either 100 people are * * entered, or until the user enters 0 for the month.* * Returns: nothing * * Notes: This allows 0/0/0 to be entered for birthdays in * * case the user is unsure. It also allows for 31 * * days in each month. * *-------------------------------------------------------------*/
void get_data(void) { for ( cont = YES, ctr = 0; ctr < MAX && cont == YES; ctr++ ) { printf("\nEnter information for Person %d.", ctr+1 ); printf("\n\tEnter Birthday:");
do { printf("\n\tMonth (0 - 12): "); scanf("%d", &month[ctr]); }while (month[ctr] < 0 || month[ctr] > 12 );
do { printf("\n\tDay (0 - 31): "); scanf("%d", &day[ctr]); }while ( day[ctr] < 0 || day[ctr] > 31 );
do { printf("\n\tYear (0 - 2002): "); scanf("%d", &year[ctr]); }while ( year[ctr] < 0 || year[ctr] > 2002 );
printf("\nEnter Yearly Income (whole dollars): "); scanf("%ld", &income[ctr]);
cont = continue_function(); /* <===================================== LOOK HERE! */ } /* ctr equals the number of people that were entered. */ } /*----------------------------------------------------------* * Function: display_report() * * Purpose: This function displays a report to the screen * * Returns: nothing * * Notes: More information could be printed. * *----------------------------------------------------------*/
void display_report() { grand_total = 0; printf("\n\n\n"); /* skip a few lines */ printf("\n SALARY SUMMARY"); printf("\n ==============");
for( x = 0; x <= 12; x++ ) /* for each month, including 0*/ { month_total = 0; for( y = 0; y < ctr; y++ ) { if( month[y] == x ) month_total += income[y]; } printf("\nTotal for month %d is %ld", x, month_total); grand_total += month_total; } printf("\n\nReport totals:"); printf("\nTotal Income is %ld", grand_total); printf("\nAverage Income is %ld", grand_total/ctr );
printf("\n\n* * * End of Report * * *\n"); } /*----------------------------------------------------------------* * Function: continue_function() * * Purpose: This function asks the user if they wish to continue.* * Returns: YES - if user wishes to continue * * NO - if user wishes to quit * *----------------------------------------------------------------*/
int continue_function( void ) { printf("\n\nDo you wish to continue? (0=NO/1=YES): "); scanf( "%d", &x );
while( x < 0 || x > 1 ) { printf("\n%d is invalid!", x); printf("\nPlease enter 0 to Quit or 1 to Continue: "); scanf("%d", &x); } if(x == 0) return(NO); else return(YES); }
================================================== ================================
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Buck Rogers wrote: Hi all! Newbie here.
Below is an example from Teach Yourself C in 21 Days. My apologies if it is a bit long.
What I don't understand is how the "get_data" function can call the "continue_function", and if NO is returned to "get_data", display_report executes and the program ends? Basically I am having trouble understanding the program flow within the "if" loop in the "main" function.
The "if" loops only initiates if cont=YES, so what happens if a function in the loop(in this case get_data) gets cont=NO? Does this really mean that if "something" within an "if" loop doesn't meet the condition of the loop, the result is that the next statement below the "something" gets executed and the the "if" loop is exited?
Hope I made myself clear.
What seems clear is that you need to go back and carefully study a good
C textbook, and I mean it in the nicest possible way.
There is no such thing as "if loop". The way `if` *statement* works is
this:
if (condition) {
/* "then" block */
} else {
/* "else" block */
}
/* "next" statement */
If `condition` (which is an expression) is non-zero, statements in the
"then" block get executed. Otherwise, statements in the "else" block
get executed. Once one of them is executed, the execution continues
with the "next" statement.
The "else" block is optional. If omitted, and the `condition` is zero,
the execution continues with the "next" statement.
In both "then" and "else" blocks curly braces are requied only if the
blocks contain more than one statement. So, you can have:
if (condition)
printf("true\n");
else
printf("false\n");
Note how the "then" statement still has to have a terminating ';'.
I'm sure your textbook (but apparently not the "...in 21 days" one) can
describe this much better, and in greater detail. I'm also sure someone
will shortly come along and correct any mistakes I made, but I think I
put across the most important things you seem to have misunderstood.
I recommend you get yourself K&R2, and read it. Thanks in advance!
================================================== ============================= #include <stdio.h>
#define MAX 100 #define YES 1 #define NO 0
/*--------------------*/ /* variables */ /*--------------------*/
long income[MAX]; /* to hold incomes */ int month[MAX], day[MAX], year[MAX]; /* to hold birthdays */ int x, y, ctr; /* For counters */ int cont; /* For program control */ long month_total, grand_total; /* For totals */
/*--------------------*/ /* function prototypes*/ /*--------------------*/
int main(void); int display_instructions(void); void get_data(void); void display_report(void); int continue_function(void);
/*--------------------*/ /* start of program */ /*--------------------*/
int main(void) { cont = display_instructions();
if ( cont == YES ) /* <================================ LOOK HERE! */ { get_data(); display_report(); } else printf( "\nProgram Aborted by User!\n\n");
return 0; } /*-----------------------------------------------------------* * Function: display_instructions() * * Purpose: This function displays information on how to * * use this program and asks the user to enter 0 * * to quit, or 1 to continue. * * Returns: NO - if user enters 0 * * YES - if user enters any number other than 0 * *-----------------------------------------------------------*/ int display_instructions( void ) { printf("\n\n"); printf("\nThis program enables you to enter up to 99 people\'s "); printf("\nincomes and birthdays. It then prints the incomes by"); printf("\nmonth along with the overall income and overall average."); printf("\n");
cont = continue_function(); return( cont ); } /*-------------------------------------------------------------* * Function: get_data() * * Purpose: This function gets the data from the user. It * * continues to get data until either 100 people are * * entered, or until the user enters 0 for the month.* * Returns: nothing * * Notes: This allows 0/0/0 to be entered for birthdays in * * case the user is unsure. It also allows for 31 * * days in each month. * *-------------------------------------------------------------*/
void get_data(void) { for ( cont = YES, ctr = 0; ctr < MAX && cont == YES; ctr++ ) { printf("\nEnter information for Person %d.", ctr+1 ); printf("\n\tEnter Birthday:");
do { printf("\n\tMonth (0 - 12): "); scanf("%d", &month[ctr]); }while (month[ctr] < 0 || month[ctr] > 12 );
do { printf("\n\tDay (0 - 31): "); scanf("%d", &day[ctr]); }while ( day[ctr] < 0 || day[ctr] > 31 );
do { printf("\n\tYear (0 - 2002): "); scanf("%d", &year[ctr]); }while ( year[ctr] < 0 || year[ctr] > 2002 );
printf("\nEnter Yearly Income (whole dollars): "); scanf("%ld", &income[ctr]);
cont = continue_function(); /* <===================================== LOOK HERE! */ } /* ctr equals the number of people that were entered. */ } /*----------------------------------------------------------* * Function: display_report() * * Purpose: This function displays a report to the screen * * Returns: nothing * * Notes: More information could be printed. * *----------------------------------------------------------*/
void display_report() { grand_total = 0; printf("\n\n\n"); /* skip a few lines */ printf("\n SALARY SUMMARY"); printf("\n ==============");
for( x = 0; x <= 12; x++ ) /* for each month, including 0*/ { month_total = 0; for( y = 0; y < ctr; y++ ) { if( month[y] == x ) month_total += income[y]; } printf("\nTotal for month %d is %ld", x, month_total); grand_total += month_total; } printf("\n\nReport totals:"); printf("\nTotal Income is %ld", grand_total); printf("\nAverage Income is %ld", grand_total/ctr );
printf("\n\n* * * End of Report * * *\n"); } /*----------------------------------------------------------------* * Function: continue_function() * * Purpose: This function asks the user if they wish to continue.* * Returns: YES - if user wishes to continue * * NO - if user wishes to quit * *----------------------------------------------------------------*/
int continue_function( void ) { printf("\n\nDo you wish to continue? (0=NO/1=YES): "); scanf( "%d", &x );
while( x < 0 || x > 1 ) { printf("\n%d is invalid!", x); printf("\nPlease enter 0 to Quit or 1 to Continue: "); scanf("%d", &x); } if(x == 0) return(NO); else return(YES); } ================================================== ================================
Vladimir Oka said: What seems clear is that you need to go back and carefully study a good C textbook, and I mean it in the nicest possible way.
I was about to give you a mild slapping for forgetting to trim all that code
you didn't comment on. But... er... well... <blush fgcolor="bright red">
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
On Wed, 10 May 2006 17:14:15 +0800, Richard Heathfield
<in*****@invalid.invalid> wrote: People have been posting using the name "Buck Rogers" for around four years. I sincerely hope all those articles are not from you, because if you've taken four years to get this far in your studies, it might be time to call it a day.
LMAO! Yep, it's me again! Good to C you again! C was and still is just
a hobby. Back then I didn't take it very seriously, and eventually gave
it up. Recently, I tried twice to sell the book(Teach Yourself C in 21
Days) on ebay, with no buyer. So I thought I'd give it another go before
giving it another go before I give it to charity, or throw it in the trash.
So here I am. Still a newbie 4 years later - probably for the rest of my
life! :D Below is an example from Teach Yourself C in 21 Days. My apologies if it is a bit long. It is also badly-written, so - if you *must* use that book - be prepared to unlearn some bad habits and some misconceptions later on.
Yep, there's heaps of typo and mistakes in it, but it's good I am picking
those up, since I am taking it a bit more C-riously this time around. I am
just using it to get my toe in the C water. For a reference, I have "A book
on C", by Kelley/Pohl, which is very terse, but seems to have a better
reputation. On receiving a return value of NO from continue_function(), get_data() terminates its loop on the next test, i.e. just about immediately, and exits. Control now returns to the caller, within main(). The statement just processed was get_data(), so control now flows to the next statement along, which is display_report(). Therefore, display_report() is executed.
Thanks for your help. I remember you helping me heaps years ago. :D
C you around like a fruit loop!!
Buck.
Richard Heathfield wrote: Vladimir Oka said:
What seems clear is that you need to go back and carefully study a good C textbook, and I mean it in the nicest possible way.
I was about to give you a mild slapping for forgetting to trim all that code you didn't comment on. But... er... well... <blush fgcolor="bright red">
Great minds think alike -- even when they mess things up. ;-)
[In my defence, I'm forced to use Google from the office.]
Richard Heathfield wrote: Buck Rogers said: Below is an example from Teach Yourself C in 21 Days. My apologies if it is a bit long.
It is also badly-written, so - if you *must* use that book - be prepared to unlearn some bad habits and some misconceptions later on.
That's the book I learned from.
The unlearning should take about two years on clc.
--
pete
Buck Rogers wrote: On Wed, 10 May 2006 17:14:15 +0800, Richard Heathfield <in*****@invalid.invalid> wrote:
People have been posting using the name "Buck Rogers" for around four years. I sincerely hope all those articles are not from you, because if you've taken four years to get this far in your studies, it might be time to call it a day.
LMAO! Yep, it's me again! Good to C you again! C was and still is just a hobby. Back then I didn't take it very seriously, and eventually gave it up. Recently, I tried twice to sell the book(Teach Yourself C in 21 Days) on ebay, with no buyer. So I thought I'd give it another go before giving it another go before I give it to charity, or throw it in the trash. So here I am. Still a newbie 4 years later - probably for the rest of my life! :D
That should have been your first warning. Recycle the book and get one
of the titles recommended elsewhere.
That being said, there is no shame in taking the long road. You will be
expected to put in the hours, however. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Bjorn Heimir Bjornsson |
last post by:
Hello all
As part of a thesis, I am model checking Python programs (a limited
subset anyway).
I have a very rough prototype that can deal with...
|
by: James Fortune |
last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't
understand why people who know how to write code to completely replace
a...
|
by: John L |
last post by:
Hello!
I'm somewhat new to C, and the problem is I'm not sure how to structure my
programs. I keep ending up with massively long main() functions...
|
by: jacob navia |
last post by:
Suppose that you want to know where your program has
passed, i.e. the exact flow of the program.
A brute force solution is to make an array of n...
|
by: prash |
last post by:
Hi everybody,
I would like to request all the members to help me find the source code
for the following :
1.C program for error detecting...
|
by: BJ |
last post by:
Problem: How can I code up a client side process to detect if the
network is available?
Synopsis: I am writing ASP.NET input forms for a...
|
by: John |
last post by:
Hi,
I use try, catch and finallay to control Sql connection, and run into 1
problem. Some of the sql errors, I don't want the program flow to go...
|
by: lovecreatesbea... |
last post by:
K&R 2, sec 2.4 says: If the variable in question is not automatic, the
initialization is done once only, conceptually before the program
starts...
|
by: 100grand |
last post by:
Modify the Inventory Program to use a GUI. The GUI should display the information one
product at a time, including the item number, the name of the...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
| |