C/C++ Practice Questions on User Input Problem 3

This is my series on C/C++ problems for accepting user input in a robust way. The questions of this series start from C programs, and they slowly build and convert to classes, and which further build into inheritance. This is Problem 3 of this series.

Last Reviewed and Updated on February 7, 2020
Posted by Parveen(Hoven),
Aptitude Trainer and Software Developer

User Input - Problem 3

Write a C-program based on _cgets that asks the user to enter a character. Then it displays the day of the week that starts with that character. It should display 'No Day' if no week day starts with that choice.

Solution
// include all the headers
#include "conio.h"
#include "string.h"
#include "ctype.h"
// set maxchars = 1.
#define MAXCHARS    1
main ()
{

    // set a buffer to hold the user input
    char cBuff [MAXCHARS + 3];

    char* cReturn = NULL;

    cBuff [0] = MAXCHARS + 1;

    printf ("\nEnter a character: ");

    cReturn = _cgets (cBuff);

    printf ("The day of the week is: ");

    switch (cReturn [0])
    {

    case 's':
    case 'S':
        printf ("Sunday or Saturday\n");

        break;

    case 'm':
    case 'M':
        printf ("Monday\n");

        break;

    case 't':
    case 'T':
        printf ("Tuesday or Thursday\n");

        break;

    case 'w':
    case 'W':
        printf ("Wednesday\n");

        break;

    case 'f':
    case 'F':
        printf ("Friday\n");

        break;

    default:
        printf ("No Day\n");

    }

}


My C/C++ Videos on Youtube

Here is the complete playlist for video lectures and tutorials for the absolute beginners. The language has been kept simple so that anybody can easily understand them. I have avoided complex jargon in these videos.



Creative Commons License
This Blog Post/Article "C/C++ Practice Questions on User Input Problem 3" by Parveen (Hoven) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Updated on 2020-02-07. Published on: 2015-12-20