Homework

I am having a hard time understanding if statements. This is what I have so far.
I wanted to make the program use the if statements, but it's not even running through my second function. I am not sure what I am doing incorrectly. Thank you for whoever takes their time to help out!

#include <iostream>
#include <string>
using namespace std;

int main()

{
int speed = 0;
int speedlimit = 0;
int overlimit = 0;
//double speedingfine;

char extrafinetype = ' ';


cout << "How fast were you going? ";
cin >> speed;
cout << "What was the speed limit? ";
cin >> speedlimit;

cout << "extra fines: (N)one, (S)chool zone, no (I)nsurance, suspended (L)icense? ";
cin >> extrafinetype;

overlimit = speed - speedlimit;

cout << "you were going " << speed << " in a " << speedlimit << " zone. (" << overlimit << " MPH over the limit) " << endl;
//speedingfine = ticketcalculation(overlimit);

double N = 0;
double S = 200.00;
double I = 100.00;
double L = 300.00;

cout << "excessive speed fine " << overlimit << endl;
system("pause");

return 0;
}

double ticketcalculation(double overlimit)


{

double fine = 0.0;


{
if (overlimit >= 1 && overlimit <= 9)

fine = 0;
}
{
if (fine > 10)
fine = 100.00;
}
{
if (fine > 20)
fine = 150.00;

}
{
if (fine >= 25)
fine = 250.00;
system("pause");
return fine;
}
}
















Could you please put it between code blocks? You can see it under format at the right.
Last edited on
Nevermind. Either way.

You are missing a closing bracket in you ticketCalculation funtion.

If you are having trouble with if statements, or anything else thats considered basic.
Please check out Bucky's tutorial (thenewboston). He is fantastic at teaching out. H

You can find the playlist here - https://www.youtube.com/playlist?list=PLAE85DE8440AA6B83

If you want to specifically learn about if statements. Watch video 8, 16 and 17.
closed account (2LzbRXSz)
When you define an int, it already starts with the value of 0. Though, giving it the value of 0 is fine too! Just wanted to let you know, in case you didn't:)

Now, my skills are a little rusty, but I'll try to give you the best advice that I can. I also don't know how far you are in your homework, so if any of this hasn't been covered by your course yet, feel free to pick and choose what to use, depending on what you already know.

Your program will never get to your second function, because it is not called in main (of course, not called outside of what you have commented). Your second function must also be defined before main, in order for it to it to work. You can either move your second function above main, or define it like this:
double ticketcalculation(double overlimit);
as long as it's before main!

If you'd like to brush up on your knowledge of functions:
http://www.cplusplus.com/doc/tutorial/functions/

Now, it's time to call your function, double ticketcalculation(double overlimit) in main Get it out of the comments! Try it out.

I almost forgot! Your if statements need a little help. You're on the right track, just not quite there.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
        if (overlimit >= 1 && overlimit <= 9)
        {
            fine = 0;
        }
        else if (fine > 10)
        {
            fine = 100.00;
        }
        else if (fine > 20)
        {
            fine = 150.00;
        }
        else if (fine >= 25)
        {
            fine = 250.00;
        }

Read more about if statements here!
http://www.cplusplus.com/doc/tutorial/control/

I tested it out myself, and it seems to work just fine:)

By the way, it's really recommended that you don't use system functions. For pausing, I like to use this_thread::sleep_for (std::chrono::seconds(int)); You'll need to #include <thread> and #include <chrono>

Read about it here!
http://www.cplusplus.com/reference/thread/this_thread/sleep_for/

Edit: I spent so long writing this, there were already two other answers by the time I finished lol.
Last edited on
Thank you guys so much! I spent a long time last night trying to fix my problems. I will check these when I get home from work! You guys are awesome!
closed account (2LzbRXSz)
Np, glad I could be apart of helping:)
cactus:
When you define an int, it already starts with the value of 0.


I just ran this:

1
2
3
4
5
{
	int speed; // = 0;
	double speedlimit; // = 0;
	float overlimit; // = 0;
		//double speedingfine; 


in VS2013, and as you can see, each variable initialized to a non-zero value:

int speed : initialized to -858993460
double speedlimit : initialized to -9.2559631349317831e+061
float overlimit : initialized to -107374176.

Generally true. You might be surprised how many answers in this forum consist of something like "xyz is a random value because you didn't initialize it - . . ."

You should never depend on the compiler or application to create a default value for you - you will likely save a lot of trouble if you start each variable at a known point.
Last edited on
closed account (2LzbRXSz)
@PCrumley48

Wow. Thank you so much for letting me know. I had a feeling giving it the value of 0 is more... "grammatically" (or should I say syntactically?) correct, but I had always just been told that an int starts with the value of 0. Sorry about my ignorance on the subject, and thanks again for correcting me:)
@cactus I was just wondering by what you mean when you state that I need to call it in my. Do you mean to have it within my "main int()" in between the "{}" ?
closed account (2LzbRXSz)
Yes. You technically already have it in int main() in between your curly brackets, you just have it commented out. Remove the // to uncomment it, and it'll be called.
@daernac Yes thats what he means. in the function you return "fine". in main you have to create a doubel variable(in your case called speedingFine) that catches that "fine" and calls the function when you need it -

 
speedingFine = ticketcalculation(overlimit);
I am getting a syntax error saying 'ticket calculation' identifier not found once I took the comment \\ out. this is what I have:


int main()

#include <iostream>
#include <string>
using namespace std;

int main()

{
int speed = 0;
int speedlimit = 0;
int overlimit = 0;
double speedingfine;

char extrafinetype = ' ';


cout << "How fast were you going? ";
cin >> speed;
cout << "What was the speed limit? ";
cin >> speedlimit;

cout << "extra fines: (N)one, (S)chool zone, no (I)nsurance, suspended (L)icense? ";
cin >> extrafinetype;

overlimit = speed - speedlimit;

cout << "you were going " << speed << " in a " << speedlimit << " zone. (" << overlimit << " MPH over the limit) " << endl;

speedingfine = ticketcalculation(overlimit);

double N = 0;
double S = 200.00;
double I = 100.00;
double L = 300.00;



cout << "excessive speed fine " << overlimit << endl;
system("pause");

return 0;
}

double ticketcalculation(double overlimit)


{

double fine = 0.0;


{
if (overlimit >= 1 && overlimit <= 9)

fine = 0;
}
{
if (fine > 10)
fine = 100.00;
}
{
if (fine > 20)
fine = 150.00;

}
{
if (fine >= 25)
fine = 250.00;
system("pause");
return fine;
}
}
You have to prototype your function. Add this above main -

double ticketcalculation(double overlimit);
closed account (2LzbRXSz)
Along with prototyping your function like @TarikNeaj said, you have another error, and it had me stumped. I had no idea what was causing it, but then I realized you have an extra int main() above your #include <iostream>
@cactus I saw that aswell, but I assumed it was a typo when he posted the code here... surely it must have been?
closed account (2LzbRXSz)
@TarikNeaj I think it was. I just copy and pasted the whole thing, so I didn't even realize at first.
@cactus Haha ye happens :)
@Cactus and @TarikNeaj you guys are awesome! Also, I just remembered about prototyping. I need to read more on functions! Thank you so much!
closed account (2LzbRXSz)
@daernac Thank you! You too haha.
No problem. Again, I'm glad I could help:)
Topic archived. No new replies allowed.