It only runs the first " if " why so? [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
it only prints " Sunday " even if I typed another date in. how do I resolve this?
i usually don't use int main() but a friend of mine did this method and it worked but I'm using a program that is written by the school so I can't paste it directly. but I'm pretty sure I copied it corectly
#include <stdio.h>
int date;
int main()
{
printf(" \n\n JUNE 2020 \n");
printf(" SUN MON TUE WED THU FRI SAT \n");
printf(" 1 2 3 4 5 6 \n");
printf(" 7 8 9 10 11 12 13 \n");
printf(" 14 15 16 17 18 19 20 \n");
printf(" 21 22 23 24 25 26 27 \n");
printf(" 28 29 30 \n\n\n");
printf("Here is your schedule for June 2020 \n");
printf("Please select a date: ");
scanf("%d", &date);
if (( date == 7 ) || ( date == 14 ) || ( date == 21 ) || ( date || 28 ))
{
printf("sunday! ");
}
else if (( date == 1 ) || ( date == 8 ) || ( date == 15 ) || ( date == 22 ) || ( date == 29 ))
{
printf("monday! ");
}
else if (( date == 2 ) || ( date == 9 ) || ( date == 16 ) || ( date == 23 ) || ( date == 30 ))
{
printf("tuesday! " );
}
else if (( date == 3 ) || ( date == 10 ) || ( date == 17 ) || ( date = 24 ))
{
printf("wednesday!");
}
else if (( date == 4 ) || ( date == 11 ) || ( date == 18 ) || ( date == 25 ))
{
printf("thursday!");
}
else if (( date == 5 ) || ( date == 12 ) || ( date == 19 ) || ( date == 26 ))
{
printf("friday!");
}
else if (( date == 6 ) || ( date == 13 ) || ( date == 20 ) || ( date == 27 ))
{
printf("saturday!");
}
return 0;
} // end

Your problem
( date || 28 ))
Is always true and hence first if is always true.
Change to
( date == 28 ))
Explanation
( date || 28 ))
Is equivalent to if (date || true) and it's always true.

The problem with your code is that you have probably made a typo in the line:
if (( date == 7 ) || ( date == 14 ) || ( date == 21 ) || ( date || 28 ))
The condition is always evaluated as true because of ( date || 28 ).
Change it to ( date == 28 ) and everything should work as expected.

There is an error in the last condition of your first if statement.
if (( date == 7 ) || ( date == 14 ) || ( date == 21 ) || ( date || 28 ))
should be changed to:
if (( date == 7 ) || ( date == 14 ) || ( date == 21 ) || ( date == 28 ))
Previously, it was evaluating ( date || 28 ), which is true as date is a positive integer. This made the entire condition true.

In your first if statement, you have (date || 28) which should have been (date == 28) instead:
if (( date == 7 ) || ( date == 14 ) || ( date == 21 ) || ( date || 28 ))
{
printf("sunday! ");
}
The reason that it always runs only the first if statement is that else if only gets checked if the above if statement is not true. In your case, the first if statement is always true, because ( date || 28 ) expression is always true.

Related

What do these return conditions do in a Boolean function?

