-
-
Save paranoiacblack/3866793 to your computer and use it in GitHub Desktop.
CS 10 SI Classroom Session 2 - Example 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
237.87 | |
9.52 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
twelve | |
sixteen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main() { | |
// Again, it is only acceptable to leave built-in types uninitialized | |
// if you solely plan to fill them with user input. Even then, you should | |
// be safe and initialize them to default values. | |
double milesTraveled; | |
double gallonsUsed; | |
// From the user's side, what's wrong with just asking for input like this? | |
// | |
// On the other hand, from the developer's side, what assumptions are you | |
// making that makes this okay? | |
cin >> milesTraveled; | |
cin >> gallonsUsed; | |
// There could be an error here. | |
// 1. What type of error is it? | |
// 2. What values trigger it? | |
// 3. What does the program do if you use those values? | |
double milesPerGallon = milesTraveled / gallonsUsed; | |
// What's better? | |
// 1. cout << milesPerGallon | |
// 2. cout << milesTraveled / gallonsUsed | |
// Why? | |
cout << "You averaged " << milesPerGallon << " MPG." << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment