Sunday, October 11, 2009

Final Insurance Rate Program

Finished the insurance fee program. I think it's accurate. It's not copy & pasting right but this shows where I fixed the errors I was confused about.


/* This program prints the annual insurance fee for a person based on their car value, age, and number of tickets they have received*/

#include

#include


using namespace std;


int main ()


{

int carValue, age, numberOfTickets;

cout << "Enter your car value: \n";

cin >> carValue;

cout << "Enter your age: \n";

cin >> age;

cout << "How many tickets have you received? \n";

cin >> numberOfTickets;

float baseRate;

baseRate = (carValue*.05);

float ageRate;

if (age<=24)

ageRate=(carValue*.0575);

else if ((age>=25) && (age<=29))

ageRate=(carValue*.055);

else

ageRate=baseRate;

switch (numberOfTickets)

{

case 0: cout << "Your annual insurance rate is " <<>"\n";

break;

case 1: cout << "Your annual insurance rate is " <<>1.10 << "\n";

break;

case 2: cout << "Your annual insurance rate is " <<>1.25 << "\n";

break;

case 3: cout << "Your annual insurance rate is " <<>1.50 << "\n";

break;

default: cout << "Hop on a bike and start peddling. You are so denied.";

}

return 0;

}




/* program output */




Running…

Enter your car value:

10000

Enter your age:

35

How many tickets have you received?

1

Your annual insurance rate is 550


Running…

Enter your car value:

15000

Enter your age:

29

How many tickets have you received?

2

Your annual insurance rate is 1031.25


Running…

Enter your car value:

850

Enter your age:

19

How many tickets have you received?

3

Your annual insurance rate is 73.3125


Running…

12500

Enter your age:

81

How many tickets have you received?

4

Hop on a bike and start peddling. You are so denied.

Building an Insurance Cost Calculator in C++

My assignment this week is to build an insurance rate calculator in C++ using the simplest program possible.

***disclaimer: I am learning C++ and there is a chance that what I have written below is incorrect. I will update with corrections when I get my assignment back.

=================

The assignment variables are as follows:

1) The base rate is 5 percent of the car value
2) If the person is younger than 24, add on 15% of the base rate to the total rate
3) If the person is between 25 and 29, add on 10% of the base rate to the total rate
4) If the person is older than 30, it's just the base rate
5) If the person has 1 ticket, add 10% to the age rate
6) If the person has 2 tickets, add 25% to age rate
7) If the person has 3 tickets, add 50% to age rate (sucks for them)
8) If the person has 4 tickets, deny them coverage (muhahahaha)

Ok, so that's it. Sounds simple, right? Well, maybe it is, but for someone without a programming background, this is tough.

Ok, so our teacher said to break it down piece by piece. Get it working, then simplify.

I'm also supposed to use "Switch," "If," and "Else" which I don't completely understand yet. Hopefully by the end of this post, I will get it.

============
Integer inputs needed to solve problem (cin >>)

Int CarValue
Int Age
Int NumberOfTickets

============
Other Integers

AgeRate
FinalRate

============

Breaking down the math...

BASE RATE

BaseRate=(CarValue*.05)

AGE RATE

If Age is <=24 AgeRate=BaseRate+(BaseRate*.15)
//if age is less than or equal to 24
AgeRate = CarValue*.05+(Car Value *.05 * .15)
AgeRate = Car Value * .05 + Car Value * .0075
AgeRate = Car Value * .0575

If Age is >=25 and <=29 AgeRate=BaseRate+(BaseRate*.10)
//if age is greater than or equal to 25 and less than or equal to 29
AgeRate = CarValue*.05 + (Car Value *.05 *.10)
AgeRate = Car Value * .05 +Car Value *.005
AgeRate = Car Value *.055

FINAL (TICKETS) RATE

If NumberOfTickets = 0
FinalRate = AgeRate

If NumberOfTickets = 1
AgeRate*1.10
If NumberOfTickets = 2
AgeRate*1.25

If NumberOfTickets = 3
AgeRate*1.50

If NumberOfTickets >=4
Denied. Sucka.

=====================

/* Ok, good, I think the math is right there. Now I just have to figure out how to write this program. This is the part where I get really lost. Let's see...*/

===============

Understanding If and Else and the "Logical Operators" (And, Or, Not)

Even though ultimately I want to write this program using Switch, I need to understand If and Else, which is what I'd use if I couldn't simplify with Switch.

//stolen from textbook