On codecademy there exists a course on C, which includes a project on how to make a calendar. This project includes a boolean function which decides if a given year is a leap year or not. Code:
bool is_leap_year(int year) {
return (year % 4 == 0 && (year % 100 || year % 400 == 0));
}
Given my beginner understanding of operators and return statements, my reading of this code would be: "A given year will be a leap year if it is divisible by 4 AND 100 OR 400."
But this would mean that 1992 wouldn't be a leap year, and 1900 would be, which is plainly wrong.
How come then, that when I run the code and input these years, it does return a correct answer?
You appear to think
x || y == 0
means
x == 0 || y == 0
But it doesn't.
x || y == 0 doesn't mean "x or y is equal to zero".
x || y == 0 means "x, or y is equal to zero".
Put more clearly,
x || y == 0 means "(x) is true or (y is equal to zero) is true".
Since true simply means non-zero in C,
x || y == 0
is equivalent to
x != 0 || ( y == 0 ) != 0
That means the formula checks if the year isn't divisible by 100.
year % 4 == 0 Year is divisible by 4.
year % 100 Year isn't divisible by 100.
year % 400 == 0 Year is divisible by 400.
(Year is divisible by 4) and ( (Year isn't divisible by 100) or (Year is divisible by 400) )
How this would normally be stated in English:
It's a leap year if it's divisible by 4, but not by 100. Except years divisible by 400 are leap years.
And here's how things are calculated:
year % 4 == 0 && (year % 100 || year % 400 == 0)
1992 % 4 == 0 && (year % 100 || year % 400 == 0)
0 == 0 && (year % 100 || year % 400 == 0)
1 && (year % 100 || year % 400 == 0)
1 && (year % 100 || year % 400 == 0)
1 && (1992 % 100 || year % 400 == 0)
1 && ( 92 || year % 400 == 0)
1 && 1
1
The right-hand side of || isn't evaluated because its left-hand is true.
This return statement
return (year % 4 == 0 && (year % 100 || year % 400 == 0));
can be equivalently rewritten like
return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
or like
return (year % 4 == 0 && year % 100 != 0 ) || (year % 4 == 0 && year % 400 == 0));
The condition means that a leap year is divisible by 4 and either not divisible by 100 or divisible by 400. So 1992 is a leap year because it is divisible by 4 but not divisible by 100. And 1900 is not a leap year because though it is divisible by 4 but it also divisible by 100 and not divisible by 400. That is neither this condition (year % 4 == 0 && year % 100 != 0 ) nor this condition (year % 4 == 0 && year % 400 == 0)) is satisfied.

How to calculate how many days to the person's birthdate from the beginning of their birthyear in C?

This us how much I've gotten
int myd;
int mym;
int myy;
printf("Enter your day of birth:");
scanf("%d", &myd);
printf("Enter your month of birth:");
scanf("%d", &mym);
printf("Enter your year of birth:");
scanf("%d", &myy);
int month;
if (mym == 1 || mym == 3 || mym == 5 || mym == 7 || mym == 8 || mym == 10 || mym == 12)
{month = 31;}
else if (mym == 4 || mym == 6 || mym == 9 || mym == 11)
{month = 30;}
else if (mym == 2 && myy %4 !=0 && myy %100 ==0)
{month = 28;}
else if (mym == 2 && myy %4 ==0 && myy %100 !=0)
{month = 29;}
I'm not sure how to increment the days of the months before the birth month.
If I knew that, maybe I can add that increment + birth date = no. of days until birthday from the beginning of their birth year
You need an array, which contains a number of days in each month. Each element beside the 2nd one will have a literal value, either 30 or 31:
int days_in_month[] = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
For the 2nd element, representing February, you need to calculate the number of days based on the year. You already have that, but it is not correct. For example this condition:
... && myy % 4 != 0 && myy % 100 == 0)
will never be true. Even after after changing the operator:
... && (myy % 4 != 0 || myy % 100 == 0))
it is still not correct because it will match years divisible by 400, which are leap years. The correct condition is:
(myy % 4 != 0 || myy % 100 == 0) && myy % 400 != 0
or for leap years (basically a negation of the above):
(myy % 4 == 0 && myy % 100 != 0) || myy % 400 == 0
With that, the rest is just a loop through days_in_month, summing the values of days in months previous to the birthday, and at the end adding the day of month of the birthday. You can see the code in godbolt. The output of some examples:
]$ echo -en "1\n3\n1900\n" | ./a.out
Enter your day of birth:Enter your month of birth:Enter your year of birth:
01-03-1900: 60
]$ echo -en "1\n3\n1996\n" | ./a.out
Enter your day of birth:Enter your month of birth:Enter your year of birth:
01-03-1996: 61
]$ echo -en "1\n3\n1997\n" | ./a.out
Enter your day of birth:Enter your month of birth:Enter your year of birth:
01-03-1997: 60
]$ echo -en "1\n3\n2000\n" | ./a.out
Enter your day of birth:Enter your month of birth:Enter your year of birth:
01-03-2000: 61
As a last point, one could argue that a year starts with Jan 1st, so there is 0 days from the beginning of the year to Jan 1st :-) If that's the case, just subtract 1 from the final result.
EDIT
One more thing, before doing any calculations, validate user inputs first. In particular that the month is between 1 and 12 (inclusive), or otherwise you will start reading garbage past the days_of_month array.
You can use codes like this:
int mym;
int answer[] = {31,0,31,30,31,30,31,31,30,31,30,31};
printf("Enter your month of birth:");
scanf("%d", &mym);
if(mym==2){
if(myy %4 != 0 && myy %100 ==0){
answer[mym] = 28;
}
if(myy %4 ==0 && myy %100 !=0){
answer[mym]= 29;
}
}
else{
printf("%d",answer[mym]);
}
I think you should use array ( like answer[] ) for convenience.

