Exam 1 Flashcards
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.