I was creating a program that keeps on running until you write "exit". When you enter a month, the program tells you what order the month name is. For example, I enter "January". The program will say "January is the first month.".However, this won't work and sometimes I will get a memory reference error. I tried switching my scanf() to gets() and to fgets(), and I also tried to remove the & sign from scanf(). Here's my code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <string.h>
int main(int argc, char**argv)
{
printf("***Hello!***\n");
//char monthes[] = {"JANUARY", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"};
while(1 < 5)
{
char monthName[20];
printf("Please enter the name of the month: ");
fgets(monthName, 20, stdin);
//char a = toupper(monthName);
if (strcmp(toUpperCase(monthName), "JANUARY") == 0) //1
{
printf("January is the first month.");
printf("This is from the January if statement.");
}
else if (strcmp(toUpperCase(monthName), "FEBRUARY") == 0) //2
{
printf("February is the second month.");
}
else if (strcmp(toUpperCase(monthName), "MARCH") == 0) //3
{
printf("March is the third month.");
}
else if (strcmp(toUpperCase(monthName), "APRIL") == 0) //4
{
printf("April is the fourth month.");
}
else if (strcmp(toUpperCase(monthName), "MAY") == 0) //5
{
printf("May is the fifth month.");
}
else if (strcmp(toUpperCase(monthName), "JUNE") == 0) //6
{
printf("June is the sixth month.");
}
else if (strcmp(toUpperCase(monthName), "JULY") == 0) //7
{
printf("July is the seventh month.");
}
else if (strcmp(toUpperCase(monthName), "AUGUST") == 0) //8
{
printf("January is the eighth month.");
}
else if (strcmp(toUpperCase(monthName), "SEPTEMBER") == 0) //9
{
printf("September is the nineth month.");
}
else if (strcmp(toUpperCase(monthName), "OCTOBER") == 0) //10
{
printf("October is the tenth month.");
}
else if (strcmp(toUpperCase(monthName), "NOVEMBER") == 0) //11
{
printf("November is the eleventh month.");
}
else if (strcmp(toUpperCase(monthName), "DECEMBER") == 0) //12
{
printf("December is the twelfth month.");
}
else if (strcmp(monthName, "exit") == 0)
{
printf("Bye!!!");
exit(0);
}
else
{
printf("Unknown month.");
}
}
return 0;
}
void toUpperCase(char word[])
{
int i = 0;
char chr;
while (word[i]) {
chr = word[i];
//printf("%c", toupper(chr));
i++;
}
}