How i make restrictions into a variable

I'm new in C language and I have a question.
I have to create a program where I put the number of days I worked in every single month.
I already done that but I need to restrict the days by the number of days of each month .
Example : January more than 1 day less than 31 , February more than 1 but less than 28 etc...
int numero_dias, mes;
for (mes = 0; mes != 12; mes++){
do {
printf("Digite o
numero de dias trabalhados no mes %s: ", meses[mes]);
scanf("%d", &numero_dias);
if (meses[mes] == 0 meses[mes] == 2 meses[mes] == 4 meses[mes] == 6 meses[mes] == 7 meses[mes] == 9 meses[mes] == 11 ){
if (numero_dias < -1 || numero_dias > 31){
continue;
}else{
break;
}
}
If I understand your question correctly, you want to:
Enter the number of days you worked in a month;
Check that the number of days you entered does not exceed the number of days for that month.
So, if you're entering the number of days you worked for the month of January, that number has to be between 0 and 31 (I'm assuming it's possible you can work zero days in a month). For the month of February it has to be between 0 and 28 (29 in a leap year), for April it has to be between 0 and 30, etc.
An easy way to do this is to set up an array to keep track of the number of days per month, indexed by the month number (0 for January, 1 for February, etc.):
/**
* We're declaring this const so that we can't accidentally change any
* of the entries in the array.
*/
const int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
We'll have to add some special-case processing for February to deal with leap years, but for this first pass we won't worry about it.
It may also be useful to have another array1 for the month names, since you try to display that in your loop:
const char *names[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
So now your loop becomes:
for (mes = 0; mes < 12; mes++ )
{
do
{
printf( "Digite o numero de dias trabalhados no mes %s: ", names[mes] );
scanf ( "%d", &numero_dias );
} while ( numero_dias < 0 || numero_dias > days[mes] );
// do something with numero_dias
}
The inner do loop will repeat while numero_dias is outside the range of 0 and days[mes]. So when mes is 0, days[mes] is 31, when mes is 3, days[mes] is 30, etc.
February (1) is a special case - it can be either 28 or 29 days. So we need to modify the condition of the do loop slightly:
do
{
...
} while ( numero_dias < 0 || numero_dias > ( mes != 1 ? days[mes] : feb_days() ) );
The expression mes != 1 ? days[mes] : feb_days() checks to see if mes corresponds to February (1) - if it doesn't, then we use the value in days[mes] (January, March, April, May, etc.). If it does, then we call another function feb_days() that returns the number of days in February for the current year.
So the condition in the do-while loop now checks to see if numero_dias is less than zero or greater than the number of days for the current month, with a special case for February.
To get the number of days in February for the current year, you can do the following:
int feb_days( void )
{
time_t now = time( NULL ); // Get number of seconds since 1970-01-01
struct tm *utc = gmtime( &now ); // Break time into year, month, day, etc.
int days = 28; // default number of days in Feb
return days + (utc->tm_year % 4 == 0 && (utc->tm_year % 100 != 0 || utc->tm_year % 400 == 0 ));
}
The expression (utc->tm_year % 4 == 0 && (utc->tm_year % 100 != 0 || utc->tm_year % 400 == 0 )) will evaluate to 1 if the current year is divisible by 4 and it's not divisible by 100 or it is divisible by 400. If any of those conditions are not true, then it evaluates to 0. So if this is not a leap year, we return 28 + 0 == 28 days. If it is a leap year, we return 28 + 1 == 29 days.
There's a lot more bulletproofing that needs to be done - you need to check the return value of scanf to make sure your input was actually read correctly (and take action to clear the input stream if it wasn't), you probably want to give your user a way to cancel the input operation, etc., but this should get you started.
Actually, we'd use a single array of a struct type instead of separate arrays, but I'm trying to keep things basic.

Calculate the date after X days with a user input date

First and foremost, I am new so I am sorry for any incorrect terms I use for explaining. While I do speak English quite well I don't learn C in English,so I'll do my best to translate and understand correctly.
I am trying to write a program where the program asks a user to input a date (0<= year <=9999) and the programs then validates if that is a logical date (including leap years). if it's not logical the program asks to input a date again, until it is. When it's a logical date the program then prints it and asks the user to input a number (-1000000 <= N <= 1000000) of days and then print the date after\before the N amount of days.
I have the Date code pretty much set up,though I can't seem to get the while loop to return to the beginning when the date is wrong.
I also am trying to figure out a an algorithm to calculate the date after\before N days.
sorry for going on so long and thank you for all helpers/insights.FYI because this is a first assignment I am not allowed to use arrays or strings, so I understand the code will be bulky
void main()
{//d=day m=month y=year
int dd, mm, yy, flag = 0;
printf("Enter day month and year:\n");
scanf("%d\n%d\n%d", &dd, &mm, &yy);
//year check
while (flag==0)
{
if (yy >= 0 && yy <= 9999 && mm >= 1 && mm <= 12 && dd >= 1 && dd <= 31);
{
//month check
if (mm >= 1 && mm <= 12);
{
//day check
if ((dd >= 1 && dd <= 31) && (mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10 || mm == 12))
printf("\n%d\n%d\n%d", dd, mm, yy);
else
if ((dd >= 1 && dd <= 30) && (mm == 34 || mm == 6 || mm == 9 || mm == 1))
printf("\n%d\n%d\n%d", dd, mm, yy);
//leap year check
if ((dd >= 1 && dd <= 28) && mm == 2)
printf("\n%d\n%d\n%d", dd, mm, yy);
else
if ((dd == 29) && mm == 2 && (yy % 400 == 0 || yy % 100 != 0 && yy % 4 == 0))
printf("\n%d\n%d\n%d", dd, mm, yy);
}
}
}(flag = 1);
}

C problem requires to find students semester propably from year and age

I have an exercise where I have as input the name, the age and the current university year of whoever, and it asks the minimum years for him to graduate, the person's age when he graduates and how many semesters till the graduation. How can we find the semester the person is in? Thought I had found a way but it duplicates logical expressions so it's not corrects.
/*My attempt*/
if(age % 2 == 1 && year == 1 || age % 2 == 0 && year == 1)
semester = 1;
else if(age % 2 == 0 && year == 1 || age % 2 == 1 && year == 1)
semester = 2;
else if(age % 2 == 0 && year == 2 || age % 2 == 1 && year == 2)
semester = 3;
else if(age % 2 == 1 && year == 2 || age % 2 == 0 && year == 2)
semester = 4;
else if(age % 2 == 1 && year == 3 || age % 2 == 0 && year == 3)
semester = 5;
else if(age % 2 == 0 && year == 3 || age % 2 == 1 && year == 3)
semester = 6;
else if(age % 2 == 0 && year == 4 || age % 2 == 1 && year == 4)
semester = 7;
else if(age % 2 == 1 && year == 4 || age % 2 == 0 && year == 4)
semester = 8;

Resources