Midterm #2 Flashcards
types of input
arguments, cin»_space;, and getline
types of output
cout«
argc
argument counts
argv
argument variables
how to compile a program
g++ -o example.out example.cpp
how to run a program
./example.out (inline arguments)
(./example.out 3 10 abc) agrc
4
(./example.out 3 10 abc) argv
argv[0], argv[1], agrv[2], argv[3]
(./example.out A B C 1 2)
argc?
6
(./example.out A B C 1 2) argv[3]
c
(./example.out A B C 1 2) argv[5]
2
(./example.out A B C 1 2) argv[4] + argv[5]
we cannot have sum of two strings “1” + “2” is an invalid type of addition operator
atoi (str)
convert str to an int value
atof (str)
convert str to a double value
cout«
displays a prompt to whoever is running the program
std::
formal syntax (don’t need if “using namespace std”)
cin»
whatever the user puts into the program (becomes a variable)
getline()
if the variable you want contains a space
getline() (written out)
getline (cin, variable with space)
\
the back-slash
"
double quote marks
\t
a horizontal tab
\n
new line
std::endl
new line
branching (tree)
if, switch
if (tree)
if (){
}
if(){
}else{
}
bool type
Range: True (1) or false(0)
&& - and
|| - or
! - not
a==b==c
a==b && b==c
a != b != c
a != b && a != c && b != c
a > b > c
a > b && b > c
a < b < c
a < b && b < c
a in [b, c]
a >= b && a <= c
a ∈ [b, c]
a >= b && a <= c
a not in [b, c]
a < b || a > c
a !∈ [b, c]
a < b || a > c
one way if statement
if ( <Boolean-Expression> ){
<statement(s)>
}</Boolean-Expression>
two ways if statement
if ( <Boolean-Expression> ){
statement(s) of "true" case
}else{
statement(s) of "false" case
}</Boolean-Expression>
what can you use “if … else” for
to make multiple branches
if … else multiple branches example
if(condition1){
// statement_block1
…
} else if(condition2){
// statement_block2
…
} else if(condition3){
// statement_block3
…
} else{// statement_block4
…
}
common mistake of if statements
putting a semicolon after if expression, not using ==
what can you use “switch … case” for
when there is more options than just true and false
switch … case example
switch (integer-expression){
case Value 1:
// statement(s) for Case 1
…
break;
}
case Value2:
// statement(s) for case 2
…
break;
…
…
default:
// statement for default case
…
}
what does (char) represent
a character data type that represents a single character
what does (string) represent
a string data type that represents a sequence of characters
what is a literal character
represented by single quotation marks (‘a’ ‘5’)
message.length/message.size
shows the number of characters in a string
what is a literal string
represented by double quotation marks (“5” “csc117”)
stringname[index]
what a variable is a a certain position
stringname[index] example
string s = “ABCD”;
s[0] = ‘P’;
cout «_space;s[0] «_space;endl;
// the output is “PBCD”
while loop
writes out what ever is looped as many times as you tell it to
while loop general example
while (condition){
// statement(s);
// loop body
}
what are the beginning parts of every program
include <iostream></iostream>
#include <cmath>
#include <cctype>
using namespace std;</cctype></cmath>
int main(int argc, char * argv[]){
}