| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
|||
|
|||
|
C++ Help!!!
Okok, I'm new here so be gentle...
Ok, I need help w/ some simple programming stuff. So, tell me why this lil snippet doesn't want to work: cout << "The cost of carpet is: $"; cout << (price_per_yard*(((length_ft*width_ft)/2)/3))*STAX; STAX is a const of a couple bucks. length_ft is an integer width_ft is also an integer price_per_yard is an float. All of this crap is to a program I'm TRYING to write to compute carpet sales, and this lil part right here is the part that is supposed to compute the cost of the carpet after taking into affect the sales tax, square yards, and price per yard... Anyone have any suggestions cause I've tried everything... Thanks. |
|
#2
|
|||
|
|||
|
I dont understand why you would want to devide something by 2 and then devide it by 3, why not simply devide it by 6?
It may not solve your problem, but it will make it a lot easier to find your mistake |
|
#3
|
|||
|
|||
|
Help!
The reason is because length_ft multiplied by width_ft and then divided by 2 gives me the square footage of the room. Well, after THAT, I have to convert the square footage to square yards. So, I didvide it by 3... ANYONE know how to help me in this?
|
|
#4
|
|||
|
|||
|
Have you tried the following?
float total = 0; total = legth_ft*width_ft; total = total/6; total = total * STAX; total = total * price_per_yard; cout << "The cost of carpet is: $"; cout << total; This isn't a good way to program I know, but if this works you can try to put the puzzle together one piece at a time and find out where the problem is located Greetz |
|
#5
|
|||
|
|||
|
I'm gonna assume your main problem is that you are not getting the right answer, so I'm not gonna address issues like whether or not the code should be seperated to be more readable. The basic problem is that if you do ANY operation with an integer, the answer will be an integer. You need to cast the integers to type double (or float, if you must) and use constants of type double (of course, you don't have to permanently change the type of your declared constant if you don't want to). So the formula would look like this:
cout << (price_per_yard*(((static_cast<double>length_ft*static_cast<double>width_ft)/2.0)/3.0))*static_cast<double>STAX; Now, I have used double because I would declare price_per_yard as a double (just a thang...), I am, however, 99.9% sure you could just change everywhere I have double to float... Just note also that where you divide by 2 and then 3, you need to divide by 2.0 and 3.0. Hope this helps... |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > C++ Help!!! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|