Monday 15 September 2014

c - Enhance 2 functions in order to give correct output -



c - Enhance 2 functions in order to give correct output -

i have written next console application asks user input day.

i need help in order improve give right reply days of week.

if user inputs other day apart monday, output "today", "yesterday", "tomorrow" respective days output below headings.

the problem seems mon not producing right output.

this code far:

#include "stdafx.h" #include <stdio.h> #include <string.h> typedef enum the_days {monday,tuesday,wednesday,thursday,friday,saturday,sunday, noday} day; day yesterday (day today); day tomorrow (day today); char thedays[][10] = {"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"}; day findday(char string1[]); void main(void) { day today; char theday[10]; puts("type day (e.g. 'monday'"); gets(theday); today = findday(theday); if(today == noday) { puts("error - invalid input - exiting"); return; } printf("today \tyesterday \ttomorrow\n" "============================================\n"); printf("%s\t %s \t %s\n", thedays[today], thedays[yesterday(today)],thedays[tomorrow(today)]); } day findday(char string1[]) { int = 0; day thisday; (i=0;i<7;i++) { if (!strcmp(thedays[i],string1)) { break; } } thisday = (day)i; homecoming thisday; } day yesterday(day today) { day before; before = (day)(today- 1); homecoming before; } day tomorrow(day today) { day after; after = (day)(today + 1); homecoming after; }

you wrong results mon , sunday because yesterday , tomorrow functions not taking care of wraparound. prepare these functions this:

day yesterday(day today) { day before = today > mon ? today - 1 : sunday; homecoming before; } day tomorrow(day today) { day after = today < sunday ? today + 1 : monday; homecoming after; }

c function

No comments:

Post a Comment