Coder's Guild Mailing List

Re: Re: Quickly needed, can any one help?

Posted by Fox on 2000-07-04

> Fox,
> 
>        I'd appreciate it if you would send me some kind of
> example regarding the code request for how many days are
> left.

/*************************************/

	Certainly:

/*
This program simply creates a loop which decreases a variable
everytime it iterates.  This variable is used in the string.
It assumes QuickWin or MSDOS environment
(as it's too simple to bother with the windows interface;
it's only an example).
*/
#include <stdio.h>


void main()
{
int daysleft;
float anothernumber = 38.667;
char ArrayString[] = "\nyou have %i days left before your account expires";

/*
This just decriments the variable and uses it in the printf for formatting
into the string.
*/
for(daysleft = 60; daysleft > 0; daysleft--)
  {
   printf(ArrayString, daysleft);  //this proves that you can use a string
  }                                //stored in a string variable, too.
/*
if you want to see it work the other way, just copy and replace the 'ArrayString' in 
the function with the string itself.
It'll look like this:

printf("\nyou have %i days left before your account expires", daysleft);

It's all the same, though!
*/

  //print out plain text

printf("\n\n\nYOUR ACCOUNT HAS EXPIRED!  PREPARE TO BE 
TERMINATED!\n\n");

daysleft = 8; //reset it for illustration purposes

/*
This just shows that you can print out two types in one call.  Each
% character flag corresponds to the appropiate place in the function.

ie. the %f is first, and it corresponds to be replaced by the anothernumber.
%i is second, and it corresponds to the next one over.

These '\n' tell it to drop to a newline.

Don't pay attention to the function being on more than one line.
That's so it will fit, and that it looks here like it will look on the screen; it's still one 
fucntion call, and it's still only ONE string. Here, I'm using a string that isn't stored 
in a variable just so you can see both.
*/
printf("\nThis is just to print out a float: %f"
       "\nAnd just something else to prove this works: %i\n",
       anothernumber, daysleft);

}


	I've tested this code, and yes it does work.  It only illustrates how to use 
'printf()'.  It may not be what you want, but it's only an example of how to use it.  
Your 'daysleft' could come from anywhere (a stored file, say)!

	As you can see from the last call to 'printf()', each specific "flag" (those 
characters preceded by '%') corresponds to that same parameter distance after 
the string (the second flag corresponds to the second paramter after the string).  
By the positions of the parameters and the flags, printf knows what to replace 
with what (and where to put it).

	I hope this helps, too...as It took me only 15 minutes to make ;-).

/************************************/
signed: Khan DKC_Fox

/**********************************
The Karate master's black belt only
holds his pants up.  Rank means nothing.
It's skill and knowledge that really matter.
*************************************/