I'm getting this date as invalid "006.010.002021" and my question is, how can I validate it and convert it to "06.10.2021"
thank you in advance
int isDateValid(sDate date)
{
int daysPerMonth;
switch (date.month)
{
case 1:
case 2:
daysPerMonth = 28;
break;
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
daysPerMonth = 30;
break;
case 12:
daysPerMonth = 31;
break;
default:
return 0;
}
if ((daysPerMonth == 28) && isLeapYear(date.year))
daysPerMonth++;
if (date.day > daysPerMonth || date.day <= 0 || date.day > 2117 || date.year < 1917)
return 0;
}
You can use strptime() to read a string into a date/time format. However, you can only check if the string failed or not, it will not tell you what is wrong and how to fix it.
The best way to handle a wrong date is to throw an error informing the user that the date is wrong, because your code can not fix it.
Your case 1: will assign 28 days to january as well and the other cases will also yield wrong days per month.
switch (date.month)
{
case 2:
daysPerMonth = 28;
break;
case 4:
case 6:
case 9:
case 11:
daysPerMonth = 30;
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
daysPerMonth = 31;
break;
default:
return 0;
}
Related
Why didn't I get the output of the array? I want to save the value to array,m[10], by switch case,but i can't print out the value of array.
#include <stdio.h>
/******************************************
* 公元年分非4的倍數,為平年。
* 公元年分為4的倍數但非100的倍數,為閏年。
* 公元年分為100的倍數但非400的倍數,為平年。
* 公元年分為400的倍數為閏年。
*****************************************/
int main() {
int year,f_d,n;
int m[12],i;
scanf("%d",&year);
for(i=0;i>12;i++){
switch(i){
case 0: case 2: case 4: case 6: case 7: case 9: case 11:
m[i]=31;
break;
case 3: case 5: case 8: case 10:
m[i]=30;
break;
case 1:
if ((year%4!=0)||
((year%100==0)&&(year%400!=0)))
m[i]=28;
else
m[i]=29;
break;
default:
m[i]=0;
}
}
for(i=0;i>12;i++)
printf("%d/n",m[i]);
return 0;
}
The very first loop
for(i=0;i>12;i++)
does not do what you want: when i is set to 0, it is not greater than 12 so the whole loop is skipped.
replace for (i = 0; i > 12; i++) to for (i = 0; i < 12; i++) will work.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
int i=0, marks,pass=0, fail=0;
int A_Plus=0,A=0,A_minus=0,B_Plus=0,B =0,B_minus=0,C_Plus=0,C=0,C_minus=0,D_Plus=0,D=0,F=0;
while(marks !=-1)
{
scanf("%d", &marks);
if(marks<-1 || marks>100)
{
printf("You enter invalid mark: %d \n",marks);
}
if (marks != -1 &&(marks>=0 && marks <=100))
{
if(marks < 60)
{
fail = fail + 1;
}
else
{
pass = pass + 1;
}
i=i+1;
}
}
Although I do not recommend using a switch statement for "yes/no"-type decisions, such as yours, you could convert your if ... else blocks into the following switch block:
switch (marks/60) // If marks is less than 60, this expression will truncate to zero ...
{
case 0:
++fail;
break;
default: // ... otherwise, we'll get here.
++pass;
break;
}
Note that this assumes that you have previously ruled out negative numbers (as you have done).
This approach (dividing the 'switch variable' by a constant) can be useful when you have more than two possible outcomes, determined by adjacent, fixed-size ranges. For example, rather than just "fail" and "pass", you may have 4 different grades (ranges 0..29, 30..59, 60..89 and 90+); in such a case, a switch such as the following may be a good approach:
switch (marks/30)
{
case 0: // < 30
++fail;
break;
case 1: // 30 thru 59
++poor;
break;
case 2: // 60 thru 89
++good;
break;
default: // >= 90
++excellent;
break;
}
I assume this is some academic exercise rather than some useful design aim? So I'll play the silly game. Expanding on Adrian Mole's idea:
// Grade: 0 = fail
// 1 = pass
// other = invalid input
int grade == marks > 100 ?
-marks :
marks /= 60 ;
switch( grade )
{
case 0: fail++ ; break ;
case 1: pass++ ; break ;
default:
if( marks != -1) printf( "You enter invalid mark: %d \n", marks ) ;
}
Note in any case your original code can be simplified in any case:
if( marks == -1 ) // do nothing
else if( marks < -1 || marks > 100 ) printf( "You enter invalid mark: %d \n", marks ) ;
else if( marks < 60) fail++ ;
else pass++ ;
An "at face value" solution:
switch( marks )
{
case -1 : break
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9:
case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 19:
case 20: case 21: case 22: case 23: case 24: case 25: case 26: case 27: case 28: case 29:
case 30: case 31: case 32: case 33: case 34: case 35: case 36: case 37: case 38: case 39:
case 40: case 41: case 42: case 43: case 44: case 45: case 46: case 47: case 48: case 49:
case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: case 58: case 59:
fail++ ; break ;
case 60: case 61: case 62: case 63: case 64: case 65: case 66: case 67: case 68: case 69:
case 70: case 71: case 72: case 73: case 74: case 75: case 76: case 77: case 78: case 79:
case 80: case 81: case 82: case 83: case 84: case 85: case 86: case 87: case 88: case 89:
case 90: case 91: case 92: case 93: case 94: case 95: case 96: case 97: case 98: case 100:
pass++ ; break ;
default:
printf( "You enter invalid mark: %d \n", marks ) ;
}
With this latter solution at least you could insert the grade breaks that the Plus_A, A variables hint at perhaps. If you were to do that you should calculate the pass/fail count by adding the grade counts rather then incrementing for each grade case.
Moreover if you are to have multiple grades, do not have a separate variable for each one - use an array, then an enumeration to index the array:
int grade_count[12] = {0} ;
enum eGrade
{
A_Plus, A, A_minus,
B_Plus, B, B_minus,
C_Plus, C, C_minus,
D_Plus, D, F
} ;
Then you might have:
grade_count[B_Plus]++ ;
If you use gcc like compiler (gcc, clang etc) you can use the extension:
switch(marks)
{
case 0 ... 59:
fail = fail + 1;
break;
default:
pass = pass + 1;
break;
}
But it is not standard C
You have only two cases (greater or less than 60), so a switch would just be more complex. If you have a case with more (e.g. A, B, C etc. letter grades), you'd have to map [0, 100] to [A, B, C, D, F] to give a code or an enum, then switch on that result. Making the mapping would be more complex than just using a series of if / else if / else statements, but if you want to do that anyway, a loop like this would work:
enum GRADE {
GRADE_A,
GRADE_B,
...
GRADE_F,
GRADE_COUNT, // Not an actual grade.
};
...
GRADE grade = GRADE_F; // default
int mark = /* comes from somewhere */
struct {
GRADE grade;
int lowest;
} grade_list[] = {
{ GRADE_A, 90 },
{ GRADE_B, 80 },
{ GRADE_C, 70 },
{ GRADE_D, 60 },
{ GRADE_F, 0 },
};
for (int grade_idx = 0;
grade_idx < GRADE_COUNT;
grade_idx++
) {
if (marks >= grade_list[grade_idx].lowest) {
grade = grade_list[grade_idx].grade
break;
}
}
switch (grade) {
case GRADE_A:
...
break;
case ...:
...
break;
case GRADE_F:
...
break;
}
Don't do this as-is, instead collect the total count and subtract off the failures or successes later
// reject < 0 or > 100
...
marks_total++;
if (mark - 60 > 0) marks_passing++;
}
marks_failed = marks_total - marks_passing;
I'm trying to create a fiscal code calculator algorithm.
Here's the code:
#include<stdio.h>
#include<string.h>
int main()
{
int Day,Month,Year,i;
char Mo;
char Name[1][30];
char Surname[1][30];
char A,B,C,D,E,H,L,M,P,R,S,T;
printf("Insert your birthday day: ");
scanf("%d",&Day);
printf("Insert your birthday month: ");
scanf("%d",&Month);
printf("Insert your birthday year (last two numbers): ");
scanf("%d",&Year);
/*Month calculator*/
switch(Month)
{
case 1:
Mo="A";
break;
case 2:
Mo="B";
break;
case 3:
Mo="C";
break;
case 4:
Mo="D";
break;
case 5:
Mo="E";
break;
case 6:
Mo="H";
break;
case 7:
Mo="L";
break;
case 8:
Mo="M";
break;
case 9:
Mo="P";
break;
case 10:
Mo="R";
break;
case 11:
Mo="S";
break;
case 12:
Mo="T";
break;
}
printf("Your fiscal code is: %d%c%d",Year,Mo,Day);
}
In every case of the switch i receive the same error: Incompatible pointer to integer conversion assigning to 'char' from 'char[2]'.
Where is the error?
Thanks to all!
You are trying to assign chars to char*s. Mo is a char and strings surrounded in double quotes(") are char*s ending with a \0. Use single quotes(') to denote characters.
Change
switch(Month)
{
case 1:
Mo="A";
break;
case 2:
Mo="B";
break;
case 3:
Mo="C";
break;
case 4:
Mo="D";
break;
case 5:
Mo="E";
break;
case 6:
Mo="H";
break;
case 7:
Mo="L";
break;
case 8:
Mo="M";
break;
case 9:
Mo="P";
break;
case 10:
Mo="R";
break;
case 11:
Mo="S";
break;
case 12:
Mo="T";
break;
}
to
switch(Month)
{
case 1:
Mo='A';
break;
case 2:
Mo='B';
break;
case 3:
Mo='C';
break;
case 4:
Mo='D';
break;
case 5:
Mo='E';
break;
case 6:
Mo='H';
break;
case 7:
Mo='L';
break;
case 8:
Mo='M';
break;
case 9:
Mo='P';
break;
case 10:
Mo='R';
break;
case 11:
Mo='S';
break;
case 12:
Mo='T';
//break; Not needed
}
Here is a program to print Roman Numeral values up to a
hundred everything works fine except for when i==88, on this line tab indents one extra set to stop.
I sort of understand why that is 8 character.
How can I fix that ?
Thank you.
int main() {
unsigned int i, tensDigit, singleUnit;
printf( "Roman Numeral\t\t\t\tDecimal\n");
for ( i = 1; i <= 100; i++ ) {
tensDigit = i / 10;
singleUnit = i % 10;
switch ( tensDigit ) {
case 0:
break;
case 1:
printf("X");
break;
case 2:
printf("XX");
break;
case 3:
printf("XXX");
break;
case 4:
printf("XL");
break;
case 5:
printf("L");
break;
case 6:
printf("LX");
break;
case 7:
printf("LXX");
break;
case 8:
printf("LXXX");
break;
case 9:
printf("XC");
break;
case 10:
printf("C");
break;
default:
break;
}
switch ( singleUnit ) {
case 0:
printf("\t\t\t\t%d\n", i);
break;
case 1:
printf("I\t\t\t\t%d\n", i);
break;
case 2:
printf("II\t\t\t\t%d\n", i);
break;
case 3:
printf("III\t\t\t\t%d\n", i);
break;
case 4:
printf("IV\t\t\t\t%d\n", i);
break;
case 5:
printf("V\t\t\t\t%d\n", i);
break;
case 6:
printf("VI\t\t\t\t%d\n", i);
break;
case 7:
printf("VII\t\t\t\t%d\n", i);
break;
case 8:
printf("VIII\t\t\t\t%d\n", i);
break;
case 9:
printf("IX\t\t\t\t%d\n", i);
break;
}
}
return 0; }
First post in this awesome site!
So, I'm pretty much a dabbler in C language, and I am making a program that could return the day at the user-entered date. I didn't use any functions or structures, for I want to build my basic concepts in C first, which here are, if-else, switch statements, etc...
The program compiled quickly & ran w/o errors, but the answer is delayed by 1 day ;) I mean, when I entered '21 7 1993', which was a 'Wednesday', I'm getting 'Thursday'. Similarly for others.
I'm sure there's a fault in the concept. Please help me sort it out.Any comments welcome on the code below:
#include<stdio.h>
int temp,yr,yr_new,yr_latest,date,month,i,leap,ord,odd=0;
char flag='0';
int main()
{
clrscr();
puts("Enter the date in the format dd/month-no/yyyy");
scanf("%d %d %d",&date,&month,&yr);
temp=yr/1000;
switch(temp)
{
case 1: if(temp==0)
puts("ERROR");
case 2: if(temp==1)
{
if(yr<1600)
yr_new=yr-1200;
else
yr_new=yr-1600;
break;
}
case 3: if(temp==2)
{ if (yr<2400)
yr_new=yr-2000;
else if(yr<2800)
yr_new=yr-2400;
else
yr_new=yr-2800;
break;
}
}
temp=(yr_new/100);
odd+=(temp*5);
yr_latest=yr_new-(temp*100);
leap=yr_latest/4;
ord=yr_latest-leap;
for(i=1;i<=leap;i++)
odd+=2;
for(i=1;i<=ord;i++)
odd+=1;
/* Leap Year */
if(yr%400==0 && yr%100!=0)
flag='1';
/* month */
switch(month)
{
case 1:
{odd+=(date%7); break; }
case 2:
{odd+=(date%7);break;}
case 3:
{ odd+=((date%7)+3);
if(flag=='1')
odd+=1;
break ;
}
case 4:
{ odd+=((date%7)+6);
if(flag=='1')
odd+=1;
break; }
case 5:
{ odd+=((date%7)+8);
if (flag=='1')
odd+=1;
break;
}
case 6:
{ odd+=((date%7)+11);
if (flag=='1')
odd+=1;
break; }
case 7:
{ odd+=((date%7)+13);
if(flag=='1')
odd+=1;
break; }
case 8:
{ odd+=((date%7)+16);
if(flag=='1')
odd+=1;
break ;}
case 9:
{ odd+=((date%7)+19);
if(flag=='1')
odd+=1;
break;}
case 10:
{ odd+=((date%7)+21);
if(flag=='1')
odd+=1;
break;}
case 11: {
odd+=((date%7)+24);
if(flag=='1')
odd+=1;
break; }
case 12:
{ odd+=((date%7)+26);
if(flag=='1')
odd+=1;
break;
}
}
odd=odd%7;
switch(odd)
{ case 0:puts("Sunday"); break;
case 1:puts("Monday"); break;
case 2:puts("Tuesday"); break;
case 3:puts("wednesday"); break;
case 4:puts("thursday"); break;
case 5:puts("friday"); break;
case 6:puts("Saturday"); break;
default: puts("error!");
}
getch();
return 0;
}
I think you are having some problem in your switch case!. Try the following changes, It is some what different logic from yours-
#include<stdio.h>
int temp,yr,yr_new,yr_latest,date,month,i,leap,ord,odd=0;
int main()
{
puts("Enter the date in the format dd/month-no/yyyy");
scanf("%d %d %d",&date,&month,&yr);
temp=yr/1000;
switch(temp)
{
case 1: if(temp==0)
puts("ERROR");
case 2: if(temp==1)
{
if(yr<1600)
yr_new=yr-1200;
else
yr_new=yr-1600;
break;
}
case 3: if(temp==2)
{
if(yr == 2000) // Note this change also
yr_new=yr-1900;
else if (yr<2400)
yr_new=yr-2000;
else if(yr<2800)
yr_new=yr-2400;
else
yr_new=yr-2800;
break;
}
}
temp=(yr_new/100);
odd+=(temp*5);
yr_latest=yr_new-(temp*100);
yr_latest=yr_latest-1; // Here i am leaving the current year in odd days calculation.
leap=yr_latest/4;
ord=yr_latest-leap;
for(i=1;i<=leap;i++)
odd+=2;
for(i=1;i<=ord;i++)
odd+=1;
for(i=1;i<month;i++) // this logic is to calculate the odd days for the current year.
{
switch(i)
{
case 1:
odd+=3;
break;
case 2:
if((yr_latest+1)%4 == 0)
odd+=1;
else odd+=0;
break;
case 3:
odd+=3;
break;
case 4:
odd+=2;
break;
case 5:
odd+=3;
break;
case 6:
odd+=2;
break;
case 7:
odd+=3;
break;
case 8:
odd+=3;
break;
case 9:
odd+=2;
break;
case 10:
odd+=3;
break;
case 11:
odd+=2;
break;
case 12:
odd+=3;
break;
}
}
odd+=date;
odd=odd%7;
switch(odd)
{ case 0:puts("Sunday"); break;
case 1:puts("Monday"); break;
case 2:puts("Tuesday"); break;
case 3:puts("wednesday"); break;
case 4:puts("thursday"); break;
case 5:puts("friday"); break;
case 6:puts("Saturday"); break;
default: puts("error!");
}
return 0;
}
Current year means,if your input is '21 7 1993', first i am calculating odd days till '12.12.1992', then i am calculating the odd days for the remaining days!