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.
Related
my program works on all online compilers but not on visual studios. However whenever I try to run it on visual studios 2019, it doesn't run and gives me 2 errors. error2440 and error2447. I have tried to fix it but nothing seems to work if anyone has any idea on what to do it would be appreciated the code:
EDIT: after putting const in front of char I did not get any errors but I did get multiple warnings. the warnings are:
warning C4101: 'year': unreferenced local variable
warning C4101: 't': unreferenced local variable
warning C4101: 'r': unreferenced local variable
warning C4101: 'weekdays': unreferenced local variable
warning C4101: 'leapyear': unreferenced local variable
error C4700: uninitialized local variable 'year' used
error C4700: uninitialized local variable 'firstday' used
error C4700: uninitialized local variable 'p' used
error C4716: 'printday': must return a value
error C4700: uninitialized local variable 'year' used
#include<stdio.h>
#include<string.h>
int month_days[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
char* month_name[] =
{
" ",
"\n\n\n\t\t1",
"\n\n\n\t\t2",
"\n\n\n\t\t3",
"\n\n\n\t\t4",
"\n\n\n\t\t5",
"\n\n\n\t\t6",
"\n\n\n\t\t7",
"\n\n\n\t\t8",
"\n\n\n\t\t9",
"\n\n\n\t\t10",
"\n\n\n\t\t11",
"\n\n\n\t\t12"
};
int get_first_start_day(int year)
{
int start_day;
int x, y, z;
x = (year - 1) / 4;
y = (year - 1) / 100;
z = (year - 1) / 400;
start_day = (year + x - y + z) % 7;
return start_day;
}
void set_feb_days(int year)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
month_days[2] = 29;
else
month_days[2] = 28;
}
void display_month_calendar(int days, int start_day)
{
int month, year;
for (month = 1; month <= 12; month++)
{
printf("%s", month_name[month]);
printf("\n\nSun Mon Tue Wed Thu Fri Sat\n");
for (days = 1; days <= 1 + start_day * 5; days++)
{
printf(" ");
}
for (days = 1; days <= month_days[month]; days++)
{
printf("%2d", days);
if ((days + start_day) % 7 > 0)
printf(" ");
else
printf("\n ");
}
start_day = (start_day + month_days[month]) % 7;
}
}
int printday(void)
{
int month[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
char week[7][10];
int date, mon, year, i, t, r, s = 0;
strcpy(week[0], "Sunday");
strcpy(week[1], "Monday");
strcpy(week[2], "Tuesday");
strcpy(week[3], "Wednesday");
strcpy(week[4], "Thursday");
strcpy(week[5], "Friday");
strcpy(week[6], "Saturday");
printf("Please Enter the date and month: ");
scanf("%d%d", &date, &mon);
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
month[1] = 29;
for (i = 0; i < mon - 1; i++)
s = s + month[i];
s = s + (date + year + (year / 4) - 2);
s = s % 7;
printf("\n%d/%d is a %s\n", date, mon, week[s]);
}
void occurrenceDays(int p, char firstday)
{
int year;
int monthdays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
firstday = get_first_start_day(year);
char days[7][10];
strcpy(days[0], "Sunday");
strcpy(days[1], "Monday");
strcpy(days[2], "Tuesday");
strcpy(days[3], "Wednesday");
strcpy(days[4], "Thursday");
strcpy(days[5], "Friday");
strcpy(days[6], "Saturday");
int count[7];
for (int i = 0; i < 7; i++)
count[i] = 4;
int pos;
for (int i = 0; i < 7; i++)
{
if (firstday == days[i][10])
{
pos = i;
break;
}
}
int inc = monthdays[p] - 28;
for (int i = pos; i < pos + inc; i++)
{
if (i > 6)
count[i % 7] = 5;
else
count[i] = 5;
}
int weekdays = monthdays[p] - count[0] - count[6];
printf("Their are %d sundays,%d Saturdays and %d weekdys in the month\n", count[0], count[6],
weekdays);
}
int main(void)
{
int year, start_day, leapyear, t, p, firstday, weekdays;
int month;
printf("Please Enter a Four Digit Year : ");
scanf("%d", &year);
start_day = get_first_start_day(year);
set_feb_days(year);
display_month_calendar(year, start_day);
printf("\n");
printf("Please Enter the Starting Day of the year (1=Sun, 7=Sat): 6 ");
printf("Please enter the month : ");
scanf("%d", &month);
occurrenceDays(p, firstday);
printday();
printf("Do you want to try it again?(1 to continue/0 to quit) :");
scanf("%d", &t);
while (t >= 0)
{
if (t == 1)
{
printday();
printf("Do you want to try it again?(1 to continue/0 to quit) :");
scanf("%d", &t);
}
else
{
}
break;
}
return 0;
}
however my error is here:
{
" ",
"\n\n\n\t\t1",
"\n\n\n\t\t2",
"\n\n\n\t\t3",
"\n\n\n\t\t4",
"\n\n\n\t\t5",
"\n\n\n\t\t6",
"\n\n\n\t\t7",
"\n\n\n\t\t8",
"\n\n\n\t\t9",
"\n\n\n\t\t10",
"\n\n\n\t\t11",
"\n\n\n\t\t12"
};
thank you for any help in advance
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've created a simple program for displaying college student data and counting the accumulation of score into a grade (A,B,C,D,E) and using struct (Im prohibited to create with Array of Struct), so the problem is one of the output which is "Grade" is giving (null) result when printed with %s and completely blank result when printed with %c. The type of the "Grade" data is char by the way. Here is the complete code.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <ctype.h>
struct student
{
char nim[11];
char name[100];
char subjectCode[5];
int sks;
char grade;
}studentScore[100];
bool cekKarakter(char input[])
{
for(int x = 0; x < strlen(input); x++)
{
if(isdigit(input[x]))
return false;
}
return true;
}
bool cekNumeric(char input[])
{
for(int x = 0; x < strlen(input); x++)
{
if(input[x] >= 48 && input[x] <= 57)
return false;
}
return true;
}
int main()
{
int n;
printf("Input number of Student Data: ");
scanf("%d", &n);
fflush(stdin);
printf("\n\n");
for(int i = 1; i <= n; i++)
{
do
{
printf("NIM [Hanya numerik][10 Digit]: ");
gets(studentScore[i].nim);
fflush(stdin);
}
while(strlen(studentScore[i].nim) != 10 ||
cekNumeric(studentScore[i].nim));
printf("\n");
do
{
printf("Name [Hanya karakter]: ");
gets(studentScore[i].name);
fflush(stdin);
}
while(strlen(studentScore[i].name) < 5 || strlen(studentScore[i].name) >
30 || cekKarakter(studentScore[i].name) == false);
printf("\n");
do
{
printf("Subject Code [Must 5 length]: ");
gets(studentScore[i].subjectCode);
fflush(stdin);
}
while(strlen(studentScore[i].subjectCode) != 5);
printf("\n");
do
{
printf("SKS [Min 2|Max 8]: ");
scanf("%d", &studentScore[i].sks);
fflush(stdin);
}
while(studentScore[i].sks < 2 || studentScore[i].sks > 8);
printf("\n");
int score[5];
int WeightGrade = 0;
printf("Input 5 College Subject Score:\n\n");
for(int z = 0; z < 5; z++)
{
do
{
printf("Input Score[%d][Must be between 0 and 100]: ", z + 1);
scanf("%d", &score[z]);
}
while(score[z] < 0 || score[z] > 100);
WeightGrade += score[z];
}
if(WeightGrade / 25 == 4)
{
studentScore[i].grade = 'A';
}
else if(WeightGrade / 25 >= 3 && WeightGrade / 25 < 4)
{
studentScore[i].grade = 'B';
}
else if(WeightGrade / 25 >= 2 && WeightGrade / 25 < 3)
{
studentScore[i].grade = 'C';
}
else if(WeightGrade / 25 >= 1 && WeightGrade / 25 < 2)
{
studentScore[i].grade = 'D';
}
else if(WeightGrade / 25 == 0)
{
studentScore[i].grade = 'E';
}
}
printf("\nStudent Data\n");
for(int i = 1; i <= n; i++)
{
printf("NIM: %s\nName: %s\nSubject Code: %s\nSKS: %d\nGrade: %s\n",
studentScore[i].nim,
studentScore[i].name,
studentScore[i].subjectCode,
studentScore[i].sks,
studentScore[i].grade);
}
getchar();
return 0;
}
This is the image of the program when it's running
(null) output result using %s as printed
In your code, WeightGrade is actually the sum of all grades, so for your example - WeightGrade=370.
370/25 doesn't fall within any of the options, so studentScore[i].grade doesn't get any value. To prevents this cases, always input an else clause that shows an error
Since you have 5 values, you should divide by 5*25=125, as in WeightGrade/125
Also for printf, you shouldn't use %s on char value, you should use %c
This code is used to get n dates and sort them in the ascending order. I think the getdates function is not working properly.
main function implementation:
#include <stdio.h>
#define max 30
int leapyear(int year);/*to check whether it's leap year or not*/
int dater(int x);/*to find the date from a month begin with the beginning of the year */
void getdates(int f);/*get the date in put*/
int *caldays(int p);
int n, i, q;
int t, d, leap;
int day[30];
int month[30];
int year[30];
char ca[30];
char cb[30];
int dd[30];
int da[30];
int j, a, x;
char c1, c2;
int dayn, monthn, yearn;
int main()
{
printf("Please enter the number of dates and Press ENTER to finish\n");
/*get the numbers of dates*/
scanf("%i", &n);
printf("You have entered %i \n", n);
getdates(n);
printf("end");
for (i = 0; i <= n-1; i++)
{
printf("end");
while (day[i] < 1 || day[i] > 31 || month[i] < 1 ||
month[i] > 12 || year[i] < 1 || year[i] > 10000)
{
fprintf( stderr, "The date entered is not right, please enter again\n");
printf("Please enter the number of dates and Press ENTER to finish\n");
/*get the numbers of dates*/
scanf("%i", &n);
printf("You have entered %i \n", n);
getdates(n);
}
while (month[i] == 2 && day[i] > 29)
{
fprintf( stderr, "The date entered is not right, please enter again\n");
printf("Please enter the number of dates and Press ENTER to finish\n");
/*get the numbers of dates*/
scanf("%i", &n);
printf("You have entered %i \n", n);
getdates(n);
}
while ((month[i] == 4 ||month[i] == 6 ||
month[i] == 9 ||month[i] == 11) && day[i] > 30)
{
fprintf( stderr, "The date entered is not right, please enter again\n");
printf("Please enter the number of dates and Press ENTER to finish\n");
/*get the numbers of dates*/
scanf("%i", &n);
printf("You have entered %i \n", n);
getdates(n);
}
}
/*3 while loops are used to give msg and re-enter again when get an error input*/
caldays(n);
for (i = 0; x < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (dd[i] > dd[j])
{
a = dd[i];
dd[i] = dd[j];
dd[j] = a;
}
/*sort the days in asending order in days array*/
}
}
printf("The %i dates in ascending order are\n", n);
for (i = 0; i < n; ++i)
printf("%d%c%d%c%d\n", day[i], ca[i], month[i], cb[i], year[i]);
/*print all the date in ascending order*/
}
/*find out wheter it's leap year or not*/
int leapyear(int year)
{
if (year % 4000 == 0)
{
return 1;
}
else if (year % 400 == 0)
{
return 1;
}
else if (year % 40 == 0)
{
return 1;
}
else if (year % 4 == 0)
{
return 1;
}
else
{
return 0;
}
}
/*find out the days for the month input*/
int dater(x)
{
int y=0;
switch(x)
{
case 1: y=0; break;
case 2: y=31; break;
case 3: y=59; break;
case 4: y=90; break;
case 5: y=120; break;
case 6: y=151; break;
case 7: y=181; break;
case 8: y=212; break;
case 9: y=243; break;
case 10:y=273; break;
case 11:y=304; break;
case 12:y=334; break;
default: fprintf( stderr, "the value entered is not right\n");
}
return y;
}
void getdates(int f)
{
for(i=0;i<f;i++)
{
q = i+1;
printf("Please Enter %i dates (DD/MM/YYYY or DD-MM-YYYY) and
Press ENTER to finish each one\n", n);
scanf("%d%c%d%c%d", &dayn, &c1, &monthn, &c2, &yearn);
printf("You have entered %i date %i%c%i%c%i\n", q, dayn, c1, monthn, c2, yearn);
day[i] = dayn;
month[i] = monthn;
year[i] = yearn;
ca[i] = c1;
cb[i] = c2;
}
return;
}
int *caldays(int p)
{
for (i=0; i < p-1; i++)
{
leap = leapyear(year[i]);
t = 0;
if(leap == 1)
{
t++;
}
/*if there is a leap add one day */
/*find for the days for month entered*/
d = dater(month[i]);
/* find out the total days from the date entered begin with 0 days*/
d = d + dayn + t + (yearn * 365);
dd[i] = d;/*put all the days in an array*/
}
return dd;
}
The mistake is instead of
caldays(n);
for (i = 0; x < n; ++i)
in main function, it should be
caldays(n);
for (i = 0; i < n; ++i)
In getdates() and wherever required, use "%d %c %d %c %d" instead of %d%c%d%c%d.
As soon as you get the program working,I suggest you to post it for review on https://codereview.stackexchange.com/. The experienced people there will help you tear apart your code and make your life easier for better understanding of readability of code.
I recommend you to read Difference between format specifiers %i and %d in printf.
While posting question do follow MCVE from next time if possible.
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;
}