| | |
Calculations
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2004
Posts: 43
Reputation:
Solved Threads: 0
i decared it as a float and now i want to get the average of gallons per mile for all the 5 cars
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using namespace std; struct Car { int number, miles; int gallons; float averagegallons; }; int main() { const int NUMCAR = 5; int i; Car Cars[NUMCAR]; for(i=0;i<NUMCAR;i++) { cout << "\nPlease enter car number: "; cin >> Cars[i].number; cout << "\nPlease enter car miles driven: "; cin >> Cars[i].miles; cout << "\nPlease enter car gallons: "; cin >> Cars[i].gallons; cin.get(); Cars[i].averagegallons = Cars[i].miles / Cars[i].gallons; } cout << "\n\n Car Report" << endl; cout << " Number Miles Driven Gallons Used Average Per Car" << endl; for(i=0;i<NUMCAR;i++) { cout << setiosflags(ios::showpoint); cout << setprecision(2); cout << Cars[i].number << "\t" << Cars[i].miles << "\t" << Cars[i].gallons << "\t" << Cars[i].averagegallons << endl;
Last edited by hopeolicious; Apr 7th, 2005 at 12:49 pm. Reason: make clearer of what i intend to do
An int divided by an int results in an int using int division. To keep them as ints, cast one of the operands; this will cause the division to be done as floating point.
struct Car
{
int number, miles;
int gallons;
float averagegallons;
};
/* ... */
Cars[i].averagegallons = Cars[i].miles / Cars[i].gallons; "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
![]() |
Similar Threads
- Trouble writing code for arithmetic calculations of complex numbers (C++)
- Calculations, Variables and Constants - Please help (VB.NET)
- Price Calculations (C#)
- random number generator and calculations (Java)
- Mortgage Calculations (C++)
- calculations in java (Java)
- Java Multidimensional Arrays (Java)
- simple calculations How? intHrlyRate, etc Help me (ASP)
Other Threads in the C++ Forum
- Previous Thread: Address Book.
- Next Thread: drawing - stickman with array
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg simple string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






