carothy
asked on
how to check the input data
#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.
#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.
Looks like a homework, maybe you can start posting some try.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hint:
1. Lookup what scanf returns.
2. Lookup what scanf does if parameters are not there or spaces.
Paul
1. Lookup what scanf returns.
2. Lookup what scanf does if parameters are not there or spaces.
Paul
ASKER
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);
}
}
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);
}
}
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> 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