Small question :)

hey guys, i'm kinda noob in programming stuff and have a little question, if anyone can help that be awesome;

So, i'm working on a GUI program, and i wanna give an error message if the value is not integer put in a TextBox.. any idea what code is that? i know it must be simple but can't figure it out


thx
You may want to look into the MessageBox command. I'm not that conversant with working outside the console yet, but I know that it's one of the first things covered in the Programming for Windows book.
Hmm, interesting. If you know how to grab the input from the text box then I can help you. I haven't delved that far into WinAPI/MFC/X to help you with that, but I can help you if you have the input in string format. (or a format similar stringstream, const char [], char *, etc.)

Let's think about an integer. An integer only contains whole numbers, but integers also contain negative numbers. Therefore, integers can only contain the characters "0,1,2,3,4,5,6,7,8,9,-" I would also include space or tab since people have a tendency to press those keys after inputs. I would parse through the string and make sure that each of the characters is equal to one of those values.

I'm away from my computer that has my compiler on it. (I'm using ChromeOS, it stinks.) So my code is probably riddled with bugs, but this should be what you need to do in general. I'm going to fix it when I get home.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool checkInteger(std::string input)
{
     /* convert the string to const char */
     const char c_input [] = input.c_str();

     /* iterate through the input */
     /* you may want to change this to the size of c_input */
     for (int i = 0; i < input.size(); i++)
     {
          /* check if the character is not an integer nor a negative sign */
          if ((c_input[i] < 48 ) && (c_input[i] > 57 ) && (c_input[i] != 45))
          {
               return false;
          }

     }

     /* no violations have been detected, this is an integer */
     return true;
}


There is probably much faster ways of doing this, but I hope that this will help you along.

Edit:

If you want to use the value once you found out it is an integer, you could do something like:

1
2
3
4
if (checkInteger(input))
{
     int number = atoi(input.c_str());
}


Good Luck,
MaxterTheTurtle
Last edited on
Topic archived. No new replies allowed.