cppreference.com- strongly statically typed
- not a pure OOP, more hybrid
example:
// preprocessor directive
// contains all the definitions you need for text input / output streams
#include <iostream>
// tells the program you are using a name grouping
// dont then have to do std::cout
using namespace std;
// comments go up here, purpose, header
int main() {
// program bodyz
// for now, return 0
// exit code 0 means no errors specifically in the main function
return 0;
}
- main is a reserved function name, is run first
#include <iostream>
using namespace std;
int main() {
float inches, mm;
cout << "A string sentence that will print to console"
cout << "More string sentence" << endl;
cin >> inches;
cout << "More output here" << inches << "more even"
}
- `<<` means insertion operator, pushes contents into the `cout ` stream
- `endl` new line
- `>>` extraction operator, accepts input from the console
variables
- generally declared at the top of the function
- avoid defining local variables within blocks
// can declare line by line
float feet;
// or same typed together
float inches, mm;
- these are uninitialized, and can be very dangerous. Complier can do whatever it wants. Try to always assign values at time of variable creation.
- type is declared
- must start with a letter, cannot use
$ - primitive data types:
float, double, int, short, long, char, boolfloatis small,doubleis bigger
stringis not listed as it is user defined data type and not built into the language- this class will use arrays of characters to represent strings
- avoid global variables (defined outside of a function)
- constant globals are fine
- all caps
const int A_VERY_CONSTANT = 42
stream stuff
cin >> inches- will skip leading whitespaces
- if the wrong datatype is in the input buffer, silent error
- sometimes will need to use
cin.clear()to clear the error state - will stay at the same place for the mismatched variable, even though content was entered, in the iostream
- sometimes will need to use
\nnewline character\escape character- can behave similarly to
endlin linux environments
cin.ignore(number, '\n')cin.ignore(number)do not use, unsafe
cin.get()- reads a single character, takes out of the stream
cin.peek()- looks into the stream without removing it
- does not modify buffer
- looks into the stream without removing it
io formatting
- precision matters
- default, no more than 6 digits
cout.precision(3)- sets the precision for cout until changed otherwise
cout << setprecision(3)- only sets the precision for the cout in the stream currently used for
cout.setf(ios::showpoint)- set format can be passed a number of formatting specifications