Alright so this is a code from Jupyterlab. And i wanted to know why the int day wont respond to the changes made to the value? The code conering starts from line 37 at //day (January).
#include <stdio.h>
int main() {
int year = 2021;
int month = 1;
int day = 32;
//printf("Enter an integer: ");
//scanf("%d", &year);
// true if number is less than 0
//Year
if (year > 10000 || year < 1) {
printf("Error: Invalid year, ");
}else{
if(year <= 10000 || year >= 1){
printf("%d, ", year);
}
}
//Month
if (month > 12 || month < 1) {
printf("Error: Invalid month\n");
}else{
if(month <= 12 || month >= 1){
printf("%d\n", month);
}
}
//day (January)
if ((month == 1) && (day > 1 || day < 31)) {
printf("%d\n", day);
}else{
if ((month == 1) && (day < 1 || day > 31)) {
printf("Error: Invalid day\n");
}
}
//Febuary
if ((year%400 == 0) && (month == 2) && (day > 1 || day < 29)) {
printf("%d\n", day);
}
if ((year%400 == 0) && (month == 2) && (day < 1 || day > 29)) {
printf("Error: Invalid day\n");
}else{
if ((month == 2) && (day > 1 || day < 28)) {
printf("%d\n", day);
}
if ((month == 2) && (day < 1 || day > 28)) {
printf("Error: Invalid day\n");
}
}
}
Why is it that from //day (January) the code won't respond to the changes made to the int day? When i enter the date int = 32; it just prints out 32 and not the Error: Invalid day? Why is that and what did i do wrong? [PS. i only have for now January and February...]
Because 32 is bigger than 1. You go right into the if statement on day > 1. You need (day >= 1 && day <= 31)
Related
I dont know why my for loop is not working. I have been trying for so long. It just runs for the do / while and gives return. Everything in the do / while loop. I have declared a macro outside the main function and I am using it as loop condition. I can't add more details, all I have is code.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define MIN_YEAR 2012
#define MAX_YEAR 2022
#define LOG_DAYS 3
int main(void)
{
const int JAN = 1;
const int DEC = 12;
int year = 0;
int month = 0;
char day1 = 01;
char day2 = 02;
char day3 = 03;
float ratingMorning = 0;
float ratingEvening = 0;
printf("General Well-being Log\n"
"======================\n");
do
{
printf("Set the year and month for the well-being log (YYYY MM): ");
scanf(" %d %d", &year, &month);
if ((MIN_YEAR > year || year > MAX_YEAR))
{
printf(" ERROR: The year must be between 2012 and 2022 inclusive\n");
}
if ((JAN > month || month > DEC))
{
printf(" ERROR: Jan.(1) - Dec.(12)\n");
}
if ((JAN <= month && month <= DEC) && (MIN_YEAR <= year && year <= MAX_YEAR))
{
printf("\n*** Log date set! ***\n\n");
break;
}
} while ((MIN_YEAR <= year || year <= MAX_YEAR) && (JAN <= month || month <= DEC));
for (int i = 0; i < LOG_DAYS; i++)
{
printf(" %d", year);
printf("-");
printf(" %d", month);
printf("-");
printf(" %d",day1);
printf(" Morning rating (0.0-5.0): ");
scanf(" %d", &ratingMorning);
if (ratingMorning < 0 || ratingMorning > 5)
{
printf(" ERROR: Rating must be between 0.0 and 5.0 inclusive!");
}
printf(" Evening rating (0.0-5.0): ");
scanf(" %d", &ratingEvening);
if (ratingEvening < 0 || ratingEvening > 5)
{
printf(" ERROR: Rating must be between 0.0 and 5.0 inclusive!");
}
}
return 0;
}
The test in the while part of the do / while loop is incorrect. It is much simpler to use a for(;;) loop and break when the input is correct, avoiding redundant and sometime inconsistent tests.
The scanf() conversion specifier for float is %f, not %d.
Here is a modified version:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define MIN_YEAR 2012
#define MAX_YEAR 2022
#define LOG_DAYS 3
int main(void) {
const int JAN = 1;
const int DEC = 12;
int year = 0;
int month = 0;
char day1 = 1;
char day2 = 2;
char day3 = 3;
float ratingMorning = 0;
float ratingEvening = 0;
printf("General Well-being Log\n"
"======================\n");
for (;;) {
printf("Set the year and month for the well-being log (YYYY MM): ");
if (scanf(" %d %d", &year, &month) != 2) {
printf("invalid input\n");
return 1;
}
if (year < MIN_YEAR || year > MAX_YEAR) {
printf(" ERROR: The year must be between 2012 and 2022 inclusive\n");
} else
if (month < JAN || month > DEC)) {
printf(" ERROR: Jan.(1) - Dec.(12)\n");
} else {
printf("\n*** Log date set! ***\n\n");
break;
}
}
for (int i = 0; i < LOG_DAYS; i++) {
printf(" %d- %d- %d", year, month, day1);
printf(" Morning rating (0.0-5.0): ");
if (scanf("%f", &ratingMorning) != 1) {
printf("invalid input\n");
return 1;
}
if (ratingMorning < 0 || ratingMorning > 5) {
printf(" ERROR: Rating must be between 0.0 and 5.0 inclusive!");
continue;
}
printf(" Evening rating (0.0-5.0): ");
if (scanf("%f", &ratingEvening) != 1) {
printf("invalid input\n");
return 1;
}
if (ratingEvening < 0 || ratingEvening > 5) {
printf(" ERROR: Rating must be between 0.0 and 5.0 inclusive!");
continue;
}
// ... handle the ratings and report success
}
return 0;
}
If the problem is that:
if (ratingMorning < 0 || ratingMorning > 5) is not working
i.e. the error line ERROR: Rating must be between 0.0 and 5.0 inclusive!" is never printed, then when you change the %d to %f in the scanf(" %d", &ratingMorning); lines, the errors are printed because now ratingMorning and ratingEvening contain valid values, whereas when they were input as integers, the values were invalid.
I'm trying to build a program that will print out the month and year that the user inputs. What I have so far ends up with the year having 5 digits and inputting "2020 2" results in 28 days for the month of February.
The code I have so far in this assignment:
#include <stdio.h>
#include<stdlib.h>
int is_leapyear(int y)
{
if ((y % 100 == 0 && y % 100 != 0) || y % 400 == 0)
return 1;
else
return 0;
}
int main()
{
int i, j, year, month;
printf("Please enter the year and month:");
scanf("%d", &year);
scanf("%d", &month);
int a[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int la[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };
int c = 1;
int flag = 0;
if (is_leapyear(year))
flag = 1;
for (i = 0; i<month; i++)
{
if (flag == 1)
c += la[i];
else
c += a[i];
}
int s = (year - 1) + ((year - 1) / 4) + ((year - 1) / 400) - ((year - 1) / 100) + c;
int week = s % 7;
printf("Calendar " );
printf("%-d", year);
if (month < 10)
printf("0");
printf(" %d\n", month);
printf("----------------------\n");
printf("Su Mo Tu We Th Fr Sa \n");
printf("----------------------\n");
if (flag == 1) {
for (j = 0; j < week; j++)
{
printf(" ");
}
for (i = 1; i <= la[month]; i++)
{
if ((week + i - 1) % 7 == 0)
{
printf("\n");
}
if (i < 10)
{
printf(" ");
}
printf("%d", i);
printf(" ");
}
}
else {
for (j = 0; j < week; j++)
{
printf(" ");
}
for (i = 1; i <= a[month]; i++)
{
if ((week + i - 1) % 7 == 0)
{
printf("\n");
}
if (i < 10)
{
printf(" ");
}
printf("%d", i);
printf(" ");
}
}
printf("\n");
printf("---------------------\n");
}
The output:
Any help would be greatly appreciated.
Edit: It seems like I was too careless in reviewing my code, minor mistakes observed and problem solved!
The century check on your isleapyear()
if ((y % 100 == 0 && y % 100 != 0) || y % 400 == 0)
is flawed. It checked if the year is divisible by 100 and not divisible by 100, which is a logical impossibility. I think you actually mean
if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
So check if the year is divisible by 4 and not divisible by 100.
As for the 20200, it's because your leading zero for the month is printed before the space is printed. Just change it to
printf("%-d", year);
printf(" "); //put space right away
if (month < 10)
printf("0");
printf("%d\n", month); //no need to print space here
or even better as pointed out by #JonathanLeffler in the comment
printf(" %.2d\n", month);
It deals with space, the leading zero, and the new line in just one call, or just
printf("Calendar %.4d %.2d\n", year, month);
to handle those three parts at once
OP's is_leapyear(int y) fails with various multiples of 4.
Instead:
// Alternative for Gregorian calendar
int is_leapyear(int y) {
if (y % 4 == 0) {
if (y % 100 == 0) { // A century year?
return (y % 400 == 0);
}
return 1;
}
return 0;
}
Deeper: Leap year.
I am somewhat new to C. I'm making a calendar for a full year and storing it into a file. My main problem is that whenever I try and make a file it comes out blank. I've been trying to figure this out for a while and decided to finally ask. Can someone please show me what I am doing wrong? Thanks in advance!
#include <stdio.h>
#include <stdbool.h>
#define MIN_YEAR 2000
#define MAX_YEAR 3000
#define JANUARY 1
#define FEBRUARY 2
#define MARCH 3
#define APRIL 4
#define MAY 5
#define JUNE 6
#define JULY 7
#define AUGUST 8
#define SEPTEMBER 9
#define OCTOBER 10
#define NOVEMBER 11
#define DECEMBER 12
bool getContinueChoice();
int getYear ();
int getStartingDay (int year);
bool isLeapYear (int year);
void makeFile (int year, int startingDay);
void makeMonthHeading (int month, int year, FILE *file);
int getNumberOfDays (int month, int year);
int main()
{
int startingDay,
year;
printInstructions();
while(getContinueChoice() == true)
{
year = getYear();
startingDay = getStartingDay(year);
makeFile(year, startingDay);
printf("\nCalendar made for the year %d.\n", year);
printf("Look for the file called file.txt to see it.\n");
}
printGoodbye();
return 0;
}
bool getContinueChoice()
{
char getChoice;
bool choice;
scanf(" %c", &getChoice);
while(getChoice != 'y' || 'n')
{
printf("\n***************************************\n");
printf("* Error: That was not a (y/n) answer. *\n");
printf("***************************************\n\n");
printf("Would you like to print a calendar? (y/n): ");
scanf(" %c", &getChoice);
}
if(getChoice == 'y')
{
choice = true;
}
else
{
choice = false;
}
return choice;
}
int getYear()
{
int userYear;
printf("\nPlease enter a year.\n");
scanf("%d", &userYear);
return userYear;
}
int getStartingDay(int year)
{
int firstDay;
firstDay = (((year - 1) * 365) + ((year - 1) / 4) - ((year - 1) / 100) +
((year - 1) / 400) + 1) % 7;
return firstDay;
}
bool isLeapYear(int year)
{
bool leapYear;
if((!(year % 4) && (year % 100)) || !(year % 400))
{
leapYear = true;
}
return leapYear;
}
void makeFile(int year, int startingDay)
{
int daysInMonth;
FILE *file;
file = fopen("file.txt","w");
for(int month = JANUARY; month <= DECEMBER; month++)
{
makeMonthHeading(month, year, file);
fprintf(file, "SUN MON TUE WED THU FRI SAT\n");
for(int day = 1; day <= 1 + startingDay * 5; day++)
{
printf(" ");
daysInMonth = getNumberOfDays(month, year);
for(int days = 1; days <= daysInMonth; days++)
{
fprintf(file, "%2d", days);
if((days + startingDay) % 7 > 0)
{
printf(" ");
}
else
{
printf("\n");
}
}
}
}
printf("Calendar made for the year %d", year);
printf("\nLook for the file called file.txt to see it.\n\n");
getContinueChoice();
return;
}
void makeMonthHeading(int month, int year, FILE *file)
{
if(month == JANUARY)
{
fprintf(file, "JANUARY %d\n", year);
}
else if(month == FEBRUARY)
{
fprintf(file, "\nFEBRUARY %d\n", year);
}
else if(month == MARCH)
{
fprintf(file, "\nMARCH %d\n", year);
}
else if(month == APRIL)
{
fprintf(file, "\nAPRIL %d\n", year);
}
else if(month == MAY)
{
fprintf(file, "\nMAY %d\n", year);
}
else if(month == JUNE)
{
fprintf(file, "\nJUNE %d\n", year);
}
else if(month == JULY)
{
fprintf(file, "\nJULY %d\n", year);
}
else if(month == AUGUST)
{
fprintf(file, "\nAUGUST %d\n", year);
}
else if(month == SEPTEMBER)
{
fprintf(file, "\nSEPTEMBER %d\n", year);
}
else if(month == OCTOBER)
{
fprintf(file, "\nOCTOBER %d\n", year);
}
else if(month == NOVEMBER)
{
fprintf(file, "\nNOVEMBER %d\n", year);
}
else if(month == DECEMBER)
{
fprintf(file, "\nDECEMBER %d\n", year);
}
return;
}
int getNumberOfDays(int month, int year)
{
int monthDays;
if(month == JANUARY)
{
monthDays = 31;
}
else if(month == FEBRUARY)
{
if(isLeapYear(year) == true)
{
monthDays = 29;
}
else
{
monthDays = 28;
}
}
else if(month == MARCH)
{
monthDays = 31;
}
else if(month == APRIL)
{
monthDays = 30;
}
else if(month == MAY)
{
monthDays = 31;
}
else if(month == JUNE)
{
monthDays = 30;
}
else if(month == JULY)
{
monthDays = 31;
}
else if(month == AUGUST)
{
monthDays = 31;
}
else if(month == SEPTEMBER)
{
monthDays = 30;
}
else if(month == OCTOBER)
{
monthDays = 31;
}
else if(month == NOVEMBER)
{
monthDays = 30;
}
else if(month == DECEMBER)
{
monthDays = 31;
}
return monthDays;
}
You have two issues on the same line:
while(getChoice != 'y' || 'n')
This does not do what you think it does. Let's work on an example, where getChoice = 'a'
First operation getChoice != 'y' will evaluate to TRUE because 'a' != 'y'. Remember you are getting TRUE out of this operation.
Now, you are left with TRUE || 'n' which always be TRUE
So, your while condition will always be TRUE and this is why you are stuck in that loop.
If you fix that with
while((getChoice != 'y') || (getChoice != 'n'))
You will realize your second problem with that line, which is your logic.
Imagine if the user enters 'y'. Then what happens?
(getChoice != 'y') will be FALSE, but (getChoice != 'n') will be TRUE, so your while condition will be TRUE and you will be stuck in that loop again.
Think about other scenarios and then you will come to a conclusion that you need to use && instead of ||.
So, change that line to:
while((getChoice != 'y') && (getChoice != 'n'))
Also, as #purec said, you need to fclose() the file once you are done with it.
I'm trying to make a program that will print out the month from the given year and month that the user inputs. What I have so far just ends up printing out the entire calendar year.
#include<stdio.h>
int determineleapyear(int year);
int days_in_month[]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
char *months[]=
{
" ",
"\n\n\nJanuary",
"\n\n\nFebruary",
"\n\n\nMarch",
"\n\n\nApril",
"\n\n\nMay",
"\n\n\nJune",
"\n\n\nJuly",
"\n\n\nAugust",
"\n\n\nSeptember",
"\n\n\nOctober",
"\n\n\nNovember",
"\n\n\nDecember"
};
int determinedaycode(int year)
{
int d1, d2, d3;
d1 = year/4.0;
d2 = year/100.;
d3 = year/400.;
return 1 + (year + d1 + d2 + d3 + 1) % 7;
}
int determineleapyear(int year)
{
return (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0));
}
void calendar(int year, int month, int daycode)
{
int day;
printf("%s", months[month]);
printf("\n\nSun Mon Tue Wed Thu Fri Sat\n");
//Correct the position for the first date
for (day = 1; day <= 1 + daycode * 5; day++)
{
printf(" ");
}
//Print all the dates for one month
for (day = 1; day <= days_in_month[month]; day++)
{
printf("%2d", day);
//Is day before Sat? Else start next line Sun.
if ((day + daycode) % 7 > 0)
printf(" ");
else
printf("\n ");
}
// Set position for next month
daycode = (daycode + days_in_month[month]) % 7;
if (determineleapyear(year))
days_in_month[2] = 29;
printf("\n %s %d\n", months[month], year);
}
//------------------------------------------
int main() {
int daycode, month, year;
printf("\nEnter the month: ");
scanf("%d", &month);
if (month < 1 || month > 12)
{
printf("Error: month must be in range 1..12");
return printf("\nEnter the month: ");
scanf("%d", &month);
}
printf("\nEnter the year: ");
scanf("%d", &year);
if (year < 0)
{
printf("Error: year must be > 0");
return scanf("%d", &year);
}
daycode = determinedaycode(year);
calendar(year, month, daycode);
printf("\n");
return 0;
}
Can anyone help me out with making it print just one month?
Your calendar function is running a loop from 1 to 12, which is causing all months to be printed. Remove the loop and replace it with a parameter to allow the user to select a month:
void calendar(int year, int month, int daycode)
{
int day;
printf("%s", months[month]);
printf("\n\nSun Mon Tue Wed Thu Fri Sat\n");
//Correct the position for the first date
for (day = 1; day <= 1 + daycode * 5; day++)
{
printf(" ");
}
//Print all the dates for one month
for (day = 1; day <= days_in_month[month]; day++)
{
printf("%2d", day);
//Is day before Sat? Else start next line Sun.
if ((day + daycode) % 7 > 0)
printf(" ");
else
printf("\n ");
}
// Set position for next month
daycode = (daycode + days_in_month[month]) % 7;
}
Call it in main using the variable read from input:
calendar(year, month, daycode);
I would also highly recommend validating input. Something like the following:
printf("\nEnter the month: ");
scanf("%d", &month);
if (month < 1 || month > 12)
{
printf("Invalid month selected");
return -1;
}
printf("Please enter the start date of your trip Month/Day/Year seperated by a space:");
scanf("%d %d %d", &month, &day, &year);
checkC = error_date(month,day,year);
if (checkC == 2)
{
travel_month[i][0] == month;
travel_day[i][0] == day;
travel_year[i][0] == year;
}
else
while (checkC==1)
{
printf("Please enter the start date of your trip Month/Day/Year seperated by a space:");
scanf("%d %d %d", &month, &day, &year);
checkC= error_date(month,day,year);
}
for (row = 0; row < trip_num; row++)
{
for (col=0; col < DEST; col++)
printf("Trip#:%d %d/%d/%d\n", row+1, travel_month[row][col], travel_day[row][col], travel_year[row][col]);
}
return 0;
}
int error_date(int month, int day, int year)
{
int checkC;
if ( ((month > 0) && (month <= 12)) && ((day > 0) && (day <= 31)) && ((year> 2000) && (year < 2050)) )
{
checkC = 2;
return checkC;
}
else
{
printf("Invalid date please re-enter date\n");
checkC = 1;
return checkC;
}
}
Am I loading the month/day/year wrong, I keep getting weird integers when I'm printing out the array.
You need to use single equals sign for assignment:
travel_month[i][0] == month;
travel_day[i][0] == day;
travel_year[i][0] == year;
should be
travel_month[i][0] = month;
travel_day[i][0] = day;
travel_year[i][0] = year;