This is my function for validating a char value. So far it doesn't accept any value. I need it to accept Y and N. I can't find the problem. #include <iostream>#include <limits>using std::cin;using std::cout;char validate(char value){ while (std::cin.fail() || (value != 'Y') || (value != 'y') || (value != 'N') || (value != 'n')) {  cout << "Please input a Y or N." << endl;  cin.clear();  cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  cin >> value; } return value;}

Question
Asked Oct 13, 2019
24 views

This is my function for validating a char value. So far it doesn't accept any value. I need it to accept Y and N. I can't find the problem.

 

#include <iostream>

#include <limits>

using std::cin;

using std::cout;

char validate(char value)
{
 while (std::cin.fail() || (value != 'Y') || (value != 'y') || (value != 'N') || (value != 'n'))
 {
  cout << "Please input a Y or N." << endl;
  cin.clear();
  cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  cin >> value;
 }
 return value;
}

check_circle

Expert Answer

Step 1

A C++ program for validating a char value is as follows,

//Declare headre files

#include <iostream>

#include <limits>

//Define a function resetInpBuffer()

void resetInpBuffer () {

    //Clear the error of the buffer

    std::cin.clear ();

    //Move to the next line after the first occureance of error

    std::cin.ignore (std::numeric_limits <std::streamsize>::max (), '\n');

}

//Define a function

char validate()

{

    //Initialize the char value

    char  c {};

    //Create while loop for "True"

    while (true)

    {

        //Print string

        std::cout << "Please input  Y, y, N, or n: ";

        // make sure the input buffer is ready

        //Call the function

        resetInpBuffer ();

        //Get the character input

        std::cin >> c;

        //Check whether fail() return "True"

        if (std::cin.fail ())

        {

            //Go to the beginning of the loop

            continue;

        }

        //Check the condition for input value

        if (c == 'Y' || c == 'y' || c == 'N' || c == 'n')

        {

            //Return the character

            return c;

        }

    }

}

//Define main() function

int main()

{

    //Assign the value return from validate() method

    char c = validate();

    //Print the value

    std::cout<<"The character is "<<c;

    //Return "0"

    return 0;

}

Screenshot of program #1:

help_outline

Image Transcriptionclose

//Declare headre files include <iostream> #include <limits> //Define a function resetInpBuffer () void resetInpBuffer { //clear the error of the buffer std: :cin.clear ); /Move to the next line after the first occureance of error std: cin.ignore (std::numeric limits <std::streamsize>: :max ), \n');

fullscreen
Step 2

Screenshot of program #2:

help_outline

Image Transcriptionclose

// Define a function char validate ) //Initialize the char value char c //Create while loop for "True" while (true) //Print string std: cout <« "Please input Y, y, N, or n: ". / make sure the input buffer is ready //Call the function resetInpBuffer () //Get the character input std::cin >» c;

fullscreen
Step 3

Screenshot of program #3...

help_outline

Image Transcriptionclose

//Check whether fail () return "True" if (std: :cin.fail )) //Go to the beginning of the loop continue //Check the condition for input value if (c Y' || c == 'y' || c == 'N' || c == 'n') /Return the character return C

fullscreen

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: What is the ASCII decimal value for a lower case 'a'?

A: Definition ASCII stands for the American Standard Code for Information Interchange. Basically, this ...

question_answer

Q: How would I network two computers at home?

A: RequirementsTo network two home computers, you will need the following:Broadband internet connection...

question_answer

Q: How would I go about writing this code?

A: Program AlgorithmImport the required package and create a class called Pattern_Java.Define the main(...

question_answer

Q: Write a Java class, XXX_Lab7-3, where XXX is your Kean email ID. In the main method:* Create an arra...

A: Writing a java Program to create a class name XXX_Lab7-3. In this program there are two arrays, one ...

question_answer

Q: Python 3.7.4 Given four files named asiasales2009.txt, europesales2009.txt, africasales2009.txt, and...

A: Statements for creating objects:#for first file asiasales2009.txt for writingasia = open("asiasales2...

question_answer

Q: How would you modify the dynamic programming algorithm for the coin collecting problem if some cells...

A: Coin collecting problemIn a n-m board, several coins are placed in such a way no more than one coin ...

question_answer

Q: Consider a program that declares global integer variables x, y[10]. Thesevariables are allocated sta...

A: Explanation of the code: lw $s1, 0($gp) #load value in base address to $s1addi $s1, $s1, 25 #increme...

question_answer

Q: Python 3.7.4 Write a for loop that prints in ascending order all the positive multiples of 5 that ar...

A: Using a for loop to print all the positive 5 multiples under 175 .For loop: It is programming langua...

question_answer

Q: Draw the truth tables for the following Boolean expressions. xy+yz+xz x¯y+yz¯+x¯z (x⊕y)+(y⊕z)+(x...

A: As per our policy, we will answer the first question only since the exact one is not specified. Plea...