Write a program to evaluate the fuel consumption of a car. Have the user input the miles on the car’s odometer?

Write a program to evaluate the fuel consumption of a car. Have the user input the miles on the car’s odometer at the start and end of the journey, and also the fuel level in the tank (in gallons) at the start and end of the journey. Calculate fuel used, miles travelled, and the overall fuel consumption in miles travelled per gallon of fuel. Print these values, accompanied by appropriate messages indicating what they are.

Named constants:

We talked in lecture about naming conventions for constants: a name in all-caps usually indicates a constant. C++ allows you to instruct the compiler to insist that a variable stay constant throughout the program. You can do this by putting the keyword const before the variable declaration. For example, const double PI = 3.14159; declares a variable named PI that can never be changed after its initial assignment to 3.14159. To attempt to change the value of such a variable – a named constant – is a syntax error. Effectively, we’ve simply defined another name for the constant value 3.14159, which of course we aren’t allowed to change.

Operator shorthands:

C++ provides a number of shortcuts for simple arithmetic expressions. Often you’ll want to modify the value of a variable by doing something to its current value – adding 1, multiplying by 42, etc. We could express this as something like x = x + 2;. Because this is such a common need, though, C++ allows us to simply write x += 2;. The same syntax works for other mathematical operators: -=, *=, /=, %= are all valid operators.

There is an even shorter shortcut for saying x += 1;: We can use the pre-increment operator (syntax: ++variable) to add 1 to a variable before evaluating it, and the post-increment operator (syntax: variable++) to add 1 after evaluating it. For instance, if y is set to 3, x = y++; will set x to 3 and y to 4, while x = ++y; will first set y to 4, then x to 4, as well.

Update:

C++...Visual studio

Please enter comments
Please enter your name.
Please enter the correct email address.
You must agree before submitting.

Answers & Comments


Helpful Social

Copyright © 2024 1QUIZZ.COM - All rights reserved.