6 Replies - 5009 Views - Last Post: 14 March 2010 - 09:25 AM Rate Topic: -----

#1 cyfuzhi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 27-February 10

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;
}



Is This A Good Question/Topic? 0
  • +

Replies To: y/n input validation

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5382
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

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.
Was This Post Helpful? 0
  • +
  • -

#3 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

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')


Was This Post Helpful? 0
  • +
  • -

#4 xtr3mnico  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 89
  • Joined: 26-February 10

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.
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5382
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

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.
Was This Post Helpful? 0
  • +
  • -

#6 cyfuzhi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 27-February 10

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;
}


Was This Post Helpful? 0
  • +
  • -

#7 xtr3mnico  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 89
  • Joined: 26-February 10

Re: y/n input validation

Posted 14 March 2010 - 09:25 AM

View Postno2pencil, 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1