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;
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.
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)
My task is to check if one student in the group is 100 days older than the other one. The maximum number of students is 1000. When calculating, leap years and other characteristics such as the number of days in month should be taken in account. I don't know why my code doesn't work.If I enter the wrong date(32/33/2000), I have an infinite loop with 'Wrong input'.If the date is correct,the code doesn't print anything even if the difference is 100 days. I am beginner, and I hope you could help.
#include <stdio.h>
int main() {
int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int daysleap[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
int s, month, year, day, i, j = 0, totaldays, a[1000];
do {
printf("Number of students: ");
scanf("%d", & s);
if (s < 1 || s > 100)
printf("Incorrect input");
} while (s < 1 || s > 1000);
for (i = 0; i < s; i++) {
scanf("%d,%d,%d", & day, & month, & year);
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
if (day < 1 || day > daysleap[month]) {
printf("Incorrect input");
i = i - 1;
continue;
} else if (month < 1 || month > 12) {
printf("Incorrect input");
i = i - 1;
continue;
} else {
for (i = 1; i < year; i++) {
totaldays = 0;
totaldays += 366;
}
for (i = 1; i < month; i++)
totaldays += daysleap[i];
totaldays += day;
a[j] = totaldays;
j++;
}
} else {
if (day < 1 || day > days[month]) {
printf("Incorrect input");
i = i - 1;
continue;
} else if (month < 1 || month > 12) {
printf("Incorrect input");
i = i - 1;
continue;
} else {
for (i = 1; i < year; i++) {
totaldays = 0;
totaldays += 365;
}
for (i = 1; i < month; i++)
totaldays += days[i];
totaldays += day;
a[j] = totaldays;
j++;
}
}
}
for (i = 0; i < s; i++) {
for (j = i; j < s; j++) {
while (i < s && j < s) {
if (i != j && (a[j] - a[i] == 100))
printf("Student %d is 100 days older from Student %d", j + 1, i + 1);
if (i != j && (a[j] - a[i] == -100))
printf("Student %d is 100 days older from Student %d", i + 1, j + 1);
}
}
}
}
This
for (i = 1; i < year; i++) {
totaldays = 0;
totaldays += 366;
}
Is equivalent to this
totaldays = 366;
You probably wanted to do this
totaldays = 0;
for (i = 1; i < year; i++) {
totaldays += 366;
}
However the logic is incorrect. Let's say that the year is 2000. It is a leap year, but 1999 is not. You should add 366 for leap years and 365 fon non-leap years.
I don't know if fixing it is enough for the program to work. Anyway I was eager to code and I made a program that relies on the difference between the dates as they are rather than on the difference between the dates as distance from the 1st January of the year 0. Check this approach.
#include <stdio.h>
#define N 50
int main() {
int m[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
int p[N][3]; // person: day, month, year
int n; // number of people
// if the date is in a leap year, then one of the two subscripts gets a value of 1
// if the date is before the 29th February leap[][0] = 1, otherwise leap[][1] = 1
int leap[N][2] = {};
int tot[N]; // sum of days in the birth year
int dif; // difference in days between two dates
puts("How many people?");
do {
scanf("%d", &n);
}
while( (n < 2 || n > N) && puts("Try again...") );
puts("Enter their birth days please.");
for(int i = 0; i < n; i++) {
if( scanf("%d%*c%d%*c%d", &p[i][0], &p[i][1], &p[i][2]) == 3 && p[i][0] > 0 && p[i][1] > 0 && p[i][1] < 13 ) {
if( (!(p[i][2]%4) && p[i][2]%100) || !(p[i][2]%400) ) {
if( p[i][1] == 0 || (p[i][1] == 2 && p[i][0] < 29) ) leap[i][0] = 1;
else leap[i][1] = 1;
}
if( ( leap[i][1] && ((p[i][1] == 1 && p[i][0] > 29) || (p[i][1] != 1 && p[i][0] > m[p[i][0]-1])) ) || (!leap[i][1] && p[i][0] > m[p[i][0]-1]) ) {
puts("Wrong input, try again");
--i;
}
}
else {
puts("Wrong input, try again");
--i;
}
}
for(int i = 0; i < n; i++) {
tot[i] = p[i][0];
for(int j = 0; j < p[i][1]-1; j++ ) {
tot[i] += m[j];
}
}
for(int i = 0; i < n-1; i++) {
if( p[i][2] - p[i+1][2] < - 1 ) dif = 999;
else if( p[i][2] - p[i+1][2] > 1 ) dif = -999;
else {
dif = tot[i+1] - tot[i];
if( leap[i][0] && leap[i+1][1] ) ++dif;
else if( leap[i+1][0] && leap[i][1] ) --dif;
if( p[i][2] - p[i+1][2] == - 1 ) dif += 365;
else if( p[i][2] - p[i+1][2] == 1 ) dif -= 365;
}
if( dif > 100) printf("Student #%d is 100 days older than student #%d", i+1, i+2);
else if( dif < 100) printf("Student #%d is 100 days older than student #%d", i+2, i+1);
}
return 0;
}
Yes it would be more readable using day[], month[] and year[] istead of p[][0], p[][1] and p[][2].
It would be even better to use an array of structures, like Date dates[N] with the type Date defined as follows
typedef struct {
int day, month, year;
int leapPre = 0, leapPost = 0;
int tot;
} Date;
With leapPre and leapPost being what leap[][0] and leap[][1] were.
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;
}