Midterm1CheatSheet

# Midterm1CheatSheet - Types of Errors a Runtime Error logic...

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

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; } else if(balance > 1000 && balance <=5000) { 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; } void change(int&a, int b) { 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; cin >> c; if(isdigit(c)) { num = c - ‘0’; }

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern