CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2008
    Posts
    3

    Really new to programming

    Hi, everyone. I am very new to the whole programming thing so if my questions seem pretty stupid I apologize. I was curious about something. I am trying to improve my data validation techniques and I wanted to know if there was a way to stop my computer from going nuts if a user enters an incorrect data type into a variable.


    I have this very small program that asks the user to enter the number of cookies they have eaten and it will tell them the amount of calories they consume. This is what I have so far:
    Code:
    #include <iostream>
    using namespace std;
    
    
    int main ()
    {
    
        int amount;
        int perCookie = 30;
        int total;
        
        cout<<"              Calorie Counter\n\n";
        cout<<"Please enter the amount of cookies you consumed:"; //prompt the user for cookies consumed
        cin>>amount; //accept user data
        while( amount > 40 || amount < 1) //validate user input
        {
            cout<<"That is an invalid response\n";
            cout<<"Please enter an amount less than 40 or greater than 0\n\n";
            cout<<"Please enter the amount of cookies you consumed:";
            cin>>amount;
        }
        total = perCookie * amount; //calculate the total
        cout<<"Amount of calories your consumed: " << total <<"\n\n\n\n";
        
    
    
    
    
    system("PAUSE");
    return 0;
    }
    I have it so that they have to enter a number greater than 0 but less than 40 because there is only 40 cookies in the bag. I was curious what would happen if I entered A into the amount variable and when I put it in the program crashed.

    How would I prevent that from happening? I would like to prompt them to enter a number not a letter but I don't know what way I would go about validating that.

  2. #2
    Join Date
    Nov 2007
    Posts
    74

    Re: Really new to programming

    Code:
    while(cin>>amount)
    {
        // Anything you love to do
        
        // Insert your break here
        if(>40 OR <1)
           break;
    }
    This offers better coding style, performance-wise

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Really new to programming

    Quote Originally Posted by MuggerHugger
    I have it so that they have to enter a number greater than 0 but less than 40 because there is only 40 cookies in the bag. I was curious what would happen if I entered A into the amount variable and when I put it in the program crashed.

    How would I prevent that from happening? I would like to prompt them to enter a number not a letter but I don't know what way I would go about validating that.
    In addition to checking for valid values, check if the read succeeded and clear the error flags, e.g.,
    Code:
    cin>>amount; //accept user data
    while(cin.fail() || amount > 40 || amount < 1) //validate user input
    {
        cout<<"That is an invalid response\n";
        cout<<"Please enter an amount less than 40 or greater than 0\n\n";
        cout<<"Please enter the amount of cookies you consumed:";
    
        cin.clear(); // Clear the error flags.
        // Ignore what was left on the buffer in preparation for new input.
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cin>>amount;
    }
    You should #include <limits> for std::numeric_limits and #include <ios> for std::streamsize.
    Last edited by laserlight; December 22nd, 2008 at 05:10 AM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Dec 2008
    Posts
    3

    Re: Really new to programming

    Ok, thanks a lot.

  5. #5
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Really new to programming

    Quote Originally Posted by MuggerHugger View Post
    Ok, thanks a lot.
    Please dont forget to mark threads as resolved, so people dont waste time; and dont forget to rate helpful posts.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured