In: Computer Science
Hello,
Modify the grade book program from Assessment 3 to use a custom struct to hold the student's ID number and the percentage score for each item in the grade book. The program should accept the entry of ID numbers and percentage grades (0–100) until the user signals that he or she is done entering grades. The program should then print out the ID numbers and grades entered by the user, sorted by ID number. Again, be sure to format the code properly and provide comments in the C code.
Below is the program i have written, but it is not reading the date entered by the user and adding seven days to it. Instead the output is taking the the day in today's date (07/09/2020) and adding seven days to 09 and printing the date entered is whatever month I enter, but the day is always showing 16 (05/16/0000) and the new date adding seven is always showing the day of 23 (05/23/0000) and the year is always printing as zero. I would really like to understand what I have wrong in my code below please.
#include <stdio.h>
#include <stdbool.h>
//This is a global definition of struct for date so it can be
utilized for any function within the program.
struct date
{
int month;
int day;
int year;
};
//This will add the seven days to the date entered by the
user
struct date newDate (struct date entered)
{
struct date week;
int daysOfMonth (struct date d);
if(entered.day != daysOfMonth(entered))
{
week.day = entered.day+=7;
week.month = entered.month;
week.year = entered.year;
}
else if(entered.month == 12)//end of month
{
week.day = 1;
week.month = entered.month = 1;
week.year = entered.year +1;
}
else //end of year
{
week.day = 1;
week.month = entered.month+1;
week.year = entered.year;
}
return week;
}
//Function to find the number of days in a month
int daysOfMonth (struct date d)
{
//Declare variables to be used within function
int days;
bool flagLeapYear (struct date d);
const int daysOfMonth [12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (flagLeapYear (d) == true && d.month == 2)
days = 29;
else
days = daysOfMonth[d.month - 1];
return days;
}
//Function to determine if it is a leap year
bool flagLeapYear (struct date d)
{
bool flagLeapYear;
if ((d.year % 4 == 0 && d.year % 100 != 0) || d.year %
400 == 0)
flagLeapYear = true; //It is a leap year
else
flagLeapYear = false; // Not a leap year
return flagLeapYear;
}
int main(void)
{
struct date newDate (struct date today);
struct date entered, seven;
printf("Please enter a date in mm/dd/yyyy format: ");
scanf(" %d%d%d", &entered.month, &entered.day,
&entered.year);
seven = newDate (entered);
printf("\nThe date you entered is: %02d/%02d/%.4d",
entered.month, entered.day, entered.year);
printf("\nThe date in seven days will be: %02d/%02d/%.4d",
seven.month, seven.day, seven.year);
return 0;
}
Thank you,
Annette
input code:
output:
code:
#include <stdio.h>
#include <stdbool.h>
//This is a global definition of struct for date so it can be
utilized for any function within the program.
struct date
{
int month;
int day;
int year;
};
//This will add the seven days to the date entered by the
user
struct date newDate (struct date entered)
{
struct date week;
int daysOfMonth (struct date d);
/*if last month*/
if(entered.month == 12)//end of month
{
/*and value is go out of month*/
if(entered.day+7>daysOfMonth(entered))
{
/*do this*/
week.day = (entered.day+7)%(daysOfMonth(entered));
week.month = 1;
week.year = entered.year +1;
}
else
{
/*else do this*/
week.day = (entered.day+7);
week.month = entered.month;
week.year = entered.year ;
}
}
else //end of year
{
/*if day goes out of month*/
if(entered.day+7>daysOfMonth(entered))
{
/*than do this*/
week.day = (entered.day+7)%(daysOfMonth(entered));
week.month = entered.month+1;
week.year = entered.year ;
}
else
{
/*else do this*/
week.day = (entered.day+7);
week.month = entered.month;
week.year = entered.year;
}
}
return week;
}
//Function to find the number of days in a month
int daysOfMonth (struct date d)
{
//Declare variables to be used within function
int days;
bool flagLeapYear (struct date d);
/*declare the months value*/
const int daysOfMonth [12] = {31, 28, 31, 30, 31, 30, 31, 31, 30,
31, 30, 31};
if (flagLeapYear (d) == true && d.month == 2)
days = 29;
else
days = daysOfMonth[d.month - 1];
/*return month in days*/
return days;
}
//Function to determine if it is a leap year
bool flagLeapYear (struct date d)
{
/*declare the variables*/
bool flagLeapYear;
/*check leap year or not*/
if ((d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400
== 0)
flagLeapYear = true; //It is a leap year
else
flagLeapYear = false; // Not a leap year
return flagLeapYear;
}
int main(void)
{
/*declare the struct*/
struct date newDate (struct date today);
/*declare struct variables*/
struct date entered, seven;
/*take user input*/
printf("Please enter a date in mm/dd/yyyy format: ");
scanf(" %d%d%d", &entered.month, &entered.day,
&entered.year);
/*call method*/
seven = newDate (entered);
/*print output*/
printf("\nThe date you entered is: %02d/%02d/%.4d", entered.month,
entered.day, entered.year);
printf("\nThe date in seven days will be: %02d/%02d/%.4d",
seven.month, seven.day, seven.year);
return 0;
}
error: