473,405 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Help with input validation

sonic
40
I am having problem with my printError function. It handles incorrect input from user dealing with the numerical range. However, on the second condition being evaluated (dealing with character selection for the pattern) I always recieve "Invalid character.", even if the selection is of an eligible type. Here's my code:


Expand|Select|Wrap|Line Numbers
  1. / Definition of printError which determines if user has entered         *
  2. // acceptable input.                                                     *
  3. //**************************************************  **********************
  4.  
  5. void printError(int& rows, char& characterSelect)
  6. {
  7.  
  8.     if ((rows < 2) || (rows > 14) || (rows%2 != 0))
  9.     {
  10.         cout << "\nInvalid number of rows. Please retry.\n";
  11.         cout << "\n";
  12.         getInput(rows, characterSelect);
  13.     }
  14.     else if ((characterSelect != '*') || (characterSelect != '+') || (characterSelect != '#') || (characterSelect != '$'))
  15.     {
  16.         cout << "\nInvalid character. Please retry using one of the four characters given.\n";
  17.         cout << "\n";
  18.         getInput(rows, characterSelect);
  19.     }
  20. }
  21.  
  22.  
Thanks for any help on this others may have!
Dec 3 '06 #1
2 1467
Ganon11
3,652 Expert 2GB
I am having problem with my printError function. It handles incorrect input from user dealing with the numerical range. However, on the second condition being evaluated (dealing with character selection for the pattern) I always recieve "Invalid character.", even if the selection is of an eligible type. Here's my code:

Expand|Select|Wrap|Line Numbers
  1. void printError(int& rows, char& characterSelect)
  2. {
  3. //...
  4. else if ((characterSelect != '*') || (characterSelect != '+') || (characterSelect != '#') || (characterSelect != '$'))
  5.     {
  6.         cout << "\nInvalid character. Please retry using one of the four characters given.\n";
  7.         cout << "\n";
  8.         getInput(rows, characterSelect);
  9.     }
  10. }
Thanks for any help on this others may have!
The problem is in your else if portion. Let's look at the logic determining whether or not your input is bad.

Expand|Select|Wrap|Line Numbers
  1. (characterSelect != '*')
and statements like it are fine - the block should not execute if the character is *, or + or whatever. However, in between the statements, you have || - OR. This means that you are in effect saying, "If the character is not * OR the character is not + OR...then do this." But let's assume your characterSelect is actually '*'. The first condition would evaluate to false (because characterSelect == '*'), but the second condition would evaluate to true (because characterSelect != '+'). At this point, the entire condition has evaluated to true, because of the ||.

Instead, use && - AND. Looking at the same example, the first condition would evaluate to false - therefore, the entire condition is false, because &&s need everything to be true before they execute.

Your new else if will look like this:

Expand|Select|Wrap|Line Numbers
  1. else if ((characterSelect != '*') && (characterSelect != '+') && (characterSelect != '#') && (characterSelect != '$'))
  2. {
  3.     cout << "\nInvalid character. Please retry using one of the four characters given.\n";
  4.     cout << "\n";
  5.     getInput(rows, characterSelect);
  6. }
Hope that helps!
Dec 3 '06 #2
sonic
40
Thank you so much for your help. That seemed to clear everything up.
Dec 4 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Adrienne | last post by:
I am the first to admit that I know bupkis about javascript, except that sometimes I need it to do something client side that I can't do server side. Anyway, here's my problem: <input...
20
by: Sivarn | last post by:
I'm writing a program for that takes dates as input using scanf. I want to verify that the user is inputting a full 4 digits for the year. How do I do this? I know that the return value on printf...
6
by: serge calderara | last post by:
Dear all, I have read that ASP.NET does double user input validation of control when they are place on the page. Once on teh client side and again from server side right ? Could explain how...
36
by: aljamala | last post by:
Hi, I keep getting this warning on a page, but I do not know what the problem is...does anyone have an idea about what could be wrong? line 88 column 7 - Warning: missing </formbefore <td> it...
1
by: lilbit02 | last post by:
Hi, I have a form that utilizes validation as most do via javascript. This form worked totaly fine until I needed to add a dynamic div now the validation doesn't work. It does work for the first...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.