0

I am brushing up on my C and to do that, I was attempting the following problem. I have also provided my proposed solution. It'll work when I write it as a one pass solution but not I write in the while loop (I wanted to write the code to be where I could keep prompting the user for numbers until they quit). I am getting the Runtime error 153. Thx.

Write a C program that reads an integer between 1 and 12 and print the month of the year in English. Test Data : Input a number between 1 to 12 to get the month name: 8 Expected Output: August

#include <stdio.h>
int main(void){
    int int1 = 0;
printf ("Enter a number between 1-12 to get a month. Enter 13 to quit. \n");
scanf ("%d", &int1);
char ch_arr[12][10] = 
        {
            "January",
            "February",
            "March",
            "April",
            "May",
            "June",
            "July",
            "August",
            "September",
            "October",
            "November",
            "December"
        };
        while (int1 < 13)
        {
            printf ("Your month is %s \n", ch_arr +int1 );
            scanf ("%d", &int1);
            
        }
        return 0;

}
David
  • 1
  • "*I am getting the Runtime error 153*" Could you please elaborate ? Further, this could be an issue with using `scanf()`, which is usually easy to get wrong. See also : http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html , https://stackoverflow.com/questions/58403537/what-can-i-use-for-input-conversion-instead-of-scanf – user426 Dec 04 '21 at 05:01
  • I read through part of that article and it seems to restrict when to use scanf. Yet, an online search for an alternative didn't yield any good results. Thanks for responding, user426. – David Dec 06 '21 at 00:21
  • Note that arrays start at index 0 in C - so subtract 1 from the value of `int1` before using it as an index. Also, better to use `ch_arr[int1-1]` than `ch_arr + int1 - 1`; also, check for input success (`scanf` will return 1) and for negative numbers. – Adrian Mole Dec 09 '21 at 21:42

0 Answers0