# Thread: How come my maths not working?

1. ## How come my maths not working?

Code:
```#include <stdio.h>

int main (int argc, const char * argv[])
{

int location, vehicle, weekday, peak, km, days;
float price, insurance;

printf("Please choose a location(1: Toronto 2: Missisauga 3: Milton):\n");
scanf("%d", &location);

switch (location) {
case 1:
printf("Which type of car(1: Pickup Truck 2: Cargo Truck 3: 10' Truck 4: 14' Truck 5: 20' truck):\n");
scanf("%d", &vehicle);

if (vehicle==5){
printf("not avail.\n");
break;}

printf("Enter weekday (1-Mon, 2-Tues 3-Weds,4-Thurs, 5-Fri, 6-Sat, 7-Sun):\n");
scanf("%d", &weekday);
if (weekday > 4){
peak = 1;}
else peak == 0;

printf("Enter number of KMs driven:\n");
scanf("%d", &km);

printf("Would you like insurance:(1-Yes/0-No)\n");
scanf("%d", &insurance);

if (insurance==1){
printf("How many days will you have the car for?\n");
scanf("%d", &days);
insurance=30*days;}
else {insurance==0;}

if (vehicle==1 && peak==1){
price=0.50*km+15;}

if (vehicle==1 && peak==0){
price=0.50*km+12.50;}

printf("Total is: %lf Insurance: %lf\n",price,insurance);
}

/*
break;
case 2:
printf("you chose green color\n");
break;
case 3:
printf("you chose blue color\n");
break;
default:
printf("you did not choose any color\n");

*/

}```
Compiling fine, just the the maths not making sense.

IGNORE THE COMMENT AT BOTTOM.

2. The problem with the math lies here:

Code:
```if (vehicle==1 && peak==1){
price=0.50*km+15;}

if (vehicle==1 && peak==0){
price=0.50*km+12.50;}

printf("Total is: %lf Insurance: %lf\n",price,insurance);```
If you enter anything except for '1' for "vehicle," then no calculations are performed and the variables contain junk. It is good practice to initialize your variables when they are declared; then you'd at least get predictable results.

--

Some other errors in your code:

Code:
```else peak == 0;
// ...
else {insurance==0;}```
You're confusing the assignment operator (=) and the equality operator (==).

---

Also, you're (correctly) declaring "main()" as returning an integer, but you aren't returning anything in your code.

Code:
`return 0;`
---

You also do not seem to be using the "switch()" statement the way it is intended.

3. Also, you should be checking to see if you have warnings when you compile. This will help uncover some of the problems I've mentioned.

Code:
```||In function 'main':|
|26|warning: statement with no effect|
|32|warning: format '%d' expects type 'int *', but argument 2 has type 'float *'|
|38|warning: statement with no effect|
|64|warning: control reaches end of non-void function|
||=== Build finished: 0 errors, 4 warnings ===|```

4. thanks fix those problems. can you please tell me why this is not computing correctly?

Code:
```printf("Enter number of KMs driven:\n");
scanf("%d", &km);
if (km>100){
extra_km=(km-100)*0.50;
}
else extra_km=0;```

5. What makes you think it is not computing correctly?

6. This is what I'm getting:

~/c_programming> ./km
Enter number of KMs driven:
1000
Extra KM: -0.00

---

Basically I want it so if its over 100 KM that it gets charged 0.50cent/KM minus the base 100

7. Works for me:
Code:
```#include <stdio.h>

int main(void)
{
int km;
double extra_km;

printf("Enter number of KMs driven:\n");
scanf("%d", &km);
if (km > 100) {
extra_km = (km - 100) * 0.50;
}
else
extra_km = 0;

printf("Extra KM: %f\n", extra_km);

return 0;
}```
Output:
Code:
`Extra KM: 450.000000`

8. Hmm yeah that fixed it? I guess it was the spacing? Grammar? o_O

9. I suspect it was the type of extra_km.