In the following problems assume the iostream library has been included and that
ID: 3855685 • Letter: I
Question
In the following problems assume the iostream library has been included and that you are using the namespace std. You can denote character assignments with two single quotes around the character. For an empty string use "".
1. Suppose number1 and number2 are int variables and symbol is a char variable. Consider the following input:
17 67 * 32 $
What value (if any) is assigned to number1, number2, and symbol after each of the following statements executes? (Use the same input for each statement.)
a. cin >> number1 >> symbol >> number2;
b. cin >> symbol >> number1 >> number2;
c. cin >> number1;
cin.get(symbol);
cin >> number2;
d. cin >> number1 >> number2;
cin.get(symbol);
e. cin.get(symbol);
cin >> number1 >> number2;
2. Suppose x and y are int variables and z is a double variable. Assume the following input data:
1.41 25
Using the same input for each of the following statements, state whether there is a variable assignment or write “fail state” for x, y, and z. Hint – if cin enters the fail state there will be less than three assignments. (Use the same input for each statement.)
a. cin >> x >> y >> z;
b. cin >> x >> z >> y;
c. cin >> z >> x >> y;
3. Suppose x and y are int variables and symbol is a char variable. Assume the following input data:
93 86 * 71 53
50 $ 65 # 31
# & 30 60
What value (if any) is assigned to x, y, and symbol after each of the following statements executes? (Use the same input for each statement.)
a. cin >> x >> y;
cin.ignore(100, ' ');
cin >> symbol;
b. cin >> x;
cin.ignore(100, '*');
cin >> y;
cin.get(symbol);
c. cin >> y;
cin.ignore(100, ' ');
cin >> x >> symbol;
d. cin.get(symbol);
cin.ignore(100, '*');
cin >> x;
cin.ignore(100, ' ');
cin >> y;
e. cin.ignore(100, ' ');
cin >> x >> symbol;
cin.ignore(100, ' ');
cin.ignore(100, ‘&’);
cin >> y;
4. Given the input:
44 88 -99
and the C++ code:
int x = 3, y = 4;
char z = '*';
cin >> x >> y >> z;
cout << x << " " << y << " " << z << endl;
What is the output?
5. Suppose x and y are int variables, z is a double variable, and ch is a char variable. Suppose the input statement is:
cin >> x >> y >> ch >> z;
Ignoring fail state assignments, what are the assignments are made for x, y, z, and ch if the input is:
a. 21 45.92
b. 21 45A 92.6
c. 21 .45A 92
6. Suppose that num is an int variable and discard is a char variable.
Assume the following input data:
#34
Determine if num and discard are assigned new values or are subject to the input failure after each of the following statements executes. (Use the same input for each statement.)
a. cin.get (discard);
cin >> num;
b. discard = cin.peek();
cin >> num;
c. cin.get (discard);
cin.putback (discard);
cin >> discard;
cin >> num;
7. Suppose that name is a variable of type string. Write the input statement to read and store the input line
Chris Smith
in name. (Assume that the input is from the standard input device.)
8. Suppose that age is an int variable and name is a string variable. The input is:
a. 41Terry Wilson
b. 41 Terry Wilson
c. 41
Terry Wilson
In each case, what are the values of age and name after the following input statements execute:
cin >> age;
getline(cin, name);
9. The following program is supposed to read two numbers from a file named input.dat and write the sum of the numbers to a file named output.dat However, it fails to do so. Rewrite the program so that it accomplishes what it is intended to do. Also, write statements to close the files.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int number1, number2;
ifstream infile;
outfile.open("output.dat");
infile >> number1 >> number2;
outfile << "Sum = " << number1 + number2 << endl;
return 0;
}
10. Suppose that you have the following statements:
ofstream outfile;
double distance = 100.0; //miles
double speed = 50.0; //miles per hour
double travelTime; //hours to travel using distant and speed
Write C++ statements to do the following:
a. Open the file travel.dat using the variable outfile.
b. Write the statement to format your output to two decimal places in fixed form.
c. Write the values of the variables distance and speed on their own respective lines in the file travel.dat.
d. Calculate and write the travelTime in the file travel.dat.
e. What header files are required to make (a) to (d) compile?
Explanation / Answer
Question 1
a.
number1=17
symbol="67"
number2=* Here it will throw error as number2 is an integer type.
b.
symbol="17"
number1=67
number2=* Here it will throw error as number2 is an integer type.
c.
number1=17
symbol="67"
number2=* Here it will throw error as number2 is an integer type.
d.
number1=17
number2=67
symbol="*"
e.
symbol="17"
number1=67
number2=* Here it will throw error as number2 is an integer type.
Question 2
a.cin >> x >> y >> z;
after one input cin goes to fail state
b.cin >> x >> z >> y;
working two input done
c.cin >> z >> x >> y;
Here ther should be one more argument that is one more value should be provided.
Question 3
a.The values are
x=93
y=86
symbol=*
b.The values are
x=93
y=71
symbol=
c.IT going on for input
d.The values are
x=71
y=53
symbol=*
e.It will throw an error
Question 4.
The outpu will be
44 88
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.