-2

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++;
    }
}
zackBYE344
  • 63
  • 5
  • What is a memory reference error? Who tells you that? – Goswin von Brederlow Mar 12 '22 at 18:49
  • 1
    Don't forget that `fgets` keeps the newline (if any) in the input string. Please see [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221) – Weather Vane Mar 12 '22 at 19:04
  • 1
    Note that `toUpperCase()` doesn't do what it says, which is nothing. Some C implementations have `strcasecmp()` available to sidestep it, others have `strupr()` to do the uppercasing. – Weather Vane Mar 12 '22 at 19:05
  • 1
    `toUpperCase` also returns `void`, which clearly isn't going to work as part of an argument list to `strcmp`. Your compiler should already be barking at you about an implicit function declaration of `toUpperCase` as `int()`, and that warning in itself should be considered fatal. Turn up your warning levels and treat them *all* as errors (because that's exactly what they are). – WhozCraig Mar 12 '22 at 19:25
  • You should not be running this program. Compiling issues numerous warnings, and you should fix those before running the program. If you are compiling with GCC, start with `-Wall -Werror`. If you are compiling with Clang, start with `-Wmost -Werror`. If you are compiling with MSVC, start with `/W3 /Wx`. Add `#include `. Change the return type of `toUpperCase` to return `char *` and add a `return` statement to return a `char *`. Insert a declaration of it before `main` or move its definition before `main`. – Eric Postpischil Mar 12 '22 at 19:29
  • For gcc don't forget `-O2` or `-Os` and `-W`. Because `-Wall` does not include `-W` and many warnings require an optimization pass. – Goswin von Brederlow Mar 13 '22 at 00:17

3 Answers3

1

Your toUpperCase function should look somethings like this:

char *toUpperCase(char *word)
{
    for (int i = 0; word[i]; i++) {
        word[i] = toupper(word[i]);
    }
    return word;
}
SGeorgiades
  • 1,771
  • 1
  • 11
  • 11
1

All good comments. "while(1 < 5)" is an interesting construct for an infinite loop. Perfectly functional, but "while (1)" or "for (;;)" is typically used.

Paul Lynch
  • 241
  • 1
  • 5
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 13 '22 at 07:32
1

Note assuming you make the suggested changes to toUpperCase(), your "exit" test will always fail, since your monthName buffer is always converted to upper case. There's no need to keep calling it after the first "JANUARY" strcmp(). And you need to get rid of the newline left by fgets().

Paul Lynch
  • 241
  • 1
  • 5