



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




This worksheet is for your use during and after lecture. It will not be collected or graded, but I think you will find it a useful tool as you learn C++ and study for the exams. Explain all false answers for the “True or False” questions; in general, show enough work and provide enough explanation so that this sheet is a useful pre-exam review. I will be happy to review your answers with you during office-hours, via Email, or instant messaging.
(a) string is to double as firstName is to ???
Solution: GPA or course_grade, variables of type double.
(b) GPA is to double as firstName is to ???
Solution: string
(c) How many double variables are declared? What are the name(s)?
Solution: Two, GPA and course_grade.
(d) How many class instances are declared? What are the name(s)?
Solution: One, firstName.
(e) Explain the difference between symbols double and course_grade.
Solution: double is a fundamental data type, course_grade is a variable of type double. course_grade has a value and takes up memory to hold that value.
(f) Explain the difference between symbols string and firstName.
Solution: string is a C++ class, firstName is an instance of the string class. firstName has a value (perhaps the empty string "") and takes up memory to hold that value.
4 double GPA (3.8) , course_grade ; 5 string firstName ;
Solution: cout is an instance of the ostream (short for output stream) class.
Solution: cin is an instance of the istream (short for input stream) class.
bool b( int > double);
Solution: int and double are data types. They don’t have any values associated with them, so comparing them with a relational operator doesn’t make sense.
a 4.23 -2.500 -3 ,e -100 4
And the keyboard is read by a program running the code sequence to the right.
32 char a , b , c ; 33 double m , n , p ; 34 int x , y , z ; 35 36 cin >> a; 37 cin >> m; 38 cin >> x; 39 cin >> b; 40 cin >> n; 41 cin >> y; 42 cin >> c; 43 cin >> p; 44 cin >> z;
(a) Complete the table below: provide the value of all variables that are successfully read by cin before it encounters a failure state. Leave any variables that are not valued by cin blank. Note that the variable order in the table is identical to the order of input operations in the code snippet. Variable a m x b n y Value ’a’ 4.23 -2 ’.’ 500 -
Variable c p z Value ’,’
(b) In a sentence, explain why cin entered a failure state.
Solution: After c is read as a comma (’,’), a double read for p is attempted. p fails because doubles must start with a number (1e-100 is OK, but e-100 is not).
Solution: ofstream outFile ( " the_file. txt " );
(b) Using the file stream object as a predicate, prints an error message to the console if part a failed.
Solution: if (! outFile ) { cout << " Error opening ’ the_file. txt ’" << endl ; }
Solution: False, C++ won’t print more decimals than there is accuracy in the number.
cout.width(10); cout << 3 << 4 << endl; Prints numbers 3 and 4 with a field width of 10.
Solution: False, only the first term printed, 3, will be in a field width of 10.
Solution: False, C++ seems to have a mind of its own in this regard.
Solution: False, they are right justified in the field width used for printing.
Solution:
using namespace std ; int main () { ifstream infile ( " DATA. DAT " ); if (! infile ) { cout << " Error opening file. " << endl ; exit (1); }
int i1 , i2 , i3 ; while ( infile >> i1 >> i2 >> i3 ) { if ( i2 == 0 ) { cout << i3 << endl ; break ; } }
infile. close ();
return 0; }