Requirements: (a) Have the program print an error message and ignore a reminder
ID: 3533925 • Letter: R
Question
Requirements: (a) Have the program print an error message and ignore a reminder if the corrsponding day is negative or larger than 31 (using the continue statement)
(b) Allow the user to enter a day, a 24-hout time, and a reminder. The printed reminder list should be sorted first by day, then by time. (The original program allows the user to enter a time, but it's treated as part of the reminder)
(c) Have the program print a one-year reminder list. require the user to enter days in the form month/day.
Here is the given program (I sure hope this pastes correctly):
#include "remind.h"
/* Prints a one-month reminder list */
#include <stdio.h>
#define MAX_REMIND 50 /* maximum number of reminders */
#define MSG_LEN 60 /* max length of reminder message */
int read_line(char str[], int n);
int main(void)
{
char reminders[MAX_REMIND][MSG_LEN+3];
char day_str[3], msg_str[MSG_LEN+1];
int day, i, j, num_remind = 0;
for (;;)
{
if (num_remind == MAX_REMIND)
{
printf("-- No space left -- ");
break;
}
printf("Enter day and reminder: ");
scanf("-", &day);
if (day == 0)
break;
sprintf(day_str, "-", day);
read_line(msg_str, MSG_LEN);
for (i = 0; i < num_remind; i++)
if (strcmp(day_str, reminders[i]) < 0)
break;
for (j = num_remind; j > i; j--)
strcpy(reminders[j], reminders[j-1]);
strcpy(reminders[i], day_str);
strcat(reminders[i], msg_str);
num_remind++;
}
printf(" Day Reminder ");
for (i = 0; i < num_remind; i++)
printf(" %s ", reminders[i]);
return 0;
}
int read_line(char str[], int n)
{
int ch, i = 0;
while ((ch = getchar()) != ' ')
if (i < n)
str[i++] = ch;
str[i] = '';
return i;
}
Explanation / Answer
#include<stdio.h>
#include<String.h>
#define MAX_REMIND 50
#define MSG_LEN 60
int read_line(char[], int n);
int main (){
printf("hello ");
char reminders[MAX_REMIND][MSG_LEN+3 ];
char day_str[3], msg_str[MSG_LEN +1]="";
int day, i, j, num_remind = 0;
for(;;){
if(num_remind == MAX_REMIND){
printf("--No space left-- ");
break;
}
printf("Enter day and reminder: ");
scanf("-",&day);
if(day<=0 || day >31)
continue ;
sprintf(day_str,"-",day);
printf("hi");
read_line(msg_str,MSG_LEN);
for(i=0;i<num_remind;i++)
if(strcmp(day_str,reminders[i])<0)
break;
for(j=num_remind;j>1;j--)
strcpy(reminders[j],reminders[j-1]);
strcpy(reminders[i],day_str);
strcat(reminders[i],msg_str);
num_remind++;
}
printf(" Day Reminder ");
for(i=0;i<num_remind;i++)
printf("%s ",reminders[i]);
return 0 ;
}
int read_line(char str[],int n){
int ch, i=0;
while( (ch=getchar())!=' ' )
if(i<n)
str[i++]=ch;
str[i]='';
return 0 ;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.