Friday, June 29, 2012

Validating numerical data entered by the user

I was having dickens of a time figuring out how to keep my program from going into an endless loop when a user enters a character into a double or int data type variable.

For example, I was creating a function with a while loop that would validate the rate entered by the user so it couldn't be a negative rate.  It worked well for negative entries; however, this function would go into an endless loop when a character was entered:

double getRate()
{
    double dayRate;

    cout << "What is the daily charge rate? ";

    cin >> dayRate;

    // Validate the choice
    while (dayRate < 0)
        {
          
            cout << "Enter a rate that is not negative.";
            cin  >> dayRate;
        }

    return dayRate;
}

In order to fix this, I used "cin.fail()" as a test condition, which would validate whether or not the value entered fits the variable; and then, I flushed the instream and buffer with "cin.clear()" and "cin.ignore()" functions.  I'm not 100% sure I understand, but it seems the problem was that "cin >> dayRate" wasn't accepting the character and leaving it in the buffer memory, so that every time the cin operator was going to get the entry, it just kept finding that same character over and over, thus repeating the loop infinitely.

You also need to use "#include <ctype.h>" or "#include <cctype>" (depending on your compiler/system) in the header to call these functions.  It works perfect now.  I learned this technique at this link - http://www.cplusplus.com/forum/beginner/2957/

double getRate()
{
    double dayRate;

    cout << "What is the daily charge rate? ";

    cin >> dayRate;

    // Validate the choice
    while ((dayRate < 0) || (cin.fail()))
        {
          
            cout << "Enter a valid day rate (no negatives or letters).";
            cin.clear();
            cin.ignore(1000, '\n');
            cin  >> dayRate;
        }

    return dayRate;
}


Does anyone understand why we need to add or subtract a "1" to formulas used in C++ programming and Excel formulas when they are not part of the original algebraic equations?
 Ex:   P = _10,000_                                                                                           
              (1 + .042)n

Wednesday, June 20, 2012

Everyone still in class?  I can't log in and it looks like they didn't even bill me for the class.

Thursday, June 7, 2012

Assignment 4 - Walk Program

Can someone help me understand what the purpose of these steps are?  It seem like the code computes a number as an integer while also multiplying it by 10.5 and then converts it back to a float and divides it by 10.0.

My questions are:
1. why convert it to an int at all?
2. why multiply by 10.5? if you we just divide by 10.0?  won't that throw off the math?
3. why calculate 10.5 as "10.0 + 0.5"?  why not just "10.5"?

// Compute miles for each distance on the map
miles = float(int(distance1 * scale * 10.0 + 0.5)) / 10.0;
outData << "For a measurement of " << distance1
<< " the first distance is " << miles
<< " mile(s) long." << endl;
totMiles = totMiles + miles;


ANSWER TO MY OWN QUESTION:
That's a way to round the number to the nearest 10th.

Try some examples:
let x = distance*scale
if x = 12.345
then
10x+.5 = 123.95
int( 123.95 ) = 123
123/10 = 12.3

thus the number is correctly rounded to the nearest 10th.

Tuesday, June 5, 2012

Did you know...

If you purchased a used book (like I did) and it came without the CD you can download the contents 

By the way, you can also preview one chapter for Pearson titles while you wait for your book.  I found the first chapter under the instructor resources. If you're waiting for your financial aid but need access to online homework like MyLab they will allow you to use it for the first 17 days of class.  Just register and poke around the site for a bit for the links to your class materials. Customer service responds quickly. 
 Pearson Higher Ed link

... and nope I don't work for them I just had to get creative while I waited for the book :D


Monday, June 4, 2012

Trial and error

I completed example 4-11 but the file kept giving me incorrect output.  Figured out that if the user enters Y or N as capital letters the program runs fine however if caps are not used the program prints the wrong message.  Looks like a good example of how to predict user errors and a good place to use || (or). 

Academic Integrity

Just a reminder...  Please do not post any assignments, complete class programs, quiz or test answers.
Thanks!

Sunday, June 3, 2012

Welcome

I created this blog as a place for students who are not attending classes on campus can share tips and tricks that make life as a Distance Ed student easier.  

I look forward to hearing from you!