# using double

This is a discussion on using double within the C Programming forums, part of the General Programming Boards category; ok so the program i have to right for class has me defining a constant call it A well later ...

1. ## using double

ok so the program i have to right for class has me defining a constant call it A

well later in the program i want to call it as a double... ive tried looking all over and when i do something like

a = (double(b/A)*11);

something along those lines it gives me a syntax error....

any hints or help would be great thanks

2. Originally Posted by surfingbum18
a = (double(b/A)*11);
That's a C++-style cast. You need:

Code:
`a = (double)(b) / A * 11.0;`
Notice that I didn't put (b/A) inside the cast. If b and A are both integers, then (b/A) is divided as an integer, even though you are casting the result. That would not be correct.

3. Originally Posted by brewbuck
That's a C++-style cast. You need:

Code:
`a = (double)(b) / A * 11.0;`
Notice that I didn't put (b/A) inside the cast. If b and A are both integers, then (b/A) is divided as an integer, even though you are casting the result. That would not be correct.

what if instead of dividing im trying to do a mod would it be

a = (double)((b)%A)*11;

keeping that in mind A is the defined constant at the beginning of my program no matter how i move it around it still comes out wrong when i run the program it returns 11... thanks

4. Originally Posted by surfingbum18
what if instead of dividing im trying to do a mod would it be

a = (double)((b)%A)*11;
That doesn't make sense though. Modulus is an integer-only operation. You are multiplying by an integer. So I do not see why you are casting it to double.

If all you are trying to do is convert the entire expression to double, just cast the whole thing:

Code:
`a = (double)(b % A * 11);`

5. Code:
```//Cost to buy chips.

//This is for the buying chips

int main (){

if (choice == 1){

}

return 0;
}

printf("How much cash do you want to spend for chips?\n");
printf("\n");

//Nullify transaction if the buyer wants to spend more money than allowed.

printf("Sorry, you do not have that much money. No chips bought.\n\n");

//Execute transaction.

else{

}
return numchips;
}```

okay i just extracted the part of the code that matter because i really dont feel like having the entire code on the interent due to the fact that its homework at a university...

but what im have issues with is say you buy 500\$ worth of chips so when you divide 500 by the 11 you get 45 chips and should have \$5 left over from the transaction that should be thrown back into your original amount of money... the original amount of money is cash so how do i get the left over money to get back to cash... the if statement in main is where im having my issues with cash

6. Code:
`     *cash -= buy_cash;`
Why not make it simple, and here instead do:
Code:
`     *cash -= numchips * CHIP_BUYING;`
Then you won't have to adjust the cash in main(). And I would use a better name for CHIP_BUYING like CHIP_COST or cost_per_chip, or price_per_chip.