Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

5. Suppose x and y are int variables, z is a double variable, and ch is a char v

ID: 3855718 • Letter: 5

Question

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

5a)Ouput is:x=21 y=45 ch=. z=92(when input is 21 45.92)

5b)Oupt is x=21 y=45 ch=A z=92.6(when input is 21 45A 92.6)

5c)Outpt is x=21 y=0 ch= z=4.94066(when input is 21 .45A 92)

C++ Code:

int x,y;
double z;
char ch;

cin>> x >> y >> ch >> z;

cout<<"x="<<x <<" y="<<y <<" ch=" <<ch<<" z="<<z;

6)C++ Code:

#include <iostream>
#include <fstream>

using namespace std;

int main()

{
  
int num;
char discard;
  
cin.get(discard); //if input is a and 10
cin >>num;
cout<<" Character="<<discard; //it displays character=a
cout<<"Number="<<num; //it displays number=10

discard = cin.peek();
cin >> num;
cout<<" Character= "<<discard;
cout<<" Number="<<num<<" ";

cin.get(discard);
cin.putback(discard);
cout<<" Character= "<<discard;
cout<<" Number="<<num<<" ";   
  
cin >> discard;
cin >> num;
cout<<" Character= "<<discard;
cout<<" Number="<<num<<" ";
system("pause");
  
return 0;
}

6a) if input is a and 10 Output:character=a number=10

6b)if input is 1 Ouput:character=1 number=1

if input is a Ouput: character=a; number=1962384948

6c)if input is 1 Ouput:character=1 number=1962384948

if input is a Ouput:character=a;number=1962384948

6d)if input is 1 and 1 Ouput:character=1 number=1

if input is p and 30 Ouput:character=p;number=30

if input is 30 Ouput:character=3 number=0

7)C++ Code:

string name; //name of type string variable
cin >>name; //input statement to read and store the input line
cout << "Name is="<< name; //Prints name Christ Smith

8a)Ouput is:age=41 name=Terry Wilson(when input is 41Terry Wilson)

8b)Ouput is:age=41 name= Terry Wilson(when input is 41 Terry Wilson)

8c)Ouput is:age=41 name= (when input is 41

Terry Wilson)

C++ Code:

int age;string name;
cin>>age;
getline(cin,name);
cout<<"Age="<<age <<" Name=" << name;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote