The University of Iowa
The College of Liberal Arts and Sciences
Department of Computer Science
Programming Languages and Tools:
CS:3210:0001
Lecture/Lab #5
Programming with C++
auto, understanding compilation errors, arithmetic
Warm-up
-
What type would you use...
-
to represent a boolean value?
-
to represent an integer?
-
to indicate that a function doesn't return anything?
-
for floating-point computations?
-
to store the value of a single character?
-
What operator gives us the size of a type in bytes?
-
to store a string?
auto
#include <iostream>
#include <cmath> // for std::sqrt
int main()
{
auto b = true; // a bool
auto ch = 'x'; // a char
auto i = 123; // an int
auto d = 1.2; // a double
auto z = std::sqrt(d); // z has the type of whatever
// sqrt(y) returns
auto bb{true}; // bb is a bool
}
We don’t need to state the type explicitly when it can be deduced from the initializer:
Recommendation: use auto by default, unless you have a specific reason to mention the type explicitly.
Exercise 1
Use `auto` to simplify function and variable declarations where it makes sense (without changing the meaning of the program)
-
Open the exercise template
-
Write your code, press Run to test
-
When you're done, grab your Repl's link and send it as a direct message to me (agurtovoy)
-
Click on the corresponding option in the "Lab5 exercises" poll in #general
Understanding compilation errors
-
General error message format:
source.cpp:<line number>:<column>: <error message>
-
Common error categories:
-
Syntax errors: missing/unnecessary semicolon, mismatching parentheses/brackets, unterminated string literal and other instances of incorrect language grammar
-
Semantic errors: undeclared identifier, missing return statement, mismatching types/number of function arguments and other type system-related and logical errors that can be caught at compile time
-
Link-time errors: missing function or variable definitions
-
Always start with the very first error
Exercise 2
Fix the program's compilation errors while using your best judgement to preserve the intent of the program
-
Open the exercise template
-
Write your code, press Run to test
-
When you're done, grab your Repl's link and send it as a direct message to me (agurtovoy)
-
Click on the corresponding option in the "Lab5 exercises" poll in #general
Arithmetic
Arithmetic operators
x + y // plus
+x // unary plus
x − y // minus
−x // unary minus
x * y // multiply
x / y // divide
x % y // remainder (modulus),
// ints only
Comparison operators
x == y // equal
x != y // not equal
x < y // less than
x > y // greater than
x <= y // less than or equal
x >= y // greater than
// or equal
Logical operators
x && y // logical and
x || y // logical or
!x // logical not
// (negation)
Bitwise operators (ints only)
x & y // bitwise and
x | y // bitwise or
x ^ y // bitwise xor
~x // bitwise complement
x << y // left shift
x >> y // right shift
Programming with C++, Spring 2020, Lecture #5
By Aleksey Gurtovoy
Programming with C++, Spring 2020, Lecture #5
auto, understanding compilation errors, arithmetic
- 581