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 May 17, 2017
Posted by Parveen(Hoven),
Aptitude Trainer and Software Developer

Advertisement

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 2017-05-17.


Advertisement