C How to Validate a User Entering Y or N to Run Again
#1
y/n input validation
Posted 14 March 2010 - 12:47 AM
Hey guys,
I'm using a do/while loop by asking the user if they want to add two numbers. I was able to figure out the integer input validation but i'm having trouble with the y/n loop. It should look like this.
Jeremy Smith, do you want to add two more numbers (y/n)? y
Enter the first number (0,1,2,3,4,5,6,7,8,9): 34
Your input must be (0,1,2,3,4,5,6,7,8,9). Try again: 3
Enter the second number (0,1,2,3,4,5,6,7,8,9): 5
3 + 5 = 8
**********************************************
Jeremy Smith, do you want to add two more numbers (y/n)? 3
Do you want to add two more numbers (y for yes, n for no)? n
**********************************************
Thank you Jeremy Smith.
#include <iostream> #include <string> #include <limits> using namespace std; /***************************************************************** prototype of the function(s) included in the program *****************************************************************/ int integerOnly(); char charOnly(string wholeName); void getNumbers(string wholeName); /***************************************************************** main() function definition *****************************************************************/ const unsigned int NUMBERS = 10; const string numbersToEnter = "(0,1,2,3,4,5,6,7,8,9)"; int main() { string fullName; unsigned int firstNumber, secondNumber, calcTotal; cout << "Please enter your name: "; getline(cin, fullName); cout << endl << "************************************************"; cout << "\nEnter the first number " << numbersToEnter << ": "; firstNumber = integerOnly(); cout << "\nEnter the second number " << numbersToEnter << ": "; secondNumber = integerOnly(); cout << endl << endl; calcTotal = firstNumber + secondNumber; cout << firstNumber << " + " << secondNumber << " = " << calcTotal; cout << endl << "************************************************"; getNumbers(fullName); system("pause"); return 0; } void getNumbers(string wholeName) { char yesOrNo; unsigned int numberOne, numberTwo, addedTotal; cout << "\n" << wholeName << ", do you want to add two more numbers (y/n)? "; yesOrNo = charOnly(wholeName); do { cout << endl << endl; cout << "\nEnter the first number " << numbersToEnter << ": "; numberOne = integerOnly(); cout << "\nEnter the second number " << numbersToEnter << ": "; numberTwo = integerOnly(); cout << endl << endl; addedTotal = numberOne + numberTwo; cout << numberOne << " + " << numberTwo << " = " << addedTotal; cout << endl << "************************************************"; }while (yesOrNo != 'n'); return; } int integerOnly() { int numberOnly; cin >> numberOnly; while (numberOnly >= NUMBERS) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout << "Your input must be " << numbersToEnter << "." " Try again: "; cin >> numberOnly; while (cin.fail()) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout << "Your input must be " << numbersToEnter << "." " Try again: "; cin >> numberOnly; } } return numberOnly; } char charOnly(string wholeName) { char characterOnly; cin >> characterOnly; if (characterOnly != 'y' || 'n') cout << "Do you want to add two more numbers (y for yes, n for no)? \n"; else (characterOnly == 'n'); cout << "\n************************************************\n"; cout << "Thank you " << wholeName; return characterOnly; } #2
Re: y/n input validation
Posted 14 March 2010 - 01:02 AM
You set the value of yesOrNo to based on the output of the function charOnly :
Quote
yesOrNo = charOnly(wholeName);
Inside of that function, you prompt the user :
Quote
Do you want to add two more numbers
& then after that function ends, you prompt the user :
Quote
do you want to add two more numbers
So it's going to ask them twice, regardless.
#3
Re: y/n input validation
Posted 14 March 2010 - 01:20 AM
I have also spotted a syntax error. Here (inside charOnly function):
if (characterOnly != 'y' || 'n')
Now, you know that 'n' is compared with characterOnly, but the Compiler doesn't! You have to do this:
if (characterOnly != 'y' || characterOnly != 'n')
#4
Re: y/n input validation
Posted 14 March 2010 - 01:31 AM
but what if its N or Y!
i'm sure you know how to tighten that loose end.
#5
Re: y/n input validation
Posted 14 March 2010 - 01:49 AM
You would use toupper() or tolower() to guarantee the result from the user.
#6
Re: y/n input validation
Posted 14 March 2010 - 03:26 AM
I took out the charOnly function and just did everything in the getNumbers function. The only part I'm having trouble with now is if the user enters a number instead of y or n.
void getNumbers(string wholeName) { char yesOrNo; unsigned int numberOne, numberTwo, addedTotal; do { cout << "\n" << wholeName << ", do you want to add two more numbers (y/n)? "; cin >> yesOrNo; if (yesOrNo == 'y') { cout << endl << endl; cout << "\nEnter the first number " << numbersToEnter << ": "; numberOne = integerOnly(); cout << "\nEnter the second number " << numbersToEnter << ": "; numberTwo = integerOnly(); cout << endl << endl; addedTotal = numberOne + numberTwo; cout << numberOne << " + " << numberTwo << " = " << addedTotal; cout << endl << "************************************************"; } if (yesOrNo == 'n') { cout << "\n************************************************\n"; cout << "Thank you " << wholeName << ".\n\n"; } else cout << "Do you want to add two more numbers (y for yes, n for no)? "; cin >> yesOrNo; }while (yesOrNo == 'y'); return; } #7
Re: y/n input validation
Posted 14 March 2010 - 09:25 AM
no2pencil, on 14 March 2010 - 12:49 AM, said:
You would use toupper() or tolower() to guarantee the result from the user.
But of course... or you can add it in your conditional loop which only allows users to enter what you asked. For example
char man = ' '; //declare variable as character while(man != 'y' && man != 'n') //loops until one input allows it to break (i.e. makes the loop false) { man = _getch(); //until the loop breaks all input will not be shown or saved in the variable. } This post has been edited by xtr3mnico: 14 March 2010 - 09:26 AM
Source: https://www.dreamincode.net/forums/topic/161839-yn-input-validation/
0 Response to "C How to Validate a User Entering Y or N to Run Again"
Post a Comment