The C and C++ code snippets monologue forum

The C, C++, Code, Coder, Coding Snippets Zone for Tenouk.com

 

 
It is currently Thu Sep 09, 2010 8:04 am

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Reading and discarding characters from the input stream
PostPosted: Sun Nov 12, 2006 2:27 pm 
Offline

Joined: Sun Nov 12, 2006 4:45 am
Posts: 120
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 and discarding characters from the input stream
To show: How to read & discard characters

Code:
// Reading and discarding characters from the input stream
// don't forget to put the .h lor!
#include <stdio>

int main()
{
   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:

Code:
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 . . .


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Skin by Lucas Kane