By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,417 Members | 1,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,417 IT Pros & Developers. It's quick & easy.

noobie question regarding strings/data validation

P: 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
Share this Question
Share on Google+
4 Replies


100+
P: 144
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
Expert Mod 5K+
P: 8,916
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

P: 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

P: 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

Post your reply

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