The C formatted input/output: Reading from standard input and writing to standard output while discarding characters from the input stream

 

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: none

To do: Reading from standard input and writing to standard output while discarding characters from the input stream

To show: How to read from standard input and write to standard output while discarding some characters

 

 

// Reading and discarding characters from the input stream

#include <stdio.h>

 

int main(void)

{

int month1, day1, year1, month2, day2, year2;

 

printf("Enter a date in the form mm-dd-yy: ");

// pad 0 for two fields and discarding the - characters....

/* scanf("%d%*c%d%*c%d", &month1, &day1, &year1); */

scanf_s("%d%*c%d%*c%d", &month1, &day1, &year1);

printf("month = %02d day = %02d year = %02d\n\n", month1, day1, year1);

printf("Enter a date in the form mm/dd/yy: ");

//pad 0 for two fields and discarding the / characters...

/* scanf_s("%d%*c%d%*c%d", &month2, &day2, &year2); */

scanf_s("%d%*c%d%*c%d", &month2, &day2, &year2);

printf("month = %02d day = %02d year = %02d\n", month2, day2, year2);

return 0;

}

 

Output example:

 

Enter a date in the form mm-dd-yy: 12-05-2004

month = 12 day = 05 year = 2004

Enter a date in the form mm/dd/yy: 10/07/2000

month = 10 day = 07 year = 2000

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index