Calculations

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 43
Reputation: hopeolicious is an unknown quantity at this point 
Solved Threads: 0
hopeolicious hopeolicious is offline Offline
Light Poster

Calculations

 
0
  #1
Apr 7th, 2005
how do I get my calculations like ex. I want to add up the average gallons used per mile for a car if the car went like 1450 miles and used 62 gallons of gas

I dont want it to be a whole number i want it to be like 23.38 instead of 23.00
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: Calculations

 
0
  #2
Apr 7th, 2005
instead of declaring it as an int, use a float or a double
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 43
Reputation: hopeolicious is an unknown quantity at this point 
Solved Threads: 0
hopeolicious hopeolicious is offline Offline
Light Poster

Re: Calculations

 
0
  #3
Apr 7th, 2005
i decared it as a float and now i want to get the average of gallons per mile for all the 5 cars
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. struct Car
  6. {
  7. int number, miles;
  8. int gallons;
  9. float averagegallons;
  10. };
  11.  
  12.  
  13. int main()
  14. {
  15. const int NUMCAR = 5;
  16. int i;
  17. Car Cars[NUMCAR];
  18. for(i=0;i<NUMCAR;i++)
  19. {
  20.  
  21. cout << "\nPlease enter car number: ";
  22. cin >> Cars[i].number;
  23.  
  24. cout << "\nPlease enter car miles driven: ";
  25. cin >> Cars[i].miles;
  26.  
  27. cout << "\nPlease enter car gallons: ";
  28. cin >> Cars[i].gallons;
  29. cin.get();
  30.  
  31. Cars[i].averagegallons = Cars[i].miles / Cars[i].gallons;
  32. }
  33.  
  34. cout << "\n\n Car Report" << endl;
  35. cout << " Number Miles Driven Gallons Used Average Per Car" << endl;
  36. for(i=0;i<NUMCAR;i++)
  37. {
  38. cout << setiosflags(ios::showpoint);
  39. cout << setprecision(2);
  40. cout << Cars[i].number << "\t" << Cars[i].miles << "\t" << Cars[i].gallons << "\t" << Cars[i].averagegallons
  41. << endl;
Last edited by hopeolicious; Apr 7th, 2005 at 12:49 pm. Reason: make clearer of what i intend to do
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,398
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 245
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Calculations

 
0
  #4
Apr 7th, 2005
An int divided by an int results in an int using int division.
struct Car
{
int number, miles;
int gallons;
float averagegallons;
};
/* ... */
Cars[i].averagegallons = Cars[i].miles / Cars[i].gallons;
To keep them as ints, cast one of the operands; this will cause the division to be done as floating point.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC