473,387 Members | 1,619 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,387 software developers and data experts.

Validation problem

2
Hi, I am new to c++ and having trouble with the code snippet below, in as much as it does what i think it should be doing but the last line is supposed to output toupper characters however if a numeric value is entered first it will only show the numeric values instead of the char can anyone point me in the right direction so the code will validate alpha characters only and then capitalise at the end? thanks in advance.
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<fstream>
  3. #include<cstdio>
  4. #include<string>
  5. #include<ctime>
  6. using namespace std;
  7. int main()
  8. {
  9.  
  10.  
  11.     // validation of input
  12.     bool isValid=true;
  13.     int i=0;
  14.     char chrId[3];
  15.         string id="";
  16.     cout<<"\n Enter your initials ";
  17.     getline(cin,id);
  18.     if(id.length()!=2)
  19.     {
  20.         cout<<"\n Please only enter two alpha initials "<<endl;
  21.     getline(cin,id);
  22.     }
  23.     strcpy(chrId,(id.substr(0,2).c_str()));
  24.     while(isValid && i < 2)
  25.     {
  26.         if(isalpha(chrId[i])==false)
  27.         {
  28.             cout<<"\n Please only enter two alpha initials "<<endl;
  29.             isValid=false;
  30.             getline(cin,id);;
  31.         }
  32.             chrId[i]=toupper(chrId[i]);
  33.         i++;
  34.  
  35.     }
  36.     cout<<"\n Initials entered are "<<chrId<<endl;
  37.  
  38.  
  39.     system("PAUSE");
  40.         return 0;
  41.  
  42. }
  43.  
Mar 16 '11 #1

✓ answered by weaknessforcats

A couple of things:

1) you are using C++ so why are you using arrays? Use a string or vector.

2) your code is doing more than one thing at a time. You should a) receive data, b) validate data, c)if valid, use the data.

Expand|Select|Wrap|Line Numbers
  1. if(isalpha(chrId[i])==false) 
  2.         { 
  3.             cout<<"\n Please only enter two alpha initials "<<endl; 
  4.             isValid=false; 
  5.             getline(cin,id);; 
  6.         } 
  7.             chrId[i]=toupper(chrId[i]); 
In this snippet if the data is not valid you gat new data but then you drop out of the if statement and use the array but not the data you just received.

Try something like:

Expand|Select|Wrap|Line Numbers
  1. string data;
  2. bool rval;
  3.  
  4.      GetData(data);
  5.      rval = Validate(data)
  6.      if (rval)
  7.      {
  8.         ProcessData(data);
  9.      }
  10.      else
  11.      {
  12.            //error message...
  13.      }
Also, toupper only changes lower case alpha characters to uppercase characters. Any othe characters pass through unaltered. Hence a 3 going in is a 3 coming out.

2 2049
weaknessforcats
9,208 Expert Mod 8TB
A couple of things:

1) you are using C++ so why are you using arrays? Use a string or vector.

2) your code is doing more than one thing at a time. You should a) receive data, b) validate data, c)if valid, use the data.

Expand|Select|Wrap|Line Numbers
  1. if(isalpha(chrId[i])==false) 
  2.         { 
  3.             cout<<"\n Please only enter two alpha initials "<<endl; 
  4.             isValid=false; 
  5.             getline(cin,id);; 
  6.         } 
  7.             chrId[i]=toupper(chrId[i]); 
In this snippet if the data is not valid you gat new data but then you drop out of the if statement and use the array but not the data you just received.

Try something like:

Expand|Select|Wrap|Line Numbers
  1. string data;
  2. bool rval;
  3.  
  4.      GetData(data);
  5.      rval = Validate(data)
  6.      if (rval)
  7.      {
  8.         ProcessData(data);
  9.      }
  10.      else
  11.      {
  12.            //error message...
  13.      }
Also, toupper only changes lower case alpha characters to uppercase characters. Any othe characters pass through unaltered. Hence a 3 going in is a 3 coming out.
Mar 16 '11 #2
Sion
2
Thanks for the suggestions i will keep at it :)
Mar 16 '11 #3

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

Similar topics

2
by: diong | last post by:
please help! i have a form validation problem. here is the code if ((form1.Decline.value=='') && (form1.OfficeStatus == "YES") & (form1.FloorBldgDiscStat.value=='')) { alert('Enter value on...
0
by: Brian | last post by:
I am having alot of trouble getting a XML document validated with a schema. I got a sample document and schema off of w3schools.com, which passed an online xml validator:...
4
by: bnp | last post by:
Hi All, I am quite new the JavaScript. Basically I am a C++ programmer, but now I am working on JavaScript since last 5 days. I have a problem regarding the form validation. I have created a...
3
by: Michael Skulsky | last post by:
Hi all, I've got the following validation problem. There are 2 schemas and a document: ----------------------------------------------------------------- bar.xsd ====== <?xml version="1.0"...
2
by: Shahar | last post by:
hi I have the following problem: I have two button, tow textboxes and two RequiredFieldValidator controls, like that: <form id="Form1" method="post" runat="server"> <asp:TextBox id="TextBox1"...
2
by: TIBM | last post by:
Hi. I've posted this question on another newsgroup, but I haven't received any answers.. I have a login page where users input userID and password and click a Login button. Before calling the ...
0
by: koen | last post by:
Hi! In asp.net (version 1.1) I run into a problem when trying to perform client validation. The error I get on the client-side is : "Error: expected ';'". This problem occurs when I place a...
5
by: Tina | last post by:
the Edit, Update, Cancel, and Delete buttons in my datagrid are causing validation elsewhere on the page. I want to specify that these buttons should not cause validation but they have no design...
6
by: tshad | last post by:
I have been having some issues trying to get validation to work. On most of my pages, I have no problem. But I have a page that has 4 validation objects and none are displaying an error...
5
by: Kuldeep | last post by:
Framework: Visual Studio 2005 Technology: ASP.NET 2.0 Language: C#.NET 2.0 Hi All, We have developed a Web Application on Visual Studio 2005 (ASP.NET 2.0) and deployed it on the Client's...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.