Hey everyone,
I tried to write a program wich scans some strings from stdi, stores them in arrays then from an array ("days") reads the chars and prints the day of the week according to the abreviation (MTWRF).
Code:
#include <stdio.h>
#include <string.h>
int abvToString(char days);
int main(int argc, const char *argv[])
{
char *dept[5];
char days[5];
char *wDays[] = {"Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays"};
int d, l, j;
int course_num;
int time;
//Getting input from user (MATH 1270 TR 800)
printf("Enter dept code, course number, days and time like so:\n>COSC 2060 MWF 1410\n>");
scanf("%s%d%s%d", dept, &course_num, days, &time);
printf("%s %d meets ", dept, course_num);
//Obtaining length of "days" array to loop from 0 to d
d = strlen(days);
for(j = 0; j <= d; j++){
//Return value is the index for "wDays" array
l = abvToString(days[j]);
printf("%s ", wDays[l]);
}
printf("at %d\n", time);
return 0;
}
int abvToString(char days){
const char abv[] = {'M', 'T', 'W', 'R', 'F'};
switch(days){
case 'M':
case 'm':
return 0;
break;
case 'T':
case 't':
return 1;
break;
case 'W':
case 'w':
return 2;
break;
case 'R':
case 'r':
return 3;
break;
case 'F':
case 'f':
return 4;
break;
default:
break;
}
}
Sample run:
[me@host C]$ ./fig9-2
Enter dept code, course number, days and time like so:
>COSC 2060 MWF 1410
>MATH 1270 TR 800
MATH 1270 meets Tuesdays Thursdays at 800
Two things I don't understand:
1. When I change
Code:
int main(int argc, const char *argv[])
to
I get MATH 1270 meets Tuesdays Thursdays H��� at 800 as output.
2. MATH 1270 meets Tuesdays Thursdays at 800
prints an extra blank space before the "at 800"
Its probably messy.
Is there a better way of solving the problem?
Any suggestion is welcome, thanks.