Help with validation

This is a discussion on Help with validation within the C++ Programming forums, part of the General Programming Boards category; Hi Guys, Can you have a browse through my code and see if you can see a problem. The problem ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    59

    Help with validation

    Hi Guys,

    Can you have a browse through my code and see if you can see a problem.

    The problem i am having is, when i enter the second number and validate it as wrong ie, more than 6 digits it returns to begining asking for the fisrt num0ber again (This is right) but then it will not accept a valid number it just keeps jumping back to the beginning asking for the first number... can anyone help?

    Here are the two functions that i am using: -

    Code:
    void calc()
    {
    clrscr();
    cout << "Please enter your first number:";
    cin >> num1;
    
    validation1();
    
    cout << "Please enter your second number:";
    cin >> num2;
    
    validation1();
    
    cout << "Please select an operator + - * / %";
    cin >> option1;
    
    switch (option1)
    {
    case '+': add();
    break;
    case '-': subtract();
    break;
    case '*': multiply();
    break;
    case '/': divide();
    break;
    default: cout << "invalid";
    }
    }
    
    void validation1()    // start of validation1 function
    {
    
    length1 = strlen(num1);
    convertnum1 = atof(num1);
    length2 = strlen(num2);
    convertnum2 = atof(num2);
    
    if (length1 <= 6 && convertnum1 > 0 && convertnum1 < 1000)
    {
    }
    
    
    if (length2 <= 6 && convertnum2 > 0 && convertnum2 < 999)
    {
    }
    
    else if (length1 > 6 || length2 > 6)
    {
    calc();    // if false returns to calculator funtion
    }
    
    }// end of validation function

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    Your Stream Might Have The New Line Symbol

    Hi,

    I haven't taken time to read your code completely, but I believe I understand your problem.

    Let us say, you are accepting a value X from the keyboard into a character variable called ch;

    cin >> ch;

    You would have entered X and then pressed the enter-key. The symbol X is stored into ch, but the input stream still consists the enter-key. Its not yet flushed.

    so you could try the c-function

    fflush(stdin);

    This would flush your input stream and things might work as wished.

  3. #3
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801
    Code:
    else if (length1 > 6 || length2 > 6)
    {
    calc();    // if false returns to calculator funtion
    }
    This doesn't return back to calc(), this calls it. So all your program is doing running the first three lines of calc() over and over again. You might want to define validation1 as returning an int and then replacethis code with:

    Code:
    else if (length1 > 6 || length2 > 6)
    {
    return 0;    // if false returns to calculator funtion
    }
    Also

    Code:
    if (length2 <= 6 && convertnum2 > 0 && convertnum2 < 999)
    {
    }
    What's thepurpose of this? You are evaluating a condition but then not doing anything if it is true.

    Your code is a little confusing. I assume that num1 etc are characters as you have used string functions on them, but that leads me to ask why you named them num1 etc.
    Last edited by minesweeper; 01-10-2003 at 04:14 AM.

  4. #4
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801
    hmmmm, I'm also confused by the comment in
    Code:
    else if (length1 > 6 || length2 > 6)
    {
    calc();    // if false returns to calculator funtion
    }
    if what is false? For calc() to be called, the condition must be evaluated to true, not false.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    59
    Cheers guys,

    As you can tell im quite new to this C++,

    I know to myself what i mean, but to experianced programmers like yourselfs i guess it does look abit out of place!

    I will give your sugestions a try and let you know how i got on..


    Thanks to everyone who posted

    Boontune

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    59
    Please tell me if im wrong,

    What the idea is with the validation function is to check that the input number is < or = to 6 digits long and in the range 0 - 1000,

    if it passes the validation i want it to return to calc(),
    and continue running the calc() function, this is why i left it blank in the IF statement, any ideas of what i should do there?

    Because i thought that if it passed the IF statement it would return to calc and ask for the second number.....

    Cheers guys

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    you can turn the validation function into an int or bool instead of void. if the number meets the conditions, return true or some int, else return false. ex.

    Code:
    bool validation() {
        if (length1 <= 6 && convertnum1 > 0 && convertnum1 < 1000)
            return true;
        else
            return false;
    }
    think about it...

  8. #8
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801
    boontune, alpha provided a good example validation function.

    A little about 'if' statements:

    An if statement doesn't impede the execution of the rest of the function if the condition evaluates to false, it simply means that the code written inside the open and closed parentheses immediately after the statement won't be executed. For example:

    Code:
    //code before 'if' statement
    if (condition evaluating to false)
    {
    funtion();
    }
    //code after 'if' statement
    Code:
    //code before 'if' statement
    if (condition evaluating to true)
    {
    funtion();
    }
    //code after 'if' statement
    In both of these examples the code before and after the 'if' statement block will be executed, the only difference is that, in the second example 'function()' is called in between the two sections of code whereas in the first example it isn't. Is that any clearer?
    Last edited by minesweeper; 01-10-2003 at 08:03 AM.

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    59
    Yeah i think so!

    Thanks alot to everyone who chipped in!


    Boontune

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. validation of TextBoxes!?
    By c9dw2rm8 in forum C# Programming
    Replies: 1
    Last Post: 04-01-2008, 06:19 PM
  2. set validation
    By jmarsh56 in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2005, 07:49 AM
  3. DATE <TIME.H> data validation
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 08-07-2005, 01:10 AM
  4. validation routine help plz
    By rajesh23 in forum C Programming
    Replies: 8
    Last Post: 09-23-2003, 07:21 AM
  5. [C#] Validation class for events?
    By gicio in forum C# Programming
    Replies: 4
    Last Post: 01-03-2003, 11:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21