Thread: Just started C++ and need help on... validation.

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    6

    Just started C++ and need help on... validation.

    I did have a look through previous threads about validation, but most of the posts went straight over my head. I'm making a CLI calc app in Linux, and it works fine, but there are a couple of queries:

    1. My current program uses GOTOs to return to the options dialogue, so the user can enter another option to use a different operation. I have heard plenty about GOTOs being bad practice, but what I don't clearly know is why? All I know is they are decried on every corner of the globe. If I shouldn't use GOTOs, I assume a loop of some sort would do the job? I am not sure which to use or how to break out of it should the user wish to close the program.

    2. I have no idea how to validate the input so that letters are rejected. I have been told a for loop of some kind is the way to do it, but I am still stumped as to how. I was also told that getting the program to check the ASCII values of the characters/digits passed through and only accept a certain range, but again, I am clueless as to how exactly.

    If I could have some help on these issues, it would be much appreciated.

    My program so far:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(void)
    {
        float option,operation,num1,num2;
    
        cout << "Welcome to Bal Jacques' calculator program. Select the operation you want from the list below, "
                "by typing in the corresponding number:\n";
        cout << "1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. About\n6. Quit\n";
        Start:
        cout << "Option: ";
        cin >> option;
        /*int iCount = 0
        for(iCount = 0; iCount < len(option); iCount++;)
        {
    
        }*/
        if (option == 1)
        {
            cout << "This operation will add two numbers.\n";
            cout << "Number 1: ";
            cin  >> num1;
            cout << "Number 2: ";
            cin >> num2;
            operation = num1 + num2;
            cout << "The answer is " << operation << "\n";
    		goto Start;
        }
        else if (option == 2)
        {
            cout << "This operation will subtract two numbers.\n";
            cout << "Number 1: ";
            cin >> num1;
            cout << "Number 2: ";
            cin >> num2;
            operation = num1 - num2;
            cout << "The answer is " << operation << "\n";
    		goto Start;
        }
        else if (option == 3)
        {
            cout << "This operation will multiply two numbers.\n";
            cout << "Number 1: ";
            cin >> num1;
            cout << "Number 2: ";
            cin >> num2;
            operation = num1 * num2;
            cout << "The answer is " << operation << "\n";
    		goto Start;
        }
        else if (option == 4)
        {
            cout << "This operation will divide two numbers.\n";
            cout << "Number 1: ";
            cin >> num1;
            cout << "Number 2: ";
            cin >> num2;
            if (num2 == 0)
            {
                cout << "You cannot divide by zero.\n";
            }
            else
            {
                operation = num1 / num2;
                cout << "The answer is " << operation << "\n";
            }
    		goto Start;
        }
        else if (option == 5)
        {
            cout << "Calculator App version 1.6.1 by Bal Jacques.\n";
    		goto Start;
        }
    	else if (option == 6)
    	{
    		cout << "Thank you for using this program, Goodbye.\n";
    		cin.ignore();
    		cin.get();
    	}
        else
        {
            cout << "Enter a number from 1 to 6.\n";
            goto Start;
        }
    
    }
    Last edited by CornedBee; 10-07-2009 at 10:55 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Old Marcus
    If I shouldn't use GOTOs, I assume a loop of some sort would do the job? I am not sure which to use or how to break out of it should the user wish to close the program.
    This is roughly what you are currently doing:
    Code:
    Start:
        read option
        if option is 1
            ...
            goto Start
        else if option is 2
            ...
            goto Start
        ...
        else if option is 6
            ...
        else
            ...
            goto Start
    For alternatives, you could express it this way:
    Code:
    loop indefinitely
        read option
        if option is 1
            ...
        else if option is 2
            ...
        else if option is 6
            ...
            break
        else
            ...
    or this way:
    Code:
    set option to 0
    while option is not 6
        read option
        if option is 1
            ...
        else if option is 2
            ...
        else if option is 6
            ...
        else
            ...
    To loop indefinitely, you could use a for loop for which the loop condition is empty, e.g., for (;;) or a while loop for which the condition is true, e.g., while (true).

    Quote Originally Posted by Old Marcus
    2. I have no idea how to validate the input so that letters are rejected. I have been told a for loop of some kind is the way to do it, but I am still stumped as to how. I was also told that getting the program to check the ASCII values of the characters/digits passed through and only accept a certain range, but again, I am clueless as to how exactly.
    Well, suppose the user enters 12 and 123abc. Are you willing to ignore the "abc"?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Regarding validation of input: try searching the board. For example: RPG troubles
    (That was me. What can I say? I remember and can more easily find my own posts . . . )
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    There's nothing wrong with goto's, although you generally don't need to use them, there are a few cases where goto is the most efficient and clearest method. So void them when you can, but don't stress out about using them if you need to.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    1. Goto is bad because it makes it more difficult to follow the control flow of the program and it doesn't express the intent of the coder as well as the equivalent loop. It makes reading and maintaining the code more difficult. Experienced coders can use goto in certain situation where they understand the pros and cons of such use, but beginners lack the knowledge and experience to use goto appropriately.

    Since it can seem easier to implement in the short run, beginners often turn to goto rather than take the time to identify the more appropriate loop structure. This is why the "goto is evil" advice is often provided to beginners. If you find yourself using goto, then consider taking some time to rethink your design and identify a loop that can express your intent more clearly. That is exactly what you're doing here, so well done. laserlight has shown some good examples of how you might better implement your design.

    2. There are different ways of validating input, but I prefer using a loop as dwks suggested in the second code example in the linked thread. I don't think looking at ascii values of numbers is a good idea. The main reason is that you would have to consider all possibilities yourself. But this code has already been written for you, and it is better to make use of it than to try to get it right yourself. Unfortunately, there is no easy, standard way of doing the whole task, but the loop shown by dwks should be good enough.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    6
    Thanks for your help guys, I changed my method to a while loop, and used the validation that dwks suggested and it all works fine. Cheers.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Daved View Post
    1. Goto is bad because it makes it more difficult to follow the control flow of the program and it doesn't express the intent of the coder as well as the equivalent loop. It makes reading and maintaining the code more difficult. Experienced coders can use goto in certain situation where they understand the pros and cons of such use, but beginners lack the knowledge and experience to use goto appropriately.
    I like to compare it to a writer taking "literary license" to break the rules of English. A good writer can do it for effect, and get away with it. An inexperienced writer trying to do the same just looks ignorant.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed