Notes from 9/17

a2sol.cpp

// Assignment 2 - temperature conversion
// CS102 Fall 2012
// Chris League
#include <iostream>
using namespace std;
int main()
{
    cout.setf(ios::fixed);
    cout.precision(1);    
    cout << "Enter temperature (C): ";
    float celsius = 0.0;
    cin >> celsius;
    float fahrenheit = celsius * 1.8 + 32; 
    cout << "That is " << fahrenheit << " F.\n";
    return 0;
}
// In future, comments and indentation will matter
// for grading.

equal-mistake.cpp

#include <iostream>
using namespace std;
int main()
{
    int x;

    x = 99;  // assignment: set x to 99
    x == 99; // equality: true/false question (true)
    x == 42; // equality: (false)
    
    if(x == 99) // if expression true, then
    {           // execute body in braces
        cout << "x is 99\n";         
    }

    if(x == 42) // This expression is false
    {           // so we skip this body in braces
        cout << "x is 42\n";
    }

    if(x = 42) // ASSIGNMENT, changes x!
    {          // (probably not what you meant)
        cout << "x is 42 now!\n";
    }

    if(x = 0) // ASSIGNMENT, changes x!
    {         // but also evaluates to RHS, which is false.
        cout << "x is 0 now!\n";
    }

    // Multiple assignment:
    int y, z;
    y = z = 42; // Assigns 42 to both variables

    return 0;
}

if-else.cpp

#include <iostream>
using namespace std;
int main()
{
    int x;
    cout << "Type an integer: " << endl;
    cin >> x;
    if(x > 50)
    {
        cout << "That is big!\n";
    }
    else
    {
        cout << "That is small.\n";
    }
    cout << "Both join here.\n";    
    return 0;
}

temp-if.cpp

// Assignment 2 - temperature conversion
// CS102 Fall 2012
// Chris League
#include <iostream>
using namespace std;
int main()
{
    cout.setf(ios::fixed);
    cout.precision(1);    
    cout << "Enter temperature (C): ";
    float celsius = 0.0;
    cin >> celsius;
    float fahrenheit = celsius * 1.8 + 32; 
    cout << "That is " << fahrenheit << " F.\n";
    if (fahrenheit > 80 )
    {
      cout << " it's hot\n";
    }
    if(fahrenheit >= 50 && fahrenheit <= 80)
    {
        cout << "it's mild\n";
    }
    if(fahrenheit < 50)
    {
        cout << "It's cold!"<< endl;
    }
    
    return 0;
}