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

noobie question regarding strings/data validation

19
Hi

No doubt this question, or a similar version of it, has been asked before. But, I was looking around the forums and i couldn't find anything.

I'm using VC++ to create a simple calculator that sums the values from 2 textboxes. I wanted to implement a simple data validation that would spit out various error messages if the user typed in incorrect data.

I managed to vomit out the following code. Yes I know it's beastly, rudimentary and all around cumbersome...I've been attempting to teach myself C++. Any ideas on how to make it more efficient would be greatly appreciated.

i have no idea what the difference between int16 and int64 means, and the reference to indexes can be ignored for now. eventually i want to be able to parse an entire string that a user inputs, and run validation through it. but one step at a time...

here's my code so far...

Expand|Select|Wrap|Line Numbers
  1. private: System::Int64 check_for_num(char test){
  2.              if(test == '1' || test == '2' || test == '3' || test == '4' || test == '5' || test == '6' || test == '7' || test == '8' || test == '9' || test == '0'){
  3.                     return 1;
  4.              }
  5.              else {
  6.                  return 0;         
  7.              }
  8.          }
  9.  
  10. private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
  11.  
  12.             double num_1, num_2, num_3;
  13.  
  14.             if(textBox1->Text->Length == 0 && textBox2->Text->Length == 0){
  15.                    textBox3->Text = "Empty entries in Boxes 1 and 2";
  16.             }
  17.             else if(textBox1->Text->Length == 0){
  18.                    textBox3->Text = "Empty entry in Box 1";
  19.             }
  20.             else if(textBox2->Text->Length == 0){
  21.                    textBox3->Text = "Empty entry in Box 2";
  22.             }
  23.             else if(check_for_num((char)(textBox1->Text[0])) == 0 && check_for_num((char)(textBox2->Text[0])) == 0){
  24.                    textBox3->Text = "Non-integer in Boxes 1 and 2";
  25.             }
  26.             else if(check_for_num((char)(textBox1->Text[0])) == 0){
  27.                    textBox3->Text = "Non-integer in Box 1";
  28.             }
  29.             else if(check_for_num((char)(textBox2->Text[0])) == 0){
  30.                    textBox3->Text = "Non-integer in Box 2";
  31.             }
  32.             else if(textBox1->Text->Length == 0 && check_for_num((char)(textBox2->Text[0])) == 0){
  33.                    textBox3->Text = "Empty entry in Box 1 and Non-integer in Box and 2";
  34.             }
  35.             else if(check_for_num((char)(textBox1->Text[0])) == 0 && textBox2->Text->Length == 0){
  36.                    textBox3->Text = "Empty entry in Box 2 and Non-integer in Box and 1";
  37.             }
  38.             else{
  39.                 num_1 = Convert::ToDouble(textBox1->Text);
  40.                 num_2 = Convert::ToDouble(textBox2->Text);
  41.                 num_3 = num_1 + num_2;
  42.                 textBox3->Text = num_3.ToString();
  43. }
  44.  }
  45.  
Sep 28 '06 #1
4 2890
tyreld
144 100+
You could just use the "isdigit()" function defined in the C <ctype.h> header.

Expand|Select|Wrap|Line Numbers
  1. // Returns non-zero if character is a digit between '0' and '9', returns 0
  2. // otherwise.
  3.  
  4. // NOTE: even though it takes an integer as input c must have the value of
  5. // usigned char or EOF.
  6.  
  7. int isdigit(int c)
  8.  
Also, Int64 and Int16 are ways of defining an integer of a specific bit size. This is useful if you know you need a integer of a specific size, as the primitve sizes are machine dependent (ie. char, int, and long will not necesarily be the same size on all machines).
Sep 28 '06 #2
Banfa
9,065 Expert Mod 8TB
Your if else if statement is a little cumbersome because you have multiplexed the testing of 2 variables so that you have 8 error options created from 2 error options on 2 variables. Treating each variable separately may make things clearer

Expand|Select|Wrap|Line Numbers
  1.     CString output = "";
  2.  
  3.     // First look for errors in textBox1
  4.     if(textBox1->Text->Length == 0){
  5.         output = "Empty entry in Box 1";
  6.     }
  7.     else if(check_for_num((char)(textBox1->Text[0])) == 0){
  8.         output  = "Non-integer in Box 1";
  9.     }
  10.  
  11.     // Next look for errors in textBox2
  12.     if(textBox2->Text->Length == 0){
  13.         if (output.GetLength() != 0){
  14.             output += " and ";
  15.         }
  16.         output += "Empty entry in Box 2";
  17.     }
  18.     else if(check_for_num((char)(textBox2->Text[0])) == 0){
  19.         if (output.GetLength() != 0){
  20.             output += " and ";
  21.         }
  22.         output += "Non-integer in Box 2";
  23.     }
  24.  
  25.     // If there have been no errors calculate the result
  26.     if (output.GetLength() == 0){
  27.         num_1 = Convert::ToDouble(textBox1->Text);
  28.         num_2 = Convert::ToDouble(textBox2->Text);
  29.         num_3 = num_1 + num_2;
  30.         output = num_3.ToString();
  31.     }
  32.  
  33.     // Display the result
  34.     textBox3->Text = output;
  35.  
Also this

check_for_num((char)(textBox2->Text[0]))

looks to me like it is only checking the first digit of the text in the textBox to make sure it's a digit. You may wish to change check_for_num so it takes a pointer so you can check all digits.
Sep 29 '06 #3
sanou
19
Yeah I thought about the basic algorithm some more and I realized there were probably easier ways of doing it. I'm still learning :p

Thanks for the tips guys. Much appreciated.
Sep 29 '06 #4
sanou
19
Here's my revised code. The next step would be to creat a function that would parse a string and check if there's any non-integers/blanks in it.

I know there are libraries out there which can handle this, but it's been years since i've done any coding (dropped out comp sci major in college to do biology) and i needed the refresher.

Banfa I used a variation of ur algorithm. it's a little bit more cumbersome, but i needed the practice. i can work on optimizing my code later on.

Expand|Select|Wrap|Line Numbers
  1. private: System::Int32 is_num(char test){
  2.    if(test == '1' || test == '2' || test == '3' || test == '4' || test == '5' || test == '6' || test == '7' || test == '8' || test == '9' || test == '0'){
  3.       return 1;
  4.    }
  5.    else {
  6.       return 0;         
  7.    }
  8. }
  9.  
  10. private: System::Void button1_Click(System::Object^  sender , System::EventArgs^  e) {
  11.  
  12.    double num_1 = 0, num_2 = 0, num_3 = 0;
  13.  
  14.    if(textBox1->Text->Length == 0){
  15.       if(textBox2->Text->Length == 0){
  16.          textBox3->Text = "Enter something in Box 1 and Box 2! - Msg 1";
  17.      return;
  18.       }
  19.       else if(is_num((char)(textBox2->Text[0])) == 0){
  20.          textBox3->Text = "Enter something in Box 1 and correct your entry in Box 2! - Msg 2";
  21.          return;
  22.       }
  23.       else{
  24.          textBox3->Text = "Enter something in Box 1! - Msg 3";
  25.          return;
  26.       }
  27.    }
  28.  
  29.    if(is_num((char)(textBox1->Text[0])) == 0){
  30.       if(textBox2->Text->Length == 0){
  31.          textBox3->Text = "Correct your entry in Box 1 and enter something in Box 2! - Msg 4";
  32.          return;
  33.       }
  34.       else if(is_num((char)(textBox2->Text[0])) == 0){
  35.          textBox3->Text = "Enter a correct entry in Box 1 and Box 2! - Msg 5";
  36.          return;
  37.       }
  38.       else{
  39.          textBox3->Text = "Correct your entry in Box 1! - Msg 6";
  40.          return;
  41.       }
  42.    } 
  43.  
  44.    if(textBox1->Text->Length != 0 && is_num((char)(textBox1->Text[0])) == 1){
  45.       if(textBox2->Text->Length == 0){
  46.          textBox3->Text = "Enter something in Box 2! - Msg 7";
  47.          return;
  48.       } 
  49.       if(is_num((char)(textBox2->Text[0])) == 0){
  50.          textBox3->Text = "Correct your entry in Box 2! - Msg 8";
  51.          return;
  52.       }
  53.       else{
  54.          num_1 = Convert::ToDouble(textBox1->Text);
  55.          num_2 = Convert::ToDouble(textBox2->Text);
  56.          num_3 = num_1 + num_2;
  57.          textBox3->Text = num_3.ToString();
  58.          return;
  59.       }
  60.    }
  61.  
  62. }
Sep 29 '06 #5

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

Similar topics

2
by: Shaun | last post by:
Hello! I can't seem to get paths and variables working together: import os a = 'books' os.chdir( '/test') os.mkdir("/test/"a) the last line does not seem to work. os.mkdir(a) makes the...
1
by: Aristotelis E. Charalampakis | last post by:
Hi everyone, this is a newbie question... I have a dll written in c++. This dll is called from a executable written in VB 6.0 I want to use a variable (say a float) within multiple functions...
0
by: NWx | last post by:
Hi, I discovered a strange (?!?!) issue related objects saved to cache. Let's explain my situation. I have a webform for user registration. Users enter username, password and other info...
2
by: msnews.microsoft.com | last post by:
Hi All, I m facing a problem. As u know asp.net vallidators did not work in non-microsoft browsers. So for that i m using Server Side Solution. But i don't understand how can i show the...
6
by: Katash | last post by:
***newbie_request*** I have a simple program that inserts user details into a MySQL database - The form validation is dealt with by another program that contains the html form I would like to...
1
by: DLN | last post by:
Hello all, I have a quick question regarding how best to use static strings in my C# code that I'm hoping someone can help me with. Is there any advantage/disadvantage from a performance...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
12
by: Martin Drautzburg | last post by:
I would like to validate sql strings, which are spread all over the code, i.e. I run ("prepare") them against a database to see if it happy with the statements. Spelling errors in sql have been a...
0
by: alagan | last post by:
Hai Guys, I'm Mathiyalgan... Working as asp.net developer in a small software concern . I've seen ur Article in the website...4guysfromrolla... It is quite nice... I hav used...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.