"The if statement enables you to test for a condition and branch to different parts of your code, depending on the result. The simplest form:

if (expression)
statement;

The expression in the parentheses can be any expression, but usually contains one of the relational expressions. If the expression has the value false, the statement is skipped. If it's true, it's executed.

if (bigNumber > smallNumber
bigNumber = smallNumber;

So...

{
if (age<=24);
ageRate=(carValue*.0575);
}

{
if ((age=>29) && (age=<24));
ageRate=(carValue*.055);

{
else
ageRate=baseRate;
}

// the logical operators: (And = && ) (Or= ||) ( Not= !)

==============

Let's Switch

Ok, so I'm not sure if I'm supposed to use Switch for just number of tickets, or if I should use it for the ageRate too. Hmm...

First, let me see if I can figure out "Switch."

Stolen from the textbook:

"Unlike if, which evaluates one value, switch statements enable you to branch on to any of several values. The general form of the switch statement is:

switch (expression)
{
case valueOne: statement;
break;
case valueTwo: statement;
break;
default: statement
break;
}

expression is any legal C++ expression, and the statements are any legal C++ statements. If one of the case values matches the expression, program execution jumps to those statements and continues to the end of switch block, unless a break statement is encountered." Also, if nothing matches, it goes to the optional default statement || no default exists and execution falls through switch commands and basically ignores them.

/* Now what's confusing me the most is figuring out where I define what the case is. Also, does it make sense to do Switch for ageRate or is that simple enough that it doesn't need switch? I guess I might be able to make the entire program written in one switch statement, but I'm not sure how I'd do that. Good thing I have all of Sunday afternoon and unlimited blog space to figure this out

Maybe the case name doesn't matter, you just have to label it.
*/

/*ugh*/

/*$*!@*/

/* 30 minutes later. Ok, I'm not getting this. I'm going to try a different approach, starting with the beginning of the program and asking for inputs, then seeing how I can apply switch to them */

#include
using namespace std;
int main(void)

{
int carValue;
cout << "Enter your car value: ";
cin >> carValue;
float baseRate = (carValue*.05)

switch (ageRate)
case

/*huh?*/

/*Google searching "complex switch C++"*/

/*FINALLY figured out what was confusing me... thanks to a devmaster.net thread ... "You can't use ranges in switch statements. Not supported by the language, sorry." Ooooh. So, I can't switch for AgeRate BUT I can for Tickets since that's just a number. OMG. No wonder I was confused. Let's see if this works.*/


#include
using namespace std;
int main(void)

{
int carValue;
int age;
int numberOfTickets

cout << "Enter your car value: ";
cin >> carValue;
cout << "Enter your age: ";
cin >> age;
cout >> "How many tickets have you received?";
cin >> numberOfTickets

float baseRate = (carValue*.05)

{
if (age<=24);
ageRate=(carValue*.0575);
}

{
if ((age=>29) && (age=<24));
ageRate=(carValue*.055);

{
else
ageRate=baseRate;
}


switch (finalRate)
{
case 0: cout >> "Your annual insurance rate is "ageRate<<"\n;
break;
case 1: cout >> "Your annual insurance rate is "ageRate*1.10<<"\n;
break;
case 2:cout >> "Your annual insurance rate is "ageRate*1.25<<"\n;
break;
case 3:cout >> "Your annual insurance rate is "ageRate*1.50<<"\n;
}


{
if numberOfTickets =>4;
cout >> "Hop on a bike. You are so denied.";
}

return 0;
}


/*this is probably incorrect but at the very least I have the start of a program that could feasibly work. Time to move it over to the compiler and see what happens. Holding breath.*/

=========================
If Debugging=Fun cout>>"Liar."

Running program in xcode for error search...

Well, that only had 14 errors and 4 warnings. :) Time to get down to the real work.

#include expects "FILENAME" or

ok, so I copied that top from some blog somewhere. I'm replacing with the top part I used in my last assignment that seemed to work fine.

Down to 12 errors but now at 6 warnings.

Expected initializer before 'cout' for cout << "Enter your care value: ";

I forgot a semicolon...

Now at 11 errors and 7 warnings, huh?

Unused variable: carValue

but I do use it. Or maybe it has to be within the same brackets. Getting rid of the ints I'm defining up top...

10 errors, 4 warnings...

Expected constructor, destructor, or type conversion before '>>' token

Moved the ints to the same place i'm asking for cins and the program seems to like that.

9 errors, 4 warnings...

No match for 'operator>>' in'std::cout>> "How many tickets have you received?"

arrows going wrong way, duh. also adding semicolon I'm missing on next line...

7 errors, 4 warnings

'carValue' was not declared

have a feeling this has to do with brackets

6 errors, 4 warnings...

no, wait... 9 errors, 4 warnings... what???

Expected unqualified-id before '{' token

... down to 4 warnings with the following code:


#include

#include


using namespace std;


int main ()


{

int carValue, age, numberOfTickets;

cout << "Enter your car value: \n";

cin >> carValue;

cout << "Enter your age: \n";

cin >> age;

cout << "How many tickets have you received? \n";

cin >> numberOfTickets;

float baseRate;

baseRate = (carValue*.05);

float ageRate;

if (age<=24)

ageRate=(carValue*.0575);

else if ((age<=24) && (age>=29))

ageRate=(carValue*.055);

else

ageRate=baseRate;

float finalRate

switch (finalRate)

{

case 0: cout << "Your annual insurance rate is ageRate \n";

break;

case 1: cout << "Your annual insurance rate is >> ageRate*1.10 \n";

break;

case 2:cout << "Your annual insurance rate is >> ageRate*1.25 \n";

break;

case 3:cout << "Your annual insurance rate is >> ageRate*1.50 \n";

break;

}

if (numberOfTickets >=4)

cout << "Hop on a bike. You are so denied.";

return 0;

}



================


/*more when i get this figured out*/




Learning C++

I put Javascript on hold and decided to learn C++ first. I'm convinced if I can figure out C++ my brain will understand programming overall and it will make learning other computer languages easier.

I'm taking two courses at the local community college: Intro to C++ and Intro to Data Management Systems. So far I really like the Intro to C++ course, but I'm not quite at the point where I feel comfortable with the language. Granted, I've only had 3 classes as of today. Last week we had to write a BMI calculator from scratch. It took me a while to figure out. She gave us the math formula, but we had to figure out the entire program in our compiler. Once I got the hang of int and float, it made a little more sense.

This week, though, our assignment is harder, and I'm struggling. We're doing Switch and If and Else. Our assignment is to build a insurance rate calculator. There are a bunch of different variables. In my next post, I'm going to work through the code until I figure it out.


Tuesday, September 8, 2009

linux

My boyfriend is IM'ing me through installing linux because he says I should use Linux, not Mac, if I want to even think of learning to program. Since all my nerd friends use Linux I am going to trust him on this one. Downloading via Ubuntu. Installation will be... interesting.

Trying to figure out the basics

Perhaps I should be tweeting my trials and tribulations instead of blogging. At the moment I've downloaded the MAMP software and can't figure out how to install it. I'm a total noob. Hopefully once I get going, as in, have all the stuff I need installed properly, these posts will be longer and more insightful.

I hate being a noob. It hurts my brain. But then again, learning is fun.

Getting back into this, for real this time...

Ok, ok. So I'm super ADD and I get massively distracted by life every time I sit down to teach myself how to program. I didn't even get past chapter 3 in my JavaScript book. I'm ashamed.

The good news is, I signed myself up for two computer sci classes at the community college for the fall. The professors are supposed to be awful (thanks ratemyprofessors.com for getting me real excited about these classes, not) BUT I'm hoping that a structured into to programming will, at the least, get me on the right track.

At the moment I'm signed up for Intro to C++ and Intro to Data Management Systems. The names of both those classes alone scare me to bits. But if programming is a language, it should be learnable, at least to the point where I can not be so deathly afraid of it and look at programmers as if they are gods with minds far beyond the intellect of normal folk like myself. Or, I'll be able to understand exactly WHY they are gods and I have a feeling it has nothing to do with knowing "how" to program, more like "how" to organize things with a language that anyone can learn.

Tonight, however, I'm staring at my Beginning PHP5, Apache, MySQL Web Development book and considering trying to get through a chapter or two. I'm under the weather and have the night to my lonesome, so might as well start learning something... even though PHP is going to be very different from C++, which I'll begin tackling in a few weeks.

I'm not even sure PHP5 is current anymore, but hopefully the book is still good. I have a bagillion random programming books that I've bought over the last 3 years or so. Time to put them to use. Really. I mean it this time.

Saturday, April 4, 2009

Getting Back into This

I'm back. And hoping to move on to the next lesson this weekend. I finally have some time to focus on learning to code. It's important to do, so I'm going to try to hold myself accountable for staying on top of studying.