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;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm trying to do switch then if else in it. I don't know where I missed but this doesn't work.
Here's my code:
include<stdio.h>
include<conio.h>
int main()
{
clrscr();
int grade;
printf("Input Grade");
scanf("%d",&grade);
switch(grade<101)
{
case 1:
if (grade>=95)
printf("A+");
else
printf("Invalid");
break;
case 2:
if (grade>=85)
printf("A");
else
printf("Invalid");
break;
default:
printf("Invalid");
}
getch();
}
So I don't know what's the matter. Only the first case work, when i enter a lower number than 95 'A' isn't the result. Please help me out.
If you have to use switch you should bring the if else statements outside of your switch in order to switch over a proper integer value. Something like this:
int state = 0;
if ( grade < 101 && grade >= 95 )
state = 1;
else if ( grade < 101 && grade >= 85 )
state = 2;
switch state {
case 1:
printf("A+");
break;
case 2:
printf("A");
break;
default:
printf("invalid");
break;
}
You can express this with switch statements in the following way (though you probably do not want to):
switch(grade)
{
case 100:
case 99:
case 98:
case 97:
case 96:
case 95:
printf("A+");
break;
case 94:
case 93:
case 92:
case 91:
case 90:
case 89:
case 88:
case 87:
case 86:
case 85:
printf("A");
break;
default:
printf("Invalid");
//or an if statement if more options are required.
break;
}
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
}
When I compile this program, it outputs both the corresponding switch case and the default tag contents, the only value it doesn't print out for is January, any help would be great!
Sample Input: 4
Sample Output:
April
Error
Though I could make the program better, I need to have it this specific way.
while (month != EOF)
{
switch (month)
{
case 49:
month2 = getchar();
switch (month2)
{
case 10:
printf("January \n");
break;
case 48:
printf("October \n");
break;
case 49:
printf("November \n");
break;
case 50:
printf("December \n");
break;
}
break;
case 50:
printf("February \n");
break;
case 51:
printf("March \n");
break;
case 52:
printf("April \n");
break;
case 53:
printf("May \n");
break;
case 54:
printf("June \n");
break;
case 55:
printf("July \n");
break;
case 56:
printf("August \n");
break;
case 57:
printf("Septembe \n");
break;
default: printf("Error \n");
}
month = getchar();
}
system("PAUSE");
return (0);
}
You just need an extra case to handle the stray '\n' that comes from you pressing enter at your terminal. January should work if you just type 1
Edit:Actually I just tested it on Linux and it works flawlessly, there might be some minor differences on Windows, though.
while (month != EOF)
{
switch (month)
{
case 49:
month2 = getchar();
switch (month2)
{
case 10:
printf("January \n");
break;
case 48:
printf("October \n");
break;
case 49:
printf("November \n");
break;
case 50:
printf("December \n");
break;
}
break;
case 50:
printf("February \n");
break;
case 51:
printf("March \n");
break;
case 52:
printf("April \n");
break;
case 53:
printf("May \n");
break;
case 54:
printf("June \n");
break;
case 55:
printf("July \n");
break;
case 56:
printf("August \n");
break;
case 57:
printf("Septembe \n");
break;
// Filter out stray \n
case '\n':
break;
default: printf("Error \n");
}
month = getchar();
}
system("PAUSE");
return (0);
I want to create a program that requests from the user 10 grades and then filters them to pass and fail, then prints the number of passes and fails. I did the program but the output is wrong.
int pass,fail,grade,studentcounter;
pass=0;
fail=0;
grade=0;
studentcounter=10;
while (studentcounter!=0)
{
printf("enter the next grade\n");
scanf("%d",grade);
student--;
}
switch (grade)
{
case 1:
if (grade >= 50)
pass++;
break;
case 2:
if (grade <= 49)
fail++;
break;
}
}
printf("the number of fail is %d",fail);
printf("the number of pass is %d",pass);
}
The problem is that the program request the ten grades but at the end it will print the number of fail and the number of pass as zero. Why?
You are entering a grade number. A switch statement tests that grade against the cases, and i am pretty sure the grades are not 1 percent or 2 percent. An if statement would be a more logical choice in this situation.
Second of all, you have a code block which is never used. First you set studentcounter to zero, then you say "only execute this block when studentcounter is NOT zero"...
studentcounter=0;
while (studentcounter!=0) {
printf("enter the next grade\n");
scanf("%d",grade);
student--;
}
The third problem is, you misspelled grade.
you can rewrite your code as follows:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int pass,fail,grade,studentcounter;
pass=0;
fail=0;
grade=0;
studentcounter=0;
while (studentcounter < 10) {
printf("enter the next grade:\n");
scanf("%d",&grade);
if (grade >= 50) {
pass++;
} else {
fail++;
}
studentcounter++;
}
printf("the number of fail is: %d \n",fail);
printf("the number of pass is: %d \n",pass);
return 0;
}
Sorry if I overlooked something; I don't have time to throw it into my editor :P
cheers
you need to keep the count of pass/fails inside the while loop, the grade variable will be overwritten at every input.
edit:
also, dont use a switch statement.
Ok, the code as presented is a pile of crap. I will leave it as an exercise for you to sort out what I fixed for you:
#include <stdio.h>
int main(int argc, char *argv[])
{
int pass = 0;
int fail = 0;
int grade= 0;
int studentcounter;
for (studentcounter=1; studentcounter<=10; studentcounter++ )
{
printf("Enter grade for student #%-2d :", studentcounter);
scanf("%d",&grade);
if(grade >=50)
pass++;
if(grade<=49)
fail++;
}
printf("the number of fail is %d\n",fail);
printf("the number of pass is %d\n",pass);
}
PS. If this is homework, please give credit when you turn it in. You can learn a lot by looking at other people's code, but never claim to create that which you haven't.
hmm, since you have to use swicth i will suggest the following:
Keep a variable which checks whether the grade is more than 50 or not and then use this variable inside switch:
char pass_fail = (grade >= 50)? y : n;
switch( pass_fail )
{
case 'y':
// print pass
break;
case 'n':
// print fail
break;
}
hope this helps you
BTW, there is a solution that uses 'switch' in a reasonable fashion. I won't get into a discussion of the advantages/pitfalls of this approach or the arcaneness of the 'C' language, but just to prove it's possible, here it is:
#include <stdio.h>
int main(void)
{
int pass, fail, grade, answer, studentcounter;
pass = 0;
fail = 0;
grade = 0;
studentcounter = 10;
while ( studentcounter-- )
{
printf("enter the next grade\n");
scanf("%d", &grade);
answer = ( grade > 49 );
switch ( answer )
{
case 1:
pass++;
break;
default:
fail++;
break;
}
}
printf("the number of fail is %d \n", fail);
printf("the number of pass is %d \n", pass);
}
C 'case' statements do not work the way you are expecting.
They operate on 'constant-expressions' which means that in order to accomplish the task you have given, 'Determine if a grade is pass or fail' one would have to write a convoluted switch statement of the form
switch ( grade )
{
case 100:
case 99:
case 98:
case 97:
case 96:
case 95:
case 94:
case 93:
case 92:
case 91:
case 90:
case 89:
case 88:
case 87:
case 86:
case 85:
case 84:
case 83:
case 82:
case 81:
case 80:
case 79:
case 78:
case 77:
case 76:
case 75:
case 74:
case 73:
case 72:
case 71:
case 70:
case 69:
case 68:
case 67:
case 66:
case 65:
case 64:
case 63:
case 62:
case 61:
case 60:
case 59:
case 58:
case 57:
case 56:
case 55:
case 54:
case 53:
case 52:
case 51:
case 50:
pass++;
break;
default:
fail++;
break;
}
This would probably give you the correct answer, however, you may receive a 'fail' on your homework for writing such a monster.
I would try to think of another problem that has 'while, switch and if' statements or perhaps you could modify your current program such that, instead of accepting 10 grades, it prompted the user if they wished to enter another grade and accepted only the answer 'Y', 'y', or 'N', 'n' thus 'switching' on that answer to continue.
2 errors:
1: The while loop should end after the switch statement.
2: Your variable name is "studentcounter" but you are decrementing "student".
Check the following code. This should work.
void main()
{
int pass,fail,grade,studentcounter;
pass=0;
fail=0;
grade=0;
studentcounter=10;
while (studentcounter!=0)
{
printf("enter the next grade\n");
scanf("%d",grade);
studentcounter--;
switch (grade)
{
case 1:
if (grade >= 50)
pass++;
break;
case 2:
if (grade <= 49)
fail++;
break;
}
}
printf("the number of fail is %d",fail);
printf("the number of pass is %d",pass);
}