Midterm1CheatSheet

Midterm1CheatSheet - Types of Errors a. Runtime Error:...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Types of Errors a. Runtime Error: logic error b. Compile-time Error: syntax error, will not allow program to be compiled Types of Data int (integer); double (decimal); bool (T or F); char (characters); string (“Hi” sequence of characters) Declare Variables int x; double number; identifier must start with a letter or _ Initializing Variables int x = 0; string name = “John”; NEED QUOTES! Constants Fixed value variables const int days_in_year = 365; const double pi = 3.14; Arithmetic Operators + - * / % Note: When arithmetic operators used with only integers, the result does not yield a decimal and will round down Ex: a = 11%3; // 11/3 take remainder a = 2 Compound Assignment +=; -=; *=; /=; %=; Increase/Decrease by 1 ++; -- Relational Operators = = equal to ! = not equal to > greater than < less than >= greater than or equal to <= less than or equal to = assign value Order of Precedence Logical Operators &&: and ||: or !: not -and statements only true when both are true -or statements are true when on of the options is true -These are read from left to right, once one is true the other is omitted for or statements Input/Output cout << prints to console cin >> takes input from keyboard getline(cin, name); takes string input *If after cin goes to getline, include cin.ignore(10000,’\n’); in between If/Else Statements if(condition) { statements;} else if(condition) {statements;} else { statements;} *else if and else are optional but if else is used it must be the last one i.e. if(balance <= 1000) { interest = 2.5; } { interest = 5.0; } else if (balance >5000) { interest = 5.5; } Strings Comparison string name1= “John”; string name2= “Jonathan”; name1 == name2 F name1 > name2 F name1 < name2 T Alphabetical Length string sentence = “Today is Friday.” int length = sentence.size(); OR int length2 = senetence.length(); Concatenation string s1 = “Hi”; string s2 = “there”; string s3 = s1+s2; or s1+ “ ”+s2; Character Access string s1 = “Hi there”; string copy; for(int k=0; k<s1.size();k+=2) { copy += s1[k]; } char c = s1[4]; store h into c s1[3]= ‘x’; change t to x Functions void: functions have no return value double: functions have decimal return value int: functions have integer return value Be sure to include: #include<math.h> #include<iostream> #include<iomanip> #include<string> Call by Value/Reference Value: ‘int’ number always stays the same Reference: ‘int&’ new number calculated is stored as that variable, use this if you want to change the original value ex. Call by value/reference void change(int&a, int b); int main() { int first = 6, second = 8l change(first, second); cout << first << endl; << second << endl; } { a=1 b=a } main(): first 6 1 second 8 void change: a=1 b=8 1 Characters Check to see if character is digit isdigit(c) Convert digit to its integer value int numl char c;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

Midterm1CheatSheet - Types of Errors a. Runtime Error:...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online