Exam 1 Flashcards
(33 cards)
What are 3 common errors in if statements?
- Misplaced semicolons
- Missing braces
- Confusing = with ==
What’s wrong with this?
If (2 < count < 9)
C++ does not allow you to check numeric ranges with expressions such as 5 < x < 20. Instead you must use a logical operator to connect two relational expressions.
How do you work around a floating point error (example: .66666667 * 6.0)?
Test for closeness within a range.
Example: if (abs(result - 4.0 < .0001))
What is executed if average = 80?
if (average == 100)
cout «_space;“Congratulations! “;
cout «_space;“That’s a perfect score!\n”;
How do you work around a floating point error (example: .66666667 * 6.0)?
Test for closeness within a range.
Example: if (abs(result - 4.0 < .0001))
How do you gather user input that may include spaces?
getline(cin, varName);
&& ____ conditions must be true
|| ____ conditions must be true
! ____
Both
One or more
Reverses the truth of the expression (NOT)
How do you gather user input that may include spaces?
getline(cin, varName);
What does cin.get(); do?
It reads a single character, including any whitespace character.
To store the character, use either:
cin.get(ch);
ch = cin.get();
What is wrong with this code?
char ch; int number; cout << "Enter a number. "; cin >> number; cout << "Enter a character. "; ch = cin.get(); cout << "Thank you!\n"
cin and and cin.get() are combined, which can cause issues.
cin.get() is skipped, because the Enter key entered with the integer is stored in the buffer and then assigned to ch.
What does cin.ignore() or cin.ignore(n, c) do?
It ignores one or more characters in the keyboard buffer.
cin. ignore(n, c) can be used to skip n number of characters, or until character c is encountered.
cin. ignore() skips the next character.
What does string.lenth(); do?
It returns the number of characters in a string.
What does string.assign() do?
It is used to create a string with a certain number of characters.
Example: spaces.assign(22, ‘ ‘);
What is a c-string?
Array of characters used prior to the introduction of strings.
char word[10] = “Hello”;
What is buffer overrun?
When extra values spill over from the memory allocation for one variable into whatever was stored in the next bit of memory.
How many quotation marks are used for a string literal?
A character literal?
char letterOne = ‘A’;
string sentence = “okay”;
How many bytes of memory does a character occupy?
1
How many characters is ‘\n’?
1
What marks the end of a string?
A null terminator ‘\0’; this is one character.
What header file must you include when you use strings?
include
What does sizeof(short) do?
Returns the number of bytes of memory used by any data type or variable. This is helpful, because not all machines use the same sizes for data types.
What is wrong with this?
cout «_space;value;
int value = 100;
A variable cannot be used before it is defined.
This is a scope rule.
How are single line comments written in C++?
With //
How are multiline comments written in C++?
With /*
and */
Everything between the asterisks is ignored.