-
December 21st, 2008, 09:59 PM
#1
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.
-
December 22nd, 2008, 04:56 AM
#2
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
-
December 22nd, 2008, 05:06 AM
#3
Re: Really new to programming
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.
-
December 24th, 2008, 01:32 AM
#4
Re: Really new to programming
-
December 25th, 2008, 10:20 PM
#5
Re: Really new to programming
Originally Posted by MuggerHugger
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|