Hey guys, I was wondering if you guys could take a look at my code. Basically, it's trying to simulate Linux's Cal function, and takes in the input, "MonthName Year", where monthname is a string, and year is a number.
The problem here is, getting the program to recoqnize monthname, so that I can turn it into the month's numberical value. (ie september = 9) But when I try to input a value, the program will not take in the entire string. It takes in part of it, 1 character, as seen by the following output:
F\uffff-1075508756 hello
January
S M T W T F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
I am completely baffled by it, so I was wondering if you guys had any suggestions? Here's the code I am using, you can pretty much ignore everything after the section where I get the time information.
Code:
#include <stdio.h>
#include <time.h>
#include <string.h>
int main (void)
{
time_t then;
struct tm *timeinfo;
struct tm *endofmonth;
int year, mon ,day, firstwday, count = 0, weekcount, lastday, daycount = 1;
char monthinput;
const char *months[] = {"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"};
/* prompts user for date */
scanf("%s %d", &monthinput, &year);
/*Converts user month input into an integer*/
while (count < 12) {
if (strcmp(months[count], &monthinput) == 0) {
mon = count + 1;
printf("%s bye \n", &monthinput);
}
count++;
}
printf("%s", &monthinput);
printf("%d hello \n", mon);
/* gets current timeinfo and modify it to user's choice */
time (&then);
timeinfo = localtime(&then);
timeinfo->tm_year = year - 1900;
timeinfo->tm_mon = mon - 1;
timeinfo->tm_mday = 1;
/* call mktime: timeinfo->tm_wday will be set */
mktime(timeinfo);
firstwday = timeinfo->tm_wday;
weekcount = firstwday;
/*gets current timeinfo and modify it to user's choice*/
endofmonth = localtime(&then);
endofmonth->tm_year = year - 1900;
endofmonth->tm_mon = mon;
endofmonth->tm_mday = 0;
/* call mktime: timeinfo->tm_wday will be set */
mktime (endofmonth);
lastday = endofmonth->tm_mday;
/*Prints the month*/
printf("%s\n", months[timeinfo->tm_mon]);
/*Prints the calender*/
printf(" S M T W T F S\n");
/*Change with for loop, better integration, less declarations!*/
while (firstwday > 0) {
printf(" ");
firstwday--;
}
while (daycount <= lastday) {
if (weekcount == 7){
printf(" \n");
weekcount = 0;
} else {
if (daycount <= 9) {
printf(" %d ", daycount);
daycount++;
weekcount++;
} else {
printf("%d ", daycount);
daycount++;
weekcount++;
}
}
}
printf("\n\n");
}