Skip to content

Instantly share code, notes, and snippets.

@paranoiacblack
Forked from itsjohncs/input1.txt
Created October 10, 2012 16:44
Show Gist options
  • Save paranoiacblack/3866793 to your computer and use it in GitHub Desktop.
Save paranoiacblack/3866793 to your computer and use it in GitHub Desktop.
CS 10 SI Classroom Session 2 - Example 2
twelve
sixteen
#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