Chapter 4
PROGRAM INPUT AND THE
SOFTWARE DESIGN PROCESS
Programming Warm-Up Exercises
2. cin.get(ch1); _ or cin >> ch1;
cin.get(ch2);
cin.get(ch3);
5. Given the declarations
char ch1, ch2, ch3, ch4, ch5, ch6, ch7;
int n1, n2, n3, n4, n5, n6;
float x1, x2, x3;
here is one possibility:
cin >> ch1 >> n1 >> x1 >> ch2 >> n2;
cin >> x2 >> ch3 >> ch4 >> x3 >> n3;
cin >> ch5 >> n4 >> ch6 >> n5 >> ch7 >> n6;
There are many other ways of organizing and textually formatting the input statements. Here is a solution using just one input statement:
cin >> ch1 >> n1 >> x1 >> ch2 >> n2
>> x2 >> ch3 >> ch4 >> x3 >> n3
>> ch5 >> n4 >> ch6 >> n5 >> ch7 >> n6;
6. If we use nine variables, we need only one input statement and one output statement:
inFile >> int1 >> int2 >> int3 >> int4 >> int5
>> int6 >> int7 >> int8 >> int9;
cout << int1 << ' ' << int2 << ' ' << int3 << endl
<< int4 << ' ' << int5 << ' ' << int6 << endl
<< int7 << ' ' << int8 << ' ' << int9 << endl;
Alternatively, writing each integer as it is read in requires only one variable but nine separate input statements and nine separate output statements.
7. Interactive segment for novice user:
cout << "Enter your age: ";
cin >> age;
cout << "Enter your height: ";
cin >> height;
cout << "Enter your weight: ";
cin >> weight;
cout << "Enter the initial of your first name: ";
cin >> first;
cout << "Enter the initial of your last name: ";
cin >> last;
Interactive segment for experienced user:
cout << "Enter age, height, and weight: ";
cin >> age >> height >> weight;
cout << "Enter initials: ";
cin >> first >> last;
system. The example here is for Turbo C++ for DOS on a PC with a hard disk.
Main Module
Turn computer on
Start the Turbo system
Enter program
Run program
Start the Turbo system
Type "tc" and press Return
Enter program
Press F3
Enter the name of your program (with
Type the program into the editor
Use the cursor, Backspace, and Delete keys to make corrections
Run program
Press F2 to save the file
Press Ctrl/F9 to run the program
If an error message appears, note what it says, and then press any key. Otherwise, enter data as needed and observe any output, until the program stops running
Press Alt/X to exit the Turbo system
11. Main Module
Open files
Read three coefficients from input file
Calculate two floating point solutions
Write solutions to output file
Open files
Open file inQuad for input
Open file outQuad for output
Read three coefficients from input file
Read a from inQuad
Read b from inQuad
Read c from inQuad
Calculate two floating point solutions
Set solution1 = (–b + sqrt(b*b – 4.0*a*c)) / (2.0 * a)
Set solution2 = (–b – sqrt(b*b – 4.0*a*c)) / (2.0 * a)
Write solutions to output file
Write solution1 to outQuad
Write ' ' to outQuad
Write solution2 to outQuad
ANSWERS TO QUESTIONS
True/False
Multiple Choice
Fill-In