?
Solved

how to check the input data

Posted on 2005-05-16
9
Medium Priority
?
368 Views
Last Modified: 2010-04-15
#include <stdio.h>
#include <ctype.h>
#include <string.h>

void main() {

  char mid,month[3];
  int day, i, mon=0;
  printf("Enter a date in the format (dd-MMM): ");
  scanf("%d - %s", &day, &month);
  if(day==' ' || day=='\0' )
  printf("Error: Nothing is Entered.");
  else {
  for(i=0;i<3;i++)
     month[i]=toupper(month[i]);

  if(strcmp(month, "JAN")==0)
  mon=1;
  if(strcmp(month, "FEB")==0)
  mon=2;
  if(strcmp(month, "MAR")==0)
  mon=3;
  if(strcmp(month, "APR")==0)
  mon=4;
  if(strcmp(month, "MAY")==0)
  mon=5;
  if(strcmp(month, "JUN")==0)
  mon=6;
  if(strcmp(month, "JUL")==0)
  mon=7;
  if(strcmp(month, "AUG")==0)
  mon=8;
  if(strcmp(month, "SEP")==0)
  mon=9;
  if(strcmp(month, "OCT")==0)
  mon=10;
  if(strcmp(month, "NOV")==0)
  mon=11;
  if(strcmp(month, "DEC")==0)
  mon=12;

  printf("Day entered:  %d\n", day);
  printf("Month entered:  %s\n", month);
  printf("Month entered:  %d\n", mon);
  }
 }


If nothing or just spaces are entered, the program should print the following
message: Nothing is Entered.
If the entered string does not match the dd-MMM format, the following message
should be printed: Invalid Format.
• If the entered month does not match any one of the 12 abbreviated month labels,
the following message should be printed:  Unrecognized Month.
• If the entered day and month values do not appear in a calendar of normal year
(non-leap year), the following message should be printed: Invalid Date.
0
Comment
Question by:carothy
7 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 14010221
Looks like a homework, maybe you can start posting some try.
0
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 100 total points
ID: 14010319
Hi Carothy,

You're on the right track.  A bit wordy, but a very workable solution.

You've got these two lines in your program:

  printf("Enter a date in the format (dd-MMM): ");
  scanf("%d - %s", &day, &month);

The prinf() statement tells the user that the data format is dd-MMM.  The scanf() statement allows blanks, tab, etc. on both sides of the '-'.  This seems like a conflict.

'%s' in the format specified means to copy to end of string.  You may want to rethink your usage of '%s'.

Lastly, (and probably the biggest error), you define your month variables as:

  char mid,month[3];

Remember that C string are ALWAYS at least 1 character longer than the visible portion of the string "JAN", "FEB", "MAR", etc are all 4 characters long when you count the string terminator.  And you will need a string terminator!

This ties in with using '%s'  If the user enters 12-JUN, your program is writing a byte (the string terminator) to an undefined location.  month[] needs to be at least 4 characters long.

If the user enters 12-JUNE, month[] needs to be at least 5 characters long.  12-APRIL, 6 characters, etc.


Good Luck!
Kent
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 14010583
Hint:

1. Lookup what scanf returns.

2. Lookup what scanf does if parameters are not there or spaces.

Paul
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:carothy
ID: 14018506
Paul:

could give me any reference about  what scanf does if parameters are not there or spaces.

and can i modify as follow:

#include <stdio.h>
#include <ctype.h>
#include <string.h>

void main() {

  char month[4];
  int day, i, mon=0;
  printf("Enter a date in the format (dd-MMM): ");
  i=scanf("%d - %s", &day, &month);
  if(i!=0 )
  printf("Error: Nothing is Entered.");
  else {
  for(i=0;i<3;i++)
     month[i]=toupper(month[i]);

  if(strcmp(month, "JAN")==0)
  mon=1;
  if(strcmp(month, "FEB")==0)
  mon=2;
  if(strcmp(month, "MAR")==0)
  mon=3;
  if(strcmp(month, "APR")==0)
  mon=4;
  if(strcmp(month, "MAY")==0)
  mon=5;
  if(strcmp(month, "JUN")==0)
  mon=6;
  if(strcmp(month, "JUL")==0)
  mon=7;
  if(strcmp(month, "AUG")==0)
  mon=8;
  if(strcmp(month, "SEP")==0)
  mon=9;
  if(strcmp(month, "OCT")==0)
  mon=10;
  if(strcmp(month, "NOV")==0)
  mon=11;
  if(strcmp(month, "DEC")==0)
  mon=12;

  printf("Day entered:  %d\n", day);
  printf("Month entered:  %s\n", month);
  }
 }
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 14018549

Hi Carothy,

I suggest that your format string look more like this:

  i=scanf("%d - %3s", &day, &month);
 
Otherwise you still run the rist of a buffer overflow.  :(


Kent
0
 
LVL 16

Assisted Solution

by:PaulCaswell
PaulCaswell earned 100 total points
ID: 14018659
>>  i=scanf("%d - %3s", &day, &month);
This will crash. I'd suggest instead:
  i=scanf("%d - %3s", &day, month);


>>Lookup what scanf does if parameters are not there or spaces
Under Windoze, use MSDN.

Here's a Linux reference that looks like it should help:
http://www.die.net/doc/linux/man/man3/scanf.3.html
Read the paragraph under 'RETURN VALUE'.

Paul
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 14018680

>>  i=scanf("%d - %3s", &day, &month);
This will crash. I'd suggest instead:
  i=scanf("%d - %3s", &day, month);


Whoops.  :)

Although most compilers detect the "superfluous &' and issue a warning.  Many even ignore it.  Still, I should have caught it.


Kent
